Lets write a C program to generate odd numbers between 2 integer values input by the user using For loop.
Related Read:
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers
Note 1: An odd number is an integer that is not exactly divisible by 2.
Note 2: Odd numbers are of the form (2 * number + 1);
Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.
Video Tutorial: C Program To Find Odd Numbers Between Range using For Loop
In above c program, we ask the user to input 2 integer value and store it in variables start and end. If value of start is greater than the value of end, then we swap the values.
For loop counter is initialized to start, and for loop executes until value of count is less than or equal to end. For each iteration of the for loop, count value increments by 1.
Inside for loop, for every value of count, we check if its not perfectly divisible by 2. If true, it’s a Odd number and we output that number to the console window.
#include<stdio.h> int main() { int start, end, temp, count; printf("Enter start and end value, to find odd numbers\n"); scanf("%d%d", &start, &end); if(start > end) { temp = start; start = end; end = temp; } printf("Odd numbers between %d and %d are\n", start, end); for(count = start; count <= end; count++) { if(count % 2 != 0) printf("%d\n", count); } return 0; }
Output 1
Enter start and end value, to find odd numbers
40
60
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59
Output 2
Enter start and end value, to find odd numbers
60
40
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert