0 votes 0 votes int func(int a) { if(a<=0) return a; else { printf(a); func(a-2); printf(a); func(a-3); } } #include<stdio.h> int main(void) { int a=6; func(a); } what will be the sum of all values that printed ? Programming in C programming-in-c + – junaid ahmad asked Dec 5, 2017 edited Dec 5, 2017 by junaid ahmad junaid ahmad 494 views answer comment Share Follow See all 4 Comments See all 4 4 Comments reply joshi_nitish commented Dec 5, 2017 reply Follow Share the sum of values -> 34 notice one thing that, if(n<=0), then also function is returning some values, but they are not printed 0 votes 0 votes joshi_nitish commented Dec 5, 2017 reply Follow Share it can be solved easily using recursion tree. 0 votes 0 votes junaid ahmad commented Dec 5, 2017 reply Follow Share @joshi_nitish that is exactly what i want to know :) 0 votes 0 votes Ashwani Kumar 2 commented Dec 5, 2017 reply Follow Share two correction needed in main function calling as func(a) not int func(a) in function definition func(a-3) not rec(a-3) 1 votes 1 votes Please log in or register to add a comment.
Best answer 1 votes 1 votes Values printed : 6 4 2 2 4 1 1 6 3 1 1 3 Sum : 34 Ashwani Kumar 2 answered Dec 5, 2017 selected Dec 5, 2017 by joshi_nitish Ashwani Kumar 2 comment Share Follow See all 4 Comments See all 4 4 Comments reply Nitesh Choudhary commented Dec 5, 2017 reply Follow Share #include <stdio.h> void func(int a) { if(a<=0) return a; else { printf(a); func(a-2); printf(a); func(a-3); } } int main() { int b=1; func(b); } when i run this code on online gcc compiler http://rextester.com/l/c_online_compiler_gcc than it will give error Invalid memory reference i also not able to get how this possible anyone can try 0 votes 0 votes joshi_nitish commented Dec 5, 2017 reply Follow Share due to missing %d specifier in printf 0 votes 0 votes Ashwani Kumar 2 commented Dec 5, 2017 reply Follow Share @Nitesh you forgot to write %d in printf 0 votes 0 votes Nitesh Choudhary commented Dec 5, 2017 reply Follow Share Thanks 0 votes 0 votes Please log in or register to add a comment.