Program to print first n odd numbers using For loop

Program to print first n odd numbers using For loop

  • Write a program to print first n odd numbers using For loop in C
  • Write a program to print first n odd numbers using For loop in C++
  • Write a program to print first n odd numbers using For loop in Python
  • Write a program to print first n odd numbers using For loop in PHP
  • Write a program to print first n odd numbers using For loop in Java
  • Write a program to print first n odd numbers using For loop in Java Script
  • Write a program to print first n odd numbers using For loop in C#

Explanation:

Logic

  1. Take input for n (number of odd numbers to print).
  2. Use a for loop to iterate from 1 to n.
  3. For each iteration, compute the odd number using the formula 2*i – 1.
  4. Print the computed number.

Program to print first n odd numbers using For loop

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of odd numbers to print: ");
    scanf("%d", &n);

    for (int i = 1, count = 0; count < n; i += 2, count++) {
        printf("%d\n", i);
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of odd numbers to print: ";
    cin >> n;

    for (int i = 1, count = 0; count < n; i += 2, count++) {
        cout << i << endl;
    }

    return 0;
}

n = int(input("Enter the number of odd numbers to print: "))
for i in range(1, 2 * n, 2):
    print(i)

<?php
$n = (int)readline("Enter the number of odd numbers to print: ");

for ($i = 1, $count = 0; $count < $n; $i += 2, $count++) {
    echo $i . "\n";
}
?>

import java.util.Scanner;

public class FirstNOddNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of odd numbers to print: ");
        int n = scanner.nextInt();

        for (int i = 1, count = 0; count < n; i += 2, count++) {
            System.out.println(i);
        }

        scanner.close();
    }
}

let n = parseInt(prompt("Enter the number of odd numbers to print:"));

for (let i = 1, count = 0; count < n; i += 2, count++) {
    console.log(i);
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter the number of odd numbers to print: ");
        int n = int.Parse(Console.ReadLine());

        for (int i = 1, count = 0; count < n; i += 2, count++) {
            Console.WriteLine(i);
        }
    }
}

List of All Programs