C program to find the sum of even and odd numbers in the given range; Through this tutorial, we will learn how to find the sum of even and odd numbers in the given range in the c program with the help for loop and while loop.
Algorithm and Program to find Sum of Even and Odd Numbers in Given Range
Let’s use the following algorithm and program to find sum of even and odd numbers in given range in c:
- Algorithm to find Sum of Even and Odd Numbers in Given Range
- C Program to find Sum of Even and Odd Numbers in Given Range using For Loop
- C Program to find Sum of Even and Odd Numbers in Given Range using While Loop
Algorithm to find Sum of Even and Odd Numbers in Given Range
Use the following algorithm to write a program to find the sum of even and odd numbers in a given range; as follows:
- Step 1: Start Program
- Step 2: Read the min and max number from user.
- Step 3: Calculate sum of even and odd number using for loop or while loop.
- Step 4: Print sum of even and odd number
- Step 5: Stop Program
C Program to find Sum of Even and Odd Numbers in Given Range using For Loop
/* C Program to find Sum of Even and Odd Numbers from 1 to N */ #include<stdio.h> int main() { int i, Minimum, Maximum, Even_Sum = 0, Odd_Sum = 0; printf("\n Please Enter the Minimum Value:- "); scanf("%d", &Minimum); printf("\n Please Enter the Maximum Values :- "); scanf("%d", &Maximum); for(i = Minimum; i <= Maximum; i++) { if ( i%2 == 0 ) { Even_Sum = Even_Sum + i; } else { Odd_Sum = Odd_Sum + i; } } printf("\n The Sum of Even Numbers betwen %d and %d = %d", Minimum, Maximum, Even_Sum); printf("\n The Sum of Odd Numbers betwen %d and %d = %d", Minimum, Maximum, Odd_Sum); return 0; }
The output of the above c program; as follows:
Please Enter the Minimum Value:- 20 Please Enter the Maximum Values :- 200 The Sum of Even Numbers betwen 20 and 200 = 10010 The Sum of Odd Numbers betwen 20 and 200 = 9900
C Program to find Sum of Even and Odd Numbers in Given Range using While Loop
/* C Program to find Sum of Even and Odd Numbers from 1 to N */ #include<stdio.h> int main() { int i, Minimum, Maximum, Even_Sum = 0, Odd_Sum = 0; printf("\n Please Enter the Minimum Value:- "); scanf("%d", &Minimum); printf("\n Please Enter the Maximum Values :- "); scanf("%d", &Maximum); i = Minimum; while(i<=Maximum){// loop use to iterate 1 to num if(i%2==0) //Check even number for sum Even_Sum=Even_Sum+i; else Odd_Sum=Odd_Sum+i; i++; } printf("\n The Sum of Even Numbers betwen %d and %d = %d", Minimum, Maximum, Even_Sum); printf("\n The Sum of Odd Numbers betwen %d and %d = %d", Minimum, Maximum, Odd_Sum); return 0; }
The output of the above c program; as follows:
Please Enter the Minimum Value:- 20 Please Enter the Maximum Values :- 200 The Sum of Even Numbers betwen 20 and 200 = 10010 The Sum of Odd Numbers betwen 20 and 200 = 9900