C program to print odd numbers from 1 to N (10, 100, 500, 1000); Through this tutorial, we will learn how to print odd numbers from 1 to N (10, 100, 500, 1000) in the c program using for loop, while loop and function.
Algorithm and Program to Print Odd Numbers from 1 to N in C
Let’s use the following algorithm and program to print odd numbers from 1 to N using for loop, function and while loop in c:
- Algorithm to Print Odd Numbers from 1 to N
- C Program to Print Odd Numbers from 1 to N using For Loop
- C Program to Print Odd Numbers from 1 to N using While Loop
- C Program to Print Odd Numbers from 1 to N using Function
Algorithm to Print Odd Numbers from 1 to N
Use the following algorithm to write a c program to print odd numbers from 1 to N (10, 100, 500, 1000); as follows:
- Step 1: Start Program
- Step 2: Read the number from user and store it in a.
- Step 3: Iterate for or while loop according to a user input a number.
- Step 4: Inside loop, use if with n % 2 != 0 condition to print odd number.
- Step 5: Stop Program
C Program to Print Odd Numbers from 1 to N using For Loop
/* C Program to Print Odd Numbers from 1 to N using For Loop and If */
#include<stdio.h>
int main()
{
int i, number;
printf("\n Please Enter the Maximum Limit Value : ");
scanf("%d", &number);
printf("\n Odd Numbers between 1 and %d are : \n", number);
for(i = 1; i <= number; i++)
{
if ( i % 2 != 0 )
{
printf(" %d\n", i);
}
}
return 0;
}
The output of the above c program; as follows:
Please Enter the Maximum Limit Value : 10 Odd Numbers between 1 and 10 are : 1 3 5 7 9
C Program to Print Odd Numbers from 1 to N using While Loop
#include <stdio.h>
int main()
{
//loop counter declaration
int number;
//variable to store limit /N
int n;
//assign initial value
//from where we want to print the numbers
number=1;
//input value of N
printf("Enter the value of N: ");
scanf("%d",&n);
//print statement
printf("Odd Numbers from 1 to %d:\n",n);
//while loop, that will print numbers
while(number<=n)
{
//Here is the condition to check ODD number
if(number%2 != 0)
printf("%d ",number);
// increasing loop counter by 1
number++;
}
return 0;
}
The output of the above c program; as follows:
Enter the value of N: 10 Odd Numbers from 1 to 10: 1 3 5 7 9
C Program to Print Odd Numbers from 1 to N using Function
/* C Program to Print Odd Numbers from 1 to N using For Loop and If */
#include<stdio.h>
int oddNoList(no){
int i;
for(i = 1; i <= no; i++)
{
if ( i % 2 != 0 )
{
printf(" %d\n", i);
}
}
}
int main()
{
int number;
printf("\n Please Enter the Maximum Limit Value : ");
scanf("%d", &number);
printf("\n Odd Numbers between 1 and %d are : \n", number);
oddNoList(number);
return 0;
}
The output of the above c program; as follows:
Please Enter the Maximum Limit Value : 10 Odd Numbers between 1 and 10 are : 1 3 5 7 9