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

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com