Object

Write a program to print all even numbers from 1 to 100.

Algorithm

  1. Declare integer variable i.
  2. Set i = 2.
  3. Repeat WHILE i<=100
    • Write i.
      •  i = i + 2.
    • [End of WHILE loop.]
  4. Exit.

Flowchart

C++ Source Code

// program 49
#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
  int i = 2;
  while (i <= 100)
  {
    cout << setw(4) << i;
    i += 2;
  }
  return 0;
}

C Source Code

/*program 49*/
#include<stdio.h>

int main()
{
  int i=2;
  while(i<=100)
     {
     printf("%4d", i);
     i+=2;
     }
  return 0;
}

Output

2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
Design a site like this with WordPress.com
Get started