In this post, I am going to write a C program to print ‘Odd numbers’ from 1 to N.
First I need to get the N value from the user and then iterate using for loop.
To find the ‘Odd numbers’ I need to divide each value by 2, if the remainder is not zero, then it is an ‘Odd number’.
Let’s see the entire C program to print odd numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> int main() { int i, n; //print a string about the program printf("C Program to print Odd numbers!\n"); //get the limit (n) value printf("Enter the limit (n): "); scanf("%d", &n); //start printing even numbers printf("Odd Numbers:\n"); //iterate for(i=1; i<=n; i++){ //to find remainder use % modulus operator //if the value of remainder is not equal to zero then it is an odd number if(i % 2 != 0){ printf("%d\n", i); } } } |
Go through the comments in the above program to understand it.
Sample output of the above program:
1 2 3 4 5 6 7 8 9 10 11 12 |
C Program to print Odd numbers! Enter the limit (n): 10 Odd Numbers: 1 3 5 7 9 -------------------------------- Process exited after 1.763 seconds with return value 10 Press any key to continue . . . |
Enjoy learning!
One thought on “C Program to print Odd numbers from 1 to N!”