Aug 1, 2013 #1 A ashwini jayaraman Member level 2 Joined Jan 17, 2013 Messages 49 Helped 0 Reputation 0 Reaction score 0 Trophy points 1,286 Activity points 1,601 int rec(int n1) { int x=8; if(n1<=80) { int n1=x+rec(n1); return(n1); } else return 0; } #include<stdio.h> int rec(int n); void main() { int n; printf("enter no"); scanf("%d",&n); rec; printf("%d",rec); } I want 8*10= 80 to be printed!!!!
int rec(int n1) { int x=8; if(n1<=80) { int n1=x+rec(n1); return(n1); } else return 0; } #include<stdio.h> int rec(int n); void main() { int n; printf("enter no"); scanf("%d",&n); rec; printf("%d",rec); } I want 8*10= 80 to be printed!!!!
Aug 1, 2013 #2 jayanth.devarayanadurga Banned Joined Dec 4, 2012 Messages 4,280 Helped 822 Reputation 1,654 Reaction score 791 Trophy points 1,393 Location Bangalore, India Activity points 0 Can you expalain what you are trying to do? Code C - [expand]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int rec(int n1) { int x=8; if(n1<=80) { int n2=x+rec(n1); //if n1 = 80 then n2 = 88 return(n2); } else return 0; } #include<stdio.h> int rec(int n); void main() { int n, m; printf("enter no"); scanf("%d",&n); m = rec(n); printf("%d", m); }
Can you expalain what you are trying to do? Code C - [expand]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int rec(int n1) { int x=8; if(n1<=80) { int n2=x+rec(n1); //if n1 = 80 then n2 = 88 return(n2); } else return 0; } #include<stdio.h> int rec(int n); void main() { int n, m; printf("enter no"); scanf("%d",&n); m = rec(n); printf("%d", m); }
Aug 1, 2013 #3 Y YEAR_ Junior Member level 3 Joined Dec 6, 2011 Messages 25 Helped 1 Reputation 2 Reaction score 1 Trophy points 1,283 Activity points 1,410 ashwini jayaraman said: if(n1<=80) { n1=x+rec(n1); return(n1); } Click to expand... This will put your program in an infinite loop. The problem with the code is 1. you are using n1 as a case condition. 2. your program is recursively calling itself without modifying n1. n1 can't be updated unless the following line is executed. ashwini jayaraman said: n1=x+rec(n1); Click to expand... what you can do for example is n1 = n1 * x; rec(n1); *but if you write it like n1 = n1 + x; you will get 18, as only the first computation(round) result is being returned. Last edited: Aug 1, 2013
ashwini jayaraman said: if(n1<=80) { n1=x+rec(n1); return(n1); } Click to expand... This will put your program in an infinite loop. The problem with the code is 1. you are using n1 as a case condition. 2. your program is recursively calling itself without modifying n1. n1 can't be updated unless the following line is executed. ashwini jayaraman said: n1=x+rec(n1); Click to expand... what you can do for example is n1 = n1 * x; rec(n1); *but if you write it like n1 = n1 + x; you will get 18, as only the first computation(round) result is being returned.