Tuesday, 13 August 2013

Chapter 5 solutions problem [J][e]

Question-Write a function to obtain the running sum of first 
25 natural numbers. 
Solution-
I don't know exactly what's the meaning of running sum. I guess it means that program should print the result as sum of the previous numbers. like if you want the running sum till 4 then it should be 1,1+2=3,1+2+3=6,1+2+3+4=10 like that, so I am writing program for this. if my concept is wrong. please,please,please let me know.one more thing i have escaped last question of binary equivalent, which is extremely trivial. if you guys have troubles with then feel free to write to me.
so, check out this solution.

#include<stdio.h>
int main()
{
int n,i;
printf("Enter the number till you want the running sum\n");

 /*it is always a good habit to write general program.so better get used to it */ 
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d ",runsum(i));
return 0;
}
runsum(int i)
{
int sum=0,j;
for(j=1;j<=i;j++)
sum=sum+j;
return(sum);

1 comment:

  1. int main()
    {
    int num,i,result;
    printf("Enter the number: ");
    scanf("%d",&num);
    result=nature(num);
    printf("The total is %d\n",result);

    }


    int nature(int seq)
    {
    if(seq!=0)
    {
    return seq+nature(seq-1);
    }
    else
    return seq;
    }

    ReplyDelete