Problem statement:- Program to print all the Even numbers from 1 to n.
Data requirement:-
Input Data:-n
Output Data:-i
Output Data:-i
Program in C
Here is the source code of the C Program to print all the Even 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 even 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 even numbers between 1 to 51
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Program in C++
Here is the source code of the C++ Program to print all the even numbers from 1 to n.
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the n value:";
cin>>n;
cout<<"Printing even numbers between 1 to "<<n<<"\n";
for(int i=1;i<=n;i++)
if(i%2==0)
cout<<i<<"\n";
}
Input/Output:
Enter the n value:42
Printing even numbers between 1 to 42
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42
Program in Java
Here is the source code of the Java Program to print all the even numbers from 1 to n.
Code:
import java.util.Scanner;
public class Print1toNAllevenNumbers {
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 even 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:
33
Printing even numbers between 1 to 33
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32
Program in Python
Here is the source code of the C Program to print all the even numbers from 1 to n.
Code:
n=int(input("Enter the n value:"))
print("Printing even numbers between 1 to ",n)
for i in range(1,n+1):
if i%2==0:
print(i)
for i in range(1,n+1):
if i%2==0:
print(i)
Input/Output:
Enter the n value:72
Printing even numbers between 1 to 72
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box