Q:

C Program to Find the Sum of first n even Numbers

belongs to collection: Basic C Programming Examples

0

you have to make this program in the following way:

  • C Program to Find the Sum of first n even Numbers Using For Loop
  • C Program to Find the Sum of first n even Numbers Using While Loop
  • C Program to Find the Sum of first n even Numbers Using Do While Loop
  • C Program to Find the Sum of first n even 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 the Sum of first n even Numbers Using For Loop

Algorithm

  • Program Start
  • Variable Declaration (n,i,sum)
  • Input Numbers
  • Calculating Sum
  • Displaying the Sum of first n even Numbers
  • Program End

Program

//C Program to Find the Sum of first n even Numbers Using For Loop

#include<stdio.h>
void main()
{
 //Declaring Variable
  int n, i, sum = 0 ;

//Input Number
 printf("Enter a Number\n");
 scanf("%d",&n);

 for(i=1;i<=n;i++)
  {   if(i%2==0)
      { sum = sum + i;
      }
  }
  printf("\nSum of first %d even Numbers: %d",n,sum);

}

Output

Enter a Number :
10

Sum of first 10 even Numbers : 30

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

Program

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

#include<stdio.h>
void main()
{
 //Declaring Variable
  int n, i=1, sum = 0 ;

//Input Number
 printf("Enter a Number\n");
 scanf("%d",&n);

 while(i<=n)
  {   if(i%2==0)
      { 
       sum = sum + i;
      }
      i++;
  }

  printf("\nSum of first %d even Numbers: %d",n,sum);

}

Output

Enter a Number :
5

Sum of first 5 even Numbers : 6

 

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

Program

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

#include<stdio.h>
void main()
{
 //Declaring Variable
  int n, i=1, sum = 0 ;

//Input Number
 printf("Enter a Number\n");
 scanf("%d",&n);

  do
  {
   if(i%2==0)
    {
     sum = sum + i;
    }
    i++;
  }while(i<=n);

  printf("\nSum of first %d even Numbers: %d",n,sum);

}

Output

Enter a Number
20
Sum of first 20 even Numbers : 110

 

C Program to Find the Sum of first n even Numbers Using Function

Algorithm

  • Program Start
  • Declaring variables
  • Input Number
  • Calling Function to calculate Sum of first n even Numbers
  • Print the Sum of first n even Numbers
  • Program End

Program

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

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

//Input Number
 printf("Enter a Number\n");
 scanf("%d",&n);
 s = sum(n);
 printf("\nSum of first %d even Numbers: %d",n,s);

}
int sum(int n)
{ int i, sum =0;
  for(i=1;i<=n;i++)
  {
    if(i%2==0)
      {
        sum = sum + i;
      }
  }
  return sum;

}

Output

Enter a Number
30

Sum of first 30 even Numbers : 240

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 to Find the Sum of first 10 Natural Numb... >>
<< C Program to Find the Sum of Three Numbers...