Graded Lab3
Graded Lab3
#include <iostream>
int main()
{
for (int i=1;i<=10;i++)
{
cout<<2<<"*"<<i<<"="<<2*i<<endl;
}
return 0;
}
Question 2:
Write a program in C++ to display first 10 natural numbers.
#include <iostream>
int main()
{
cout<<"The first ten natural numbers are:"<<endl;
for (int i=1;i<=10;i++)
{
cout<<i<<endl;
}
return 0;
}
Question 3:
Write a program in C++ to display the table of odd number from first 10 natural numbers.
#include <iostream>
int main()
{
int num;
cout<<"Enter an odd number between 1 and 10."<<endl;
cin>>num;
if(num%2!=0&&num<=10)
{
for (int i=1;i<=10;i++)
{
cout<<num<<"*"<<i<<"="<<num*i<<endl;
}
}
else
{
cout<<"Invalid.Enter an odd number between 1 and 10"<<endl;
}
return 0;
}
Question 4:
Write a program in C++ to display the table of Even number from first 10 natural numbers.
#include <iostream>
int main()
{
int num;
cout<<"Enter an even number from 1 to 10."<<endl;
cin>>num;
if(num%2==0&&num<=10)
{
for (int i=1;i<=10;i++)
{
cout<<num<<"*"<<i<<"="<<num*i<<endl;
}
}
else
{
cout<<"Invalid.Enter an even number between 1 and 10"<<endl;
}
return 0;
}
Question 5:
Write a program in C++ in which menu display: press 1 for odd number, press 2 for even
numbers.
#include <iostream>
using namespace std;
int main()
{
int choice;
cout<<"Press 1 for odd numbers."<<endl;
cout<<"Press 2 for even numbers."<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"The odd numbers from 1 to 10 are"<<endl;
for (int i=1;i<=10;i+=2)
{
cout<<i<<endl;
}
break;
case 2: cout<<"The even numbers from 1 to 10 are"<<endl;
for (int i=2;i<=10;i+=2)
{
cout<<i<<endl;
}
break;
default: cout<<”Invalid input.”<<endl;
}
return 0;
}
Question 6:
Write a program in C++ in which display the Menu:
Press 1 for 10 Natural numbers
Press 2 for display table of Even numbers.
Press 3 for display Odd numbers.
Press 4 for check number is Even or Odd
#include <iostream>
int main()
{
int choice,num;
cout<<"Press 1 for 10 Natural numbers."<<endl;
cout<<"Press 2 for display table of Even number."<<endl;
cout<<"Press 3 for display Odd numbers."<<endl;
cout<<"Press 4 for check number is Even or Odd."<<endl;
cin>>choice;
switch(choice)
{
case 1: cout<<"The first 10 Natural numbers are"<<endl;
for (int i=1;i<=10;i++)
{
cout<<i<<endl;
}
break;
case 2: cout<<"Enter an even number from 1 to 10."<<endl;
cin>>num;
for (int i=1;i<=10;i++)
{
cout<<num<<"*"<<i<<"="<<num*i<<endl;
}
break;
case 3: cout<<"The odd numbers from 1 to 10 are"<<endl;
for (int i=1;i<=10;i+=2)
{
cout<<i<<endl;
}
break;
case 4: cout<<"Enter a number to check even or odd."<<endl;
cin>>num;
if(num%2==0)
cout<<"Number is even"<<endl;
else
cout<<"Number is odd"<<endl;
break;
default: cout<<"Invalid input.Enter a number from 1 to 4."<<endl;
}
return 0;
}
Question 7:
*
* *
* * *
* * * *
* * * * *
#include <iostream>
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Question 8:
Display the following pattern by using for Loop.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <iostream>
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<j<<" ";
}
cout<<endl;
}
return 0;
}
Question 9:
* * * * *
* * * *
* * *
* *
*
#include <iostream>
using namespace std;
int main()
{
for (int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
cout<<"*"<<" ";
}
cout<<endl;
}
return 0;
}
Question 10:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <iostream>
int main()
for(int j=1;j<=5-i;j++)
{
cout<<" ";
for(int k=1;k<=2*i-1;k++)
cout<<"* ";
cout<<endl;
return 0;
}
Question 11:
Display the following pattern by using for Loop.
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include <iostream>
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=5-i;j++)
{
cout<<" ";
}
for(int k=i;k<=2*i-1;k++)
{
cout<<k<<" ";
}
for(int l=2*i-2;l>=i;l--)
{
std::cout <<l<<" ";
}
cout<<endl;
}
return 0;
}
Question 12:
Display the following pattern by using for Loop.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#include <iostream>
int main()
{
for (int i=1;i<=5;i++)
{
for(int j=1;j<=2*i-2;j++)
{
cout<<" ";
}
for(int k=1;k<=2*(5-i+1)-1;k++)
{
cout << "* ";
}
cout <<endl;
}
return 0;
}
Question 13:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#include <iostream>
int main() {
return 0;
}
Question 14:
Display the following pattern by using for Loop.
1
2 3
4 5 6
7 8 9 10
#include <iostream>
using namespace std;
int main()
{
int counter = 1;
for (int i = 1; i <= 4; ++i)
{
for (int j = 1; j <= i; ++j)
{
cout << counter << " ";
++counter;
}
cout << endl;
}
return 0;
}
Question 15:
Display the following pattern by using for Loop.
A
B B
C C C
D D D D
E E E E E
#include <iostream>
int main() {
char ch = 'A';
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= i; ++j) {
cout << ch << " ";
}
++ch;
cout << endl;
}
return 0;
}