All articles
c

A program to explain DO WHILE

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 025

The following code shows how to write a program to explain DO WHILE. Find sum of even numbers below a number 'n'.

Program

#include <stdio.h>
 
void main()
{
    int i = 2, n, sum = 0;
 
    printf("Enter the value of n: ");
    scanf("%d", &n);
 
    do
    {
        sum = sum + i;
        i = i + 2;
    } while (i <= n);
 
    printf("%d", sum);
 
    printf("\n");
}

Result

Enter the value of n: 4
6

Comments