Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 209

Function output recursion

$
0
0

please tell me how to rework the function so that its output is the opposite. When entering the value -105, it should output (7 5 3), but for me it outputs (3 5 7).

#include <stdio.h>void simpleFactors(unsigned int a, unsigned int last, unsigned int n);void printFactor(unsigned int last, unsigned int n);int main(){    unsigned int a;    scanf("%u", &a);    simpleFactors(a, 2, 0);    printf("\n");    return 0;}void simpleFactors(unsigned int a, unsigned int last, unsigned int n){    if (a % last == 0)    {        if (a > 1)            simpleFactors(a / last, last, n + 1);     }    else    {        printFactor(last, n);         if (a == 1) return;        if (last * last > a)        {             printf("%ld ", a);            return;         }        simpleFactors(a, last + 1, 0);        printf("\n");    }}void printFactor(unsigned int last, unsigned int n){    if (n == 0)        return;    if (n == 1)        printf("%ld ", last);    else       printf("%ld^%ld ", last, n);}

I made some small changes in function. This program should pass some tests. I am interested in the correct output when entering the number 105. I managed to reverse the output, but now the program outputs (7 5), without the third digit.

void simpleFactors(unsigned int a, unsigned int last, unsigned int n){    if (a % last == 0)    {        if (a > 1)            simpleFactors(a / last, last, n+1);    }    else    {        if (last > a) {            printFactor(last, n);        }        if (a == 1) return;        if (last * last > a)        {            printf("%ld ", a);            printFactor(last, n);            return;        }        simpleFactors(a, last + 1, 0);        printf("\n");    }}

Viewing all articles
Browse latest Browse all 209

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>