Print all the odd numbers from 1 to n

Problem statement:- Program to print all the odd numbers from 1 to n.

Data requirement:-

   Input Data:-n

  Output Data:-i

Program in C

Here is the source code of the C Program to print all the odd numbers from 1 to n.

Code:

#include<stdio.h>
int main()
{
    int n,i=0;
    printf("Enter the n value:");
    scanf("%d",&n);
    printf("Printing Odd numbers between 1 to %d\n",n);
    for(i=1;i<=n;i++)
        {
       if(i%2!=0)
        printf("%d " ,i);
    }
}

Input/Output:
Enter the n value:51
Printing Odd numbers between 1 to 51
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51

Program in C++

Here is the source code of the C++ Program to print all the odd numbers from 1 to n.

Code:

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter the n value:";
    cin>>n;
    cout<<"Printing Odd numbers between 1 to "<<n<<"\n";
    for(int i=1;i<=n;i++)
       if(i%2!=0)
        cout<<i<<" ";
}

Input/Output:
Enter the n value:43
Printing Odd numbers between 1 to 43
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43

Program in Java

Here is the source code of the Java Program to print all the odd numbers from 1 to n.

Code:

import java.util.Scanner;
public class Print1to100AllOddNumbers {

public static void main(String[] args) {

Scanner cs=new Scanner(System.in);
int n;
    System.out.println("Enter the n value:");
    n=cs.nextInt();
    System.out.println("Printing Odd numbers between 1 to "+n);
    for(int i=1;i<=n;i++)
       if(i%2!=0)
        System.out.print(i+" ");
    cs.close();
}
}

Input/Output:
Enter the n value:
77
Printing Odd numbers between 1 to 77
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77

Write a PYTHON print all the odd numbers up to n. or Write a program to print all the odd numbers up to n in Python.


Program in Python

Here is the source code of the Python Program to print all the odd numbers from 1 to n.

Code:

n=int(input("Enter the n value:"))
print("Printing Odd numbers between 1 to ",n)
for i in range(1,n+1):
    if i%2!=0:
     print(i,end=" ")

Input/Output:
Enter the n value:23
Printing Odd numbers between 1 to  23
1 3 5 7 9 11 13 15 17 19 21 23 

Post a Comment

0 Comments