Find Sum of Even Integers Proram in C

Google Advertisements

Prev Tutorial Next Tutorial

C Program to Calculate Sum of Even Integers

To write this code we need to know How to check number is ever or odd in C. First user enter upper limit to process this code.

Logic to Find Sum of Even Numbers

  • Input upper limit to find sum of all even numbers and store it in some variable say N.
  • Initialize another variable to store sum with 0 say sum = 0.
  • To find sum of even numbers we need to iterate through even numbers from 1 to n. Initialize a loop from 2 to N and increment 2 on each iteration. The loop structure should look like for(i=2; i<=N; i+=2).
  • Inside the loop body add previous value of sum with i i.e. sum = sum + i.
  • After loop print final value of sum.

Note: Operator sum += i is equivalent to sum = sum + i.

Find Sum of All Even Integers Program in C

#include<stdio.h>
#include<conio.h>

void main()
{
    int i, n, sum=0;
    clrscr();

    printf("Please Enter Upper Limit: ");
    scanf("%d", &n);

    for(i=2; i<=n; i+=2)
    {
        //Adding current even number to sum variable

		sum = sum + i
    }

    printf("Sum of All Even Number Between 1 to %d = %d", n, sum);
    getch();
}

Output

Enter The Number: 20
Sum of All Even Integers is: 110
calculate sum of all even integer in c

Find Sum of All Even Integers Program in C

#include<stdio.h>
#include<conio.h>

void main()
{
    int i,n,sum;
    sum=0;
    clrscr();
    printf("Enter The Number: ");
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        if(i%2==0)
            sum=sum+i;
    }
    printf("Sum of All Even Integers is: %d",sum);
    getch();
}

Output

Enter The Number: 30
Sum of All Even Integers is: 240
calculate sum of all even integer in c
Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders. Difference between path and classpath in Java

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs