Print Odd Numbers in C
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Print First N Odd Numbers in C Using For Loop – Complete Guide

šŸ“‘ On this page:
  • Introduction
  • What Are Odd Numbers?
  • C Program to Print First N Odd Numbers
  • Sample Output
  • Alternate Method Using Formula
  • Program Explanation
  • Conclusion
  • Frequently Asked Questions
  • Practice Assignments
šŸ“š In this tutorial, you will learn:
  • What are odd numbers and how to identify them
  • How to print first N odd numbers in C using for loop
  • Two different methods to print odd numbers
  • How the code works with step-by-step explanation
  • Practice assignments to strengthen your skills

Introduction

In this tutorial, you'll learn how to write a simple C program that prints the first N odd numbers using a for loop. This is a perfect beginner-level problem that helps you understand how loops and number sequences work in C.

What Are Odd Numbers?

Odd numbers are numbers that are not evenly divisible by 2 — they leave a remainder of 1. Examples are: 1, 3, 5, 7, 9, 11... and so on. So, when we say "print the first N odd numbers," we just need to display the first N numbers from this sequence.

šŸ’” Key Point: Odd numbers can be identified by checking if a number is not divisible by 2. In C, we use the condition i % 2 != 0 to check if a number is odd.

How Does the Program Work?

We use a loop that starts from 1 and checks each number one by one. We check if the number is odd using i % 2 != 0. If it is odd, we print it and increase the count. We stop when we've printed N odd numbers.

C Program to Print First N Odd Numbers (Method 1 - Using Modulo Operator)

#include <stdio.h>

int main() {
    int n, i, count = 0;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    printf("First %d Odd Numbers:\n", n);

    for(i = 1; count < n; i++) {
        if(i % 2 != 0)  // If i is odd
        {
            printf("%d ", i);
            count++;
        }
    }

    return 0;
}

Sample Output

Enter the value of N: 5
First 5 Odd Numbers:
1 3 5 7 9

If you enter 10, the output will be:

Enter the value of N: 10
First 10 Odd Numbers:
1 3 5 7 9 11 13 15 17 19

Alternate Method Using Formula (More Efficient)

You can also generate odd numbers using a direct formula: 2 * i - 1. This approach skips the odd/even check and directly gives you odd numbers.

#include <stdio.h>

int main() {
    int n, i;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    printf("First %d Odd Numbers:\n", n);

    for(i = 1; i <= n; i++) {
        printf("%d ", 2 * i - 1);
    }

    return 0;
}
Enter the value of N: 5
First 5 Odd Numbers:
1 3 5 7 9

šŸ“ Note: The formula method 2 * i - 1 is faster and more efficient when you only need odd numbers. The modulo method is more flexible when you need to perform extra checks or logic inside the loop.

Program Explanation

This C program asks the user to enter a number N and then prints the first N odd numbers on the screen. It uses a for loop to check each number starting from 1 and prints it only if it's odd. A separate counter keeps track of how many odd numbers have been printed so far. The loop continues until exactly N odd numbers are displayed.

Let's break down the code step by step:

  1. Include Header File: #include <stdio.h> includes the standard input/output library.
  2. Declare Variables: int n, i, count = 0; declares three variables — n for user input, i as the loop counter, and count to track how many odd numbers have been printed.
  3. Get User Input: printf("Enter the value of N: "); prompts the user, and scanf("%d", &n); reads the value.
  4. Print Heading: printf("First %d Odd Numbers:\n", n); displays a heading.
  5. For Loop: for(i = 1; count < n; i++) runs the loop until count reaches n:
    • Initialization: i = 1 starts the loop counter at 1.
    • Condition: count < n checks if we have printed enough odd numbers.
    • Update: i++ increments the counter after each iteration.
  6. Check for Odd Number: if(i % 2 != 0) checks if the current number is odd.
  7. Print and Count: printf("%d ", i); prints the odd number, and count++; increments the count.
  8. Return: return 0; indicates successful program execution.

Conclusion

This small program is a great way to practice using for loops and conditional logic in C. Understanding how to work with number patterns like odd or even numbers builds a strong foundation for more complex logic ahead. Try extending this code to print even numbers or calculate the sum of odd numbers!

šŸ’» Practice Exercise

Challenge: Modify the program to print the sum of the first N odd numbers.

šŸ” Click to Show Solution
#include <stdio.h>

int main() {
    int n, i, sum = 0;

    printf("Enter the value of N: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++) {
        sum = sum + (2 * i - 1);
    }

    printf("Sum of first %d odd numbers: %d\n", n, sum);
    return 0;
}

/* Output for N=5: Sum of first 5 odd numbers: 25 */

Frequently Asked Questions

1. What is the difference between odd and even numbers?

Odd numbers leave a remainder of 1 when divided by 2 (like 1, 3, 5, etc.), whereas even numbers are divisible by 2 with no remainder (like 2, 4, 6, etc.).

2. Why use a counter variable in the first program?

We use a counter because we don't know how many numbers we'll need to check to find exactly N odd numbers. The counter helps us track how many valid odd numbers we've printed.

3. Which method is better: using modulo or formula?

The formula method 2 * i - 1 is more efficient and direct when you just need the first N odd numbers. However, the modulo method i % 2 != 0 is more flexible if you need to perform extra checks or logic while filtering numbers.

4. Can this logic be used for even numbers too?

Absolutely! You can tweak the condition to check i % 2 == 0 or use the formula 2 * i to generate even numbers instead of odd ones.

5. What happens if I enter N = 0?

If you enter N = 0, the loop condition count < n (0 < 0) is false, so the loop doesn't run and nothing is printed.

šŸ’” Tip: Try extending this code to print even numbers or calculate the sum of odd numbers to practice more!

Practice Assignments Using For Loop

Assignment Set 1: Basic For Loop Programs

  • 1. Write a program in C using for loop to display the first N natural numbers.
  • 2. Write a program in C to print ODD numbers from 1 to N using for loop.
  • 3. Write a program in C to print EVEN numbers from 1 to N using for loop.
  • 4. Write a program in C to print all uppercase alphabets using for loop.
  • 5. Write a program in C to print all lowercase alphabets using for loop.
  • 6. Write a program in C using for loop to read 10 numbers and find their sum and average.
  • 7. Write a C program to read an integer and print its multiplication table using for loop.
  • 8. C program to check entered number is ZERO, POSITIVE or NEGATIVE using for loop.
  • 9. Write a program in C to find factorial of a number using for loop.
  • 10. Write a C program to find sum and average of first N natural numbers.
  • 11. C program to reverse a given number using for loop.
  • 12. C program to check if a number is an Armstrong number using for loop.
  • 13. C program to print Fibonacci series up to N using for loop.
  • 14. C program to check if a number is prime using for loop.
  • 15. C program to convert decimal number to binary using for loop.

Assignment Set 2: Advanced For Loop Programs

  • 1. C program to find sum of squares of first N natural numbers: 1² + 2² + 3² + ... + N²
  • 2. C program to find sum of first N natural numbers: 1 + 2 + 3 + ... + N
  • 3. C program to find sum of series: 1/1! + 1/2! + 1/3! + ... + N/N!
  • 4. C program to find sum of series: 1 + 1/2 + 1/3 + 1/4 + ... + 1/N
  • 5. C program to find sum of series: 1 + 3²/3³ + 5²/5³ + 7²/7³ + ...

šŸ“– Related Tutorials

  • for Loop in C - Complete Guide
  • for Loop Examples in C
  • while Loop in C - Complete Guide
  • do-while Loop in C - Complete Guide

Previous Topic: -->> Print Natural Numbers in C   ||   Next topic: -->> Print Even Numbers in C


šŸ“š Explore More Topics

šŸ—„ļø SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

šŸ Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

ā˜• Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.