Program to print odd numbers between 1 to n using While loop
- Write a program to print odd numbers between 1 to n using While loop in C
- Write a program to print odd numbers between 1 to n using While loop in C++
- Write a program to print odd numbers between 1 to n using While loop in Python
- Write a program to print odd numbers between 1 to n using While loop in PHP
- Write a program to print odd numbers between 1 to n using While loop in Java
- Write a program to print odd numbers between 1 to n using While loop in Java Script
- Write a program to print odd numbers between 1 to n using While loop in C#
Explanation:
To print all odd numbers between 1 and n using a While loop, you iterate through numbers from 1 to n and check if a number is odd (i.e., number % 2 != 0).
Logic
- Take input for n (upper limit).
- Use a While loop to iterate through numbers from 1 to n.
- Check if a number is odd (number % 2 != 0).
- If true, print the number.
Program to print odd numbers between 1 to n using While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int n, i = 1;
printf("Enter the value of n: ");
scanf("%d", &n);
while (i <= n) {
if (i % 2 != 0) {
printf("%d ", i);
}
i++;
}
printf("\n");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n, i = 1;
cout << "Enter the value of n: ";
cin >> n;
while (i <= n) {
if (i % 2 != 0) {
cout << i << " ";
}
i++;
}
cout << endl;
return 0;
}
n = int(input("Enter the value of n: "))
i = 1
while i <= n:
if i % 2 != 0:
print(i, end=" ")
i += 1
print()
<?php
$n = (int)readline("Enter the value of n: ");
$i = 1;
while ($i <= $n) {
if ($i % 2 != 0) {
echo $i . " ";
}
$i++;
}
echo "\n";
?>
import java.util.Scanner;
public class OddNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int i = 1;
while (i <= n) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
i++;
}
System.out.println();
sc.close();
}
}
let n = parseInt(prompt("Enter the value of n: "));
let i = 1;
while (i <= n) {
if (i % 2 !== 0) {
console.log(i);
}
i++;
}
using System;
class Program {
static void Main() {
Console.Write("Enter the value of n: ");
int n = int.Parse(Console.ReadLine());
int i = 1;
while (i <= n) {
if (i % 2 != 0) {
Console.Write(i + " ");
}
i++;
}
Console.WriteLine();
}
}