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

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

C program to print to sum of even and odd numbers from 1 to n; Through this tutorial, we will learn how to print sum of even and odd number from 1 to n.

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

Use the following 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 output 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 output 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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.