Write a program to read a number from user and
1. Print all even numbers between 0 and the given number
2. Print all odd numbers between 0 and the given number
Appropriate answers only
Answers
Answer:
def printEvenNumbers(end):
for i in range(end):
if(i%2 == 0):
print(i,end=" ")
def printOddNumbers(end):
for i in range(end):
if(i%2 == 1):
print(i , end = " ")
n = int(input("Enter the maximum range for printing odd numbers:"))
m = int(input("Enter the maximum range for printing even numbers:"))
print("All even numbers between 0 and",m)
printEvenNumbers(m)
print("\nAll odd numbers between 0 and",n)
printOddNumbers(n)
java-
import java.util.Scanner;
public class Main
{
static void printEvenNumbers(int end){
for(int i = 0; i <= end; i++){
if(i%2==0){
System.out.print(i + " ");
}
}
}
static void printOddNumbers(int end){
for(int i = 0; i <= end; i++){
if(i%2 == 1){
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter range for even numbers");
int n = sc.nextInt();
System.out.println("Enter range for odd numbers");
int m = sc.nextInt();
System.out.println("Even numbers between 0 and " + n);
printEvenNumbers(n);
System.out.println("\nOdd numbers between 0 and " + m);
printOddNumbers(m);
}
}
c++
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
void printOddNumbers(int end){
for(int i = 0; i <= end; i++){
if(i%2 == 1){
cout<<i<<" ";
}
}
}
void printEvenNumbers(int end){
for(int i = 0; i <= end; i++){
if(i%2 == 0){
cout<<i<<" ";
}
}
}
int main() {
int even , odd;
cout<<"Enter the range for printing even numbers:";
cin>>even;
cout<<"Enter the range for printing odd numbers:";
cin>>odd;
cout<<"Odd numbers between 0 and " << odd<<"\n";
printOddNumbers(odd);
cout<<"\n";
cout<<"even numbers between 0 and " << even<<"\n";
printEvenNumbers(even);
return 0;
}
c-
#include <stdio.h>
void printEvenNumbers(int end){
for(int i = 0; i <= end; i++){
if(i%2 == 0){
printf("%d ",i);
}
}
}
void printOddNumbers(int end){
for(int i = 0; i <= end; i++){
if(i%2 == 1){
printf("%d ",i);
}
}
}
int main() {
int even,odd;
printf("Enter range for even numbers:");
scanf("%d" , &even);
printf("Enter range for odd numbers:");
scanf("%d ", &odd);
printf("Printing even numbers between 0 and %d \n" , even);
printEvenNumbers(even);
printf("\nOdd numbers between 0 and %d \n" , odd);
printOddNumbers(odd);
return 0;
}
Explanation: