Adsense 728 * 15

Search

Custom Search

C, C++ Program to Print Even Numbers from 1 to 100

Write a C, C++ program to print even numbers from 1 to 100.  OR Write a C, C++ program to print even numbers from 1 to n where value of n is input by user. This question is pretty easy, so first try to solve on it's own.

Even Numbers

Even numbers are those numbers which is exactly divided by 2.

For example - 2, 4 ,6 etc. are even numbers.

Program to print odd and even numbers of an array.

Program to check whether number is even or odd.

C, C++ interview questions

C Program to Print Even Numbers from 1 to N


#include <stdio.h>

int main(void) {
 
 int n;
 
 printf ("Print all even numbers from 1 to :\n");
 scanf ("%d", &n);
 
 printf ("Even numbers from 1 to %d is : ",n);
 
 for (int i = 1; i <= n; i++) {
  
  if ( i % 2 == 0) {
   
   printf (" %d ", i);
  }
  
 }
 
 return 0;
}


C++ Program to Print Even Numbers from 1 to 100 using For Loop


#include <iostream>
using namespace std;

int main() {
 
   int i;
 
   /* Run a loop from 1 to 100. */
 
   for(i = 1; i < = 100; i++){
  
    /* If number is divisible by 2. */
  
    if(i % 2 == 0) {

      cout << i <<" "; 
     }
   }
    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

C++ Program to Print Even numbers from 1 to 100 using While Loop


#include <iostream>
using namespace std;

int main() {
 
 /* Initialize i with 1. */
 
 int i=1;
 
 /* If i is less than or equal to 100. */
 
 while( i <= 100){
  
  /* If number is divisible by 2, then print.*/
  
  if(i % 2 == 0){
   
   cout <<i<< " ";
  }
  
  /* Increment i. */
  
  i++;
 }
 
 return 0;
}

5 comments:

  1. please describe to show the even and odd number by if else condition in c++

    ReplyDelete
  2. Is there any other logic for solving the same program as it is most common logic shown in every websites..��

    ReplyDelete