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.
Algorithm and Programs to Print Sum of Even and Odd Numbers from 1 to n in C
Let’s use the following algorithm and program to print sum of even and odd numbers using for loop and while loop in c:
- 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