Sunday, 8 July 2012

c program to find average 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;
      float a,sum=0;
      printf("enter a number\n");
      scanf("%d",&n);
      for(c=1;c<=n;c++)
      {
                      sum=sum+c;
                      printf("%d+",c);
                      }
                      a=sum/(n+1);
                      printf("=%f\n",sum);
                      printf("average=%f",a);
                      getch();
                      }
or


#include<stdio.h>
#include<conio.h>
main()
{
      int n,c=1;
      float a,sum=0;
      printf("enter a number\n");
      scanf("%d",&n);
      while(c<=n)
      {
                      sum=sum+c;
                      printf("%d+",c);
                      c++;
                      }
                      a=sum/(n+1);
                      printf("=%f\n",sum);
                      printf("average=%f",a);
                      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.00000
average=2.00000

IS IT HELPFIL? comment.





3 comments:

  1. how to type a program that sums a sequence of integers. Assume that the first integer read the number
    of values remaining to be entered. the program should read only one value each time scanf is executed

    ReplyDelete
  2. how to exclude negative numbers

    ReplyDelete
  3. Find the addition and avarage of n number

    ReplyDelete