Hey there!
Welcome to ClearUrDoubt.com.
I would recommend you to go through the below post before you continue with this.
C Program to find whether a number is even or odd.
In this post, we will look at a C program to print a range of even or odd numbers from a given number.
This can be achieved using any of the below iterative statements in C.
- do-while loop
- while loop
- for loop
We will use the do-while loop and print the even/odd numbers:
Here is the C program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* C program to print series of even or odd number */ #include<stdio.h> int main() { int initial_number, numbers_to_be_printed; printf("Enter a starting number: "); scanf("%d", &initial_number); printf("Enter the number of even/odd numbers to be printed: "); scanf("%d", &numbers_to_be_printed); printf("\n%d even numbers starting from %d:\n", numbers_to_be_printed, initial_number); int num = initial_number, count = 1; do { if(num % 2 == 0) { printf("%d ", num); count = count + 1; } num = num + 1; } while(count <= numbers_to_be_printed); printf("\n\n%d even numbers starting from %d:\n", numbers_to_be_printed, initial_number); num = initial_number; count = 1; do { if(num % 2 == 1) { printf("%d ", num); count = count + 1; } num = num + 1; } while(count <= numbers_to_be_printed); return 0; } |
Output:
Happy Learning! 🙂
Please leave a reply in case of any queries.