Q:

C Program To Find Sum of N Numbers

belongs to collection: Basic C Programming Examples

0

you have to make this program in the following way:

  • C Program to Find Sum of n Numbers Using For Loop
  • C Program to Find the Sum of n Numbers Using While Loop
  • C Program to Find the Sum of n Numbers Using Do While Loop
  • C Program to Find the Sum of n Numbers Using Function

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  C Program To Find Sum of N Numbers Using For Loop 

Program -:

//Program to Calculate Sum of n Numbers In C Using Loop

#include<stdio.h>
int main()
{
  int i,n,sum=0,num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  for(i=0;i<n;++i)
   {
     scanf("%d",&num);
     sum = sum +num;
   }

  printf("\nSum of given Numbers = %d",sum);

  return 0;
}

Output -:

Enter How Mant Number You Want?
5
Enter elements one by one
10
20
30
40
50
Sum of given number = 150

 

C Program to Find the Sum of n Numbers Using While Loop

Program -:

//C Program to Find the Sum of n Numbers Using While Loop

#include<stdio.h>
int main()
{
  int i = 0, n, sum=0, num;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  while(i<n)
   {
     scanf("%d",&num);
     sum = sum +num;
     i++;
   }

  printf("\nSum of given Numbers : %d",sum);

  return 0;
}

Output -:

Enter How Mant Number You Want?
5 Enter elements one by one 1 2 3 4 5
Sum of given number : 15

C Program to Find the Sum of n Numbers Using Do While Loop

Program -:

//C Program to Find the Sum of n Numbers Using Do While Loop

#include<stdio.h>
int main()
{
  int i = 0, n, sum=0, num;
  float avg;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  printf("\nEnter elements one by one\n");
  do
   {
     scanf("%d",&num);
     sum = sum +num;
     i++;
   }while(i<n);

  printf("\nSum of Given Numbers : %d",sum);

  return 0;
}

Output -:

Enter How Mant Number You Want?
3
Enter elements one by one 
1
2
3
Sum of given number : 6

C Program to Find the Sum of n Numbers Using Function

Program -:

//C Program to Find the Sum of n Numbers Using Function

#include<stdio.h>
int sum(int);
int main()
{
  int n, s;

  printf("\nEnter How many Number you want?\n");
  scanf("%d",&n);

  //calling function to calculate sum
   s = sum(n);

  printf("\nSum of given Numbers = %d", s);

  return 0;
}
int sum(int n)
{
  int i, sum=0, num;

  printf("\nEnter elements one by one\n");
  for(i=0;i<n;i++)
   {
     scanf("%d",&num);
     sum = sum +num;
   }
   return (sum);
}

Output -:

Enter How Mant Number You Want?
3
Enter elements one by one 
11
22
33

Sum of given number : 66

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program for Addition of Two Numbers... >>