Introduction
In this post, I am going to print odd numbers in c in a given range. I am going to write this program in a number of ways. But before that let us see the expected output.
Expected Output
Enter the range to print odd numbers
10 20
Odd numbers between 10 and 20 are
11 13 15 17 19
As you can see the above expected output. We are asking the user to enter the range and then we are displaying the odd numbers between these two numbers.
In this post, I am writing various programs for printing odd numbers in c using different loops.
C Program to Print Odd Numbers using for loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,i;
printf("Enter the range to print odd numbers\n");
scanf("%d%d",&a,&b);
printf("Odd numbers between %d and %d are\n",a,b);
for(i=a;i<=b;i++)
{
if(i%2==1)
{
printf("%d ",i);
}
}
return 0;
}
Print Odd Numbers in C using while loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,i;
printf("Enter the range to print odd numbers\n");
scanf("%d%d",&a,&b);
printf("Odd numbers between %d and %d are\n",a,b);
i=a;
while(i<=b)
{
if(i%2==1)
{
printf("%d ",i);
}
i++;
}
return 0;
}
Print Odd Numbers in C using do while loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,i;
printf("Enter the range to print odd numbers\n");
scanf("%d%d",&a,&b);
printf("Odd numbers between %d and %d are\n",a,b);
i=a;
do
{
if(i%2==1)
{
printf("%d ",i);
}
i++;
}
while(i<=b);
return 0;
}
All the above c programs are printing odd numbers. We have used all three type of loops in c programming for printing our output. I hope you have understood these programs. If you have any difficulties or doubt, please feel free to contact me.
Thank you.
Other Important C Programs
- Interview Questions On C
- Switch Case in C Program to Calculate Area of Circle and Triangle
- C Language Program to Count the Number of Lowercase Letters in a Text File
- Program in C to Replace Capital C with Capital S in a File
- C Program to Remove White Spaces and Comments from a File
- Perfect Number in C Programming using All Loops
- Reverse a Number in C
- Factorial Program in C Programming
- LCM of Two Numbers in C Programming
- GCD of Two Numbers in C Programming
- Switch Case in C Programming
- C Program to Display Numbers From 1 to n Except 6 and 9
- C Program to Count the Characters in the String Except Space
- Strong Number in C Programming
- Swapping of Two Numbers in C
- The while loop in C Programming
- C Program to Find the Sum of Cubes of Elements in an Array
- C Program to Print Numbers Except Multiples of n
- Floyd Triangle in C Programming
- Operators in C with Detailed Explanation
- Print 1 to 50 using do while loop in Java
- C Program to Print Multiples of 5 using do while loop
- Palindrome in C using Pointers
- Arrays in C for Complete Beginners
- Insert and Delete element in Array in C using switch case
- Programs on Arrays in C
- Copy the Contents of One File into Another File
- Print All The Numbers In a Given Range
- C Program to Display a String in Capital Letters
- Remove Zeros from a number
- C Program to Find Camel Case without using Library Function
- C Program to Display Middle Row and Column of Matrix
- Remove Vowels from Even Position in a String