Q1: WRITE A PROGRAM USING FOR LOOP.
A DISPLAY EVEN NUMBERS FROM 1 TO 100.
B DISPLAY EVEN NUMBERS FROM 1 TO 100
Answers
Answer:
(Skip the odd number part if you don't need it)
(And i have solved the problem in three different languages because you didn't mentioned the language)
Using C:
#include <stdio.h>
int main()
{
printf("Even numbers from 1 to 100: ");
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 0)
printf("%d, ", i);
}
printf("\nOdd numbers from 1 to 100: ");
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
printf("%d, ", i);
}
return 0;
}
Using C++:
#include <iostream>
using namespace std;
int main()
{
cout << "Even numbers from 1 to 100: " << endl;
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 0)
cout << i << ", ";
}
cout << "\nOdd numbers from 1 to 100: " << endl;
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
cout << i << ", ";
}
}
Using Java:
public class MyClass {
public static void main(String[] args) {
System.out.println("Even numbers from 1 to 100: ");
for(int i =1; i<=100; i++){
if(i%2 == 0)
System.out.print(i+ ", ");
}
System.out.println("\nOdd numbers from 1 to 100: ");
for(int i =1; i<=100; i++){
if(i%2 != 0)
System.out.print(i+ ", ");
}
}
}