C Program to Print Sum of Even and Odd Numbers from 1 to n

In this tutorial, i am going to show you how to print sum of even and odd number from 1 to n in c program using for loop and while loop.

All C Programs to Print Sum of Even and Odd Numbers from 1 to n

  • Algorithm to Find Sum of Even and Odd Numbers from 1 to N
  • C Program to Print Sum of Even and Odd Numbers from 1 to n using For Loop
  • C Program to Print Sum of Even and Odd Numbers from 1 to n using While Loop

Algorithm to Find Sum of Even and Odd Numbers from 1 to N

Follow the below given algorithm to calculate the sum of even and odd numbers from 1 to n; as follows:

  • Step 1: Start Program
  • Step 2: Read N number from user and store them into a variable
  • Step 3: Calculate sum of n even and odd number using for loop and while loop
  • Step 4: Print sum of even and odd number
  • Step 5: Stop Program

C Program to Print Sum of Even and Odd Numbers from 1 to n using For Loop

/* C Program to find Sum of Even and Odd Numbers from 1 to N */
 
#include<stdio.h>
 
int main()
{
  int i, number, Even_Sum = 0, Odd_Sum = 0;
 
  printf("\n Please Enter the Maximum Limit Value : ");
  scanf("%d", &number);
  
  for(i = 1; i <= number; i++)
  {
  	if ( i%2 == 0 ) 
  	{
        Even_Sum = Even_Sum + i;
  	}
  	else
  	{
  		Odd_Sum = Odd_Sum + i;
	}
  }
  printf("\n The Sum of Even Numbers upto %d  = %d", number, Even_Sum);
  printf("\n The Sum of Odd Numbers upto %d  = %d", number, Odd_Sum);
  return 0;
}

The result of the above c program; as follows:

Please Enter the Maximum Limit Value : 10
The Sum of Even Numbers upto 10  = 30
The Sum of Odd Numbers upto 10  = 25

C Program to Print Sum of Even and Odd Numbers from 1 to n using While Loop

/* C Program to find Sum of Even and Odd Numbers from 1 to N */
 
#include<stdio.h>
 
int main()
{
  int i = 1, number, Even_Sum = 0, Odd_Sum = 0;
 
  printf("\n Please Enter the Maximum Limit Value : ");
  scanf("%d", &number);
  
  while(i <= number)
  {
  	if ( i%2 == 0 ) 
  	{
        Even_Sum = Even_Sum + i;
  	}
  	else
  	{
  		Odd_Sum = Odd_Sum + i;
	}
	i++;
  }
  printf("\n The Sum of Even Numbers upto %d  = %d", number, Even_Sum);
  printf("\n The Sum of Odd Numbers upto %d  = %d", number, Odd_Sum);
  return 0;
}

The result of the above c program; as follows:

Please Enter the Maximum Limit Value : 5
The Sum of Even Numbers upto 5  = 6
The Sum of Odd Numbers upto 5  = 9

Be First to Comment

Leave a Reply

Your email address will not be published.