1. Basic Programs

C Program to Calculate the Sum of Odd & Even Numbers

In this program 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.

#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;
 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);
Comments to: C Program to Calculate the Sum of Odd & Even Numbers

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.


Welcome to WikiCoders

Join the site and store the save posts for later, share your own articles
Join WikiCoder
Registration is closed.