Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C Program to Print Sum of all Even Numbers

by suresh

How to write a C Program to Print Sum of all Even Numbers using If Statement with an example.

C Program to Print Sum of all Even Numbers from 1 to n

This program allows the user to enter the maximum limit value. Next, this C Program finds the Sum of all Even Numbers in a given range.

TIP: We already explained the logic to check whether the given is Even or Not in C Program to Check Odd or Even article.

/* C Program to Print Sum of all Even Numbers from 1 to N */
 
#include<stdio.h>
 
int main()
{
  int i, number, Sum = 0;
 
  printf("\n Please Enter the Maximum Limit Value : ");
  scanf("%d", &number);
  
  printf("\n Even Numbers between 0 and %d are : ", number);
  for(i = 1; i <= number; i++)
  {
    if ( i%2 == 0 ) //Check whether remainder is 0 or not
    {
  	printf("%d  ", i);
        Sum = Sum + i;
    }
  }
  printf("\n The Sum of All Even Numbers upto %d  = %d", number, Sum);

  return 0;
}

OUTPUT

C Program to Print Sum of all Even Numbers from 1 to N 1

ANALYSIS

Within this C Program to find Sum of all Even Numbers from 1 to N , For Loop will make sure that the number is between 1 and maximum limit value.

for(i = 1; i <= number; i++)

In the Next line, We declared the If statement

if ( number % 2 == 0 )

Any number that is divisible by 2 is even number. If condition checks whether the remainder of the number divided by 2 is exactly equal to 0 or not. If the condition is True, then it is Even number, and the C Programming compiler will add i value to sum.

C Program to Print Sum of all Even Numbers from 1 to n

This program to find Sum of all Even Numbers is the same as above. But we altered for loop to eliminate the If statement.

/* C Program to Print Sum of all Even Numbers from 1 to N */
 
#include<stdio.h>
 
int main()
{
  int i, number, Sum = 0;
 
  printf("\n Please Enter the Maximum Limit Value : ");
  scanf("%d", &number);
  
  printf("\n Even Numbers between 0 and %d are : ", number);
  for(i = 2; i <= number; i=i+2)
  {
    Sum = Sum + i;
    printf("%d  ", i);
  }
  printf("\n The Sum of All Even Numbers upto %d  = %d", number, Sum);

  return 0;
}

OUTPUT

C Program to Print Sum of all Even Numbers from 1 to N 2

Program to Print Sum of all Even Numbers in a Given Range

This C program allows the user to enter Minimum and maximum value. Next, the C program will calculate the sum of even numbers between Minimum value and maximum value.

/* C Program to Print Sum of all Even Numbers in a Given Range */
 
#include<stdio.h>
 
int main()
{
  int i, Minimum, Maximum, Sum = 0;
 
  printf("\n Please Enter the Minimum, and Maximum Limit Values : \n");
  scanf("%d %d", &Minimum, &Maximum);
  
  printf("\n Even Numbers between %d and %d are : ", Minimum, Maximum);
  for(i = Minimum; i <= Maximum; i+=2)
  {
    printf("%d  ", i);
    Sum = Sum + i;
  }
  printf("\n\n The Sum of All Even Numbers betwen %d and %d  = %d", Minimum, Maximum, Sum);

  return 0;
}

OUTPUT

C Program to Print Sum of all Even Numbers from 1 to N 3

Placed Under: C Programs

  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy