Sunday, 8 July 2012

c program to find the sum of n natural numbers(for loop,while loop)

For beginners
{I am going write the c program but before you need a c compiler.there are a lot of compilers. like  Code::Blocks, dev c++  Download dev c++ its free and install it in your computer.open  dev c++ go to file-->new-->source file.}


#include<stdio.h>
#include<conio.h>
main()
{
      int n,c,sum=0;
      printf("enter a number\n");
      scanf("%d",&n);
      for(c=1;c<=n;c++)
      {
                      sum=sum+c;
                      printf("%d+",c);
                      }
                      printf("=%d",sum);
                      getch();
                      }

or


#include<stdio.h>
#include<conio.h>
main()
{
      int n,c=1,sum=0;
      printf("enter a number\n");
      scanf("%d",&n);
      while(c<=n)
      {
                      sum=sum+c;
                      printf("%d+",c);
                      c++;
                      }
                      printf("=%d",sum);
                      getch();
                      }

For beginners
{After typing this program go to execute-->compile & run-->a window will pop-up-->give file name:(anything u like)-->save as type: c source files-->click save.}



output

output of program is
enter a number
4
1+2+3+4+=10

IS IT HELPFIL? comment.



1 comment:

  1. plz upload solution of sum of series 1+2+3+...+n using goto function

    ReplyDelete