Problem statement
Write a program to take a input number N and find how many even numbers between 0 to N.
How to find even numbers?
A number which is divisible by 2 is called even number.
An integer that is not an even number is an odd number.
Generally, the last digit of even number is 0, 2, 4, 6 or 8.
Program to find even numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int i=0; int num=0; printf("Enter the number\n"); scanf("%d\n", &num); printf("List of even numbers upto %d is\n", num); for(i=0; i<=num; i++) { if(i%2 == 0) { printf("%d \t", i); } } } |
Output
1 2 3 4 |
Enter the number 20 List of even numbers up to 20 is 0 2 4 6 8 10 12 14 16 18 20 |
Explanation:
In above program, first of all, we take a number i.e. 20 as an input from the user.
We are going to print all even numbers from 0 to input number i.e. 20.
Here for looping, we use for loop.
The for loop will give us one by one number like 1,2,3… up to limit i.e.20.
So we are going to check each number one by one by using control statement if.
if is used to check given number is even or not.
If a number is even it get print otherwise next number will check.
In this way, we will get a list of all even numbers up to 20.
Program to find odd numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int i=0; int num=0; printf("Enter the number\n"); scanf("%d\n", &num); printf("List of odd numbers upto %d is\n", num); for(i=0; i<=num; i++) { if(i%2 != 0) { printf("%d \t", i); } } } |
Output
1 2 3 4 |
Enter the number 44 List of odd numbers upto 44 is 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 |