C Program to calculate the sum of odd and even numbers in C

0
C Program

In this program, we will calculate the sum of odd and even numbers in C, for that we will take a maximum value as input from the user. Starting from 1 we will run a for loop with increment by 1 and check if the number is even or odd. We will also create two variable as sum_of_even and sum_of_odd  numbers. Upon checking the even and odd value we will increase the value by one and at the end of the loop we will have the total number of even and odd values.

Program to calculate the sum of odd and even numbers in C

#include <stdio.h>
  
void main()
 
{
 
 int i, num, sum_of_odd = 0, sum_of_even = 0;
  
 printf("Enter the value of num\n");
 
 scanf("%d", &num);
 
 for (i = 1; i <= num; i++)
 
 {
 
 if (i % 2 == 0)
 
 sum_of_even = sum_of_even + i;
 
}
 
 else
{
 
 sum_of_odd = sum_of_odd + i;
 
 }
 
 printf("Sum of all odd numbers = %d\n", sum_of_odd);
 
 printf("Sum of all even numbers = %d\n", sum_of_even);
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *