Home

A program to explain DO WHILE

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


Last Updated on

Next Post: Rules vs Guidelines →

Comments