So i'm learning from scratch, from my workbook, but it's confusing me.
I had an excersize to do: "write a recursive sub-program that gets a number (n) and prints all the whole numbers from n to 1. meaning if n=5 it should print 5 4 3 2 1.
This is the workbook solution:
{
if(n>0)
{
printf("%d", n);
printf(n-1);
}
}
My solution was this: (they only gave the sub-program, excluding the program body so i'm not sure if the sub-program is void or int or w/e...)
mine didn't work:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int num(int n);
void main(){
int n,s;
clrscr();
printf("Enter a number:");
scanf("%d", &n);
s=num(n);
printf("%d", s);
getch();
}
int num(int n)
{
if (n>0){
printf("%d", n);
return n-1;
}
I don't know what i did wrong, cause i tried to do it like they wanted and it just printed "55555" endlessly, and my method prints out "54" only...