Odd Numbers in C in a Given Range

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

  1. Interview Questions On C
  2. Switch Case in C Program to Calculate Area of Circle and Triangle
  3. C Language Program to Count the Number of Lowercase Letters in a Text File
  4. Program in C to Replace Capital C with Capital S in a File
  5. C Program to Remove White Spaces and Comments from a File
  6. Perfect Number in C Programming using All Loops
  7. Reverse a Number in C
  8. Factorial Program in C Programming
  9. LCM of Two Numbers in C Programming
  10. GCD of Two Numbers in C Programming
  11. Switch Case in C Programming
  12. C Program to Display Numbers From 1 to n Except 6 and 9
  13. C Program to Count the Characters in the String Except Space
  14. Strong Number in C Programming
  15. Swapping of Two Numbers in C
  16. The while loop in C Programming
  17. C Program to Find the Sum of Cubes of Elements in an Array
  18. C Program to Print Numbers Except Multiples of n
  19. Floyd Triangle in C Programming
  20. Operators in C with Detailed Explanation
  21. Print 1 to 50 using do while loop in Java
  22. C Program to Print Multiples of 5 using do while loop
  23. Palindrome in C using Pointers
  24. Arrays in C for Complete Beginners
  25. Insert and Delete element in Array in C using switch case
  26. Programs on Arrays in C
  27. Copy the Contents of One File into Another File
  28. Print All The Numbers In a Given Range
  29. C Program to Display a String in Capital Letters
  30. Remove Zeros from a number
  31. C Program to Find Camel Case without using Library Function
  32. C Program to Display Middle Row and Column of Matrix
  33. Remove Vowels from Even Position in a String

FAQs

Leave a Comment