Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, July 31, 2022

C program to find the sum of odd and even numbers up to 100.


C program to find the sum of odd and even numbers up to 100.




This blog post will show you how to write a C program that will calculate the sum of odd and even numbers up to 100. The program is easily adaptable to find the sum of odd and even numbers up to any number.


Introduction:

Summing odd and even numbers is a common programming task that can be accomplished with loops and conditional statements. In this tutorial, we'll look at how to solve this problem using the C programming language. This article will provide you with a solid foundation to approach similar challenges, whether you are a beginner or an experienced programmer.


Recognizing Odd and Even Numbers:

Before we begin, let's make sure we understand the difference between odd and even numbers. An even number is any integer that can be divided by two without leaving a remainder. An odd number, on the other hand, is an integer that is not divisible by 2 without a remainder. Even numbers include 2, 4, 6, and 8, while odd numbers include 1, 3, 5, and 7.


Program Coding:


#include<stdio.h>
int main()
{
int n,esum=0,osum=0;
for(n=1;n<=100;++n)
{
if(n%2==1)
osum=osum+n;
else
esum=esum+n;
}
printf("\nSum of odd numbers=%d", osum);
printf("\n Sum of even numbers=%d", esum);
}



Explanation of the Program:

The above C program calculates the sum of odd and even numbers from 1 to 100. It initializes two variables, esum and osum, to store the sums of even and odd numbers, respectively, both initially set to 0. The program then uses a for loop to iterate through numbers from 1 to 100. Within the loop, it checks if the current number n is odd by using the modulus operator %. If the remainder of n divided by 2 is equal to 1, it means n is odd, and it adds n to the osum variable. Otherwise, if the remainder is 0, it means n is even, and it adds n to the esum variable. After the loop, the program displays the sum of odd numbers and the sum of even numbers using the printf function.


Output:





Conclusion:

Finally, we have created a C program that finds the sum of odd and even numbers up to 100. In the C programming language, this program demonstrates the use of loops, conditional statements, and variables. Any aspiring programmer must first understand these concepts. 

You can improve your coding skills and become proficient in solving various problems by practicing and exploring more programming challenges. Continue experimenting and pushing your limits to advance as a programmer.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages