• Lets see how to write a c program to print even numbers using c programming.\
  • Its a basic example to print only even numbers 
  • Use for loop and iterate from 1 to n.
  • In each iteration check if number if divisible by 2 (n%2 == 0). if yes then its a even number otherwise it is a odd number.
  • C program to print even numbers from 1 to n. 
print even numbers in c


  1. #include<stdio.h> 
  2. //write a program to print even numbers in c 
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.   
  13.     for (i=1; i<=number; i++){
  14.         
  15.         if(i%2 ==0){
  16.           printf("%d\n" ,i);
  17.                
  18.         }
  19.               
  20.         }
  21.         
  22.    getch();
  23. }


Output:
  1. Enter a number: 12
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. 12


#2 print even numbers in c using while loop

  • Now we will try to print even numbers using while loop
  • Only difference is replace for with while loop
  • Variable initialization will be done before loop
  • condition will be inside while(condition)
  • increment or decrement will be last line of while block 
  • Lets print even numbers using while loop from 1 to n.


  1. #include<stdio.h> 
  2. //write a program to print even numbers in c using while loop
  3. //www.instanceofjava.com
  4. int main()
  5. {
  6.     // declare variables 
  7.     int number,i;
  8.     //read input from user 
  9.     printf("Enter a number: ");
  10.     scanf("%d", &number);

  11.    //iterate and check if it a even number or not 
  12.      i=1;
  13.        
  14.         while(i<=number){
  15.                 
  16.         if(i%2 ==0){
  17.           printf("%d\n" ,i);
  18.                
  19.         }
  20.           i++  ;  
  21.         }
  22.         
  23.    getch();
  24. }


Output:
  1. Enter a number: 20
  2. 2
  3. 4
  4. 6
  5. 8
  6. 10
  7. 12
  8. 14
  9. 16
  10. 18
  11. 20


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu