TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C Tutorials

C program to find sum of even numbers from 1 to n

Overview

In this post, we will develop a C program to find the sum of even numbers from 1 to n. To find the sum of even numbers we need to iterate the numbers from 1 to n.

C Program

#include 
int main()
{
 int n;
 //initialize sum variable
 int sum = 0;
 int i = 2;
 printf("Please input the number n:");
 scanf("%d",&n);
 while (i <= n)
 {
 sum = sum + i;
 i =i + 2;
 }
 printf("Sum of even numbers from 1 to n is:= %d\n", sum);
 return 0;
}

–

Sum of Even Numbers C Program

 

To find the sum we have initialized a variable sum to 0. We accumulate the sum of the even numbers from the range 1 to n.

To get the even number we will start with the 2 and in each iteration
we will increment the i variable with 2.

i = i + 2;

Sample Output

Please input the number n:15
Sum of even numbers from 1 to n is:= 56

C Program Sample Output

 

 

The IDE used in the tutorial is Code:: Blocks.

To download and install Code Blocks follow the link:
https://www.testingdocs.com/download-and-install-codeblocks/

For more information on Code Blocks IDE, visit the official website of Code blocks IDE: http://www.codeblocks.org/

 

Related Posts

C Language Characteristics

C Tutorials /

C Language Characteristics

C Source Code .c File

C Tutorials /

C Program Files & Formats

GNU Compiler Collection GCC

C Tutorials /

C Compilers

C Pointers

C Tutorials /

C Pointers

C Tutorials /

C Two-Dimensional Arrays

‹ C Program to get and display a number› C Program to generate Fibonacci sequence into rows and columns

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version