Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Assignment

Name Muhammad ali

Id 12249

Subject introduction to programming

Teacher furqan nasir

1. Write a program to take input from user and show user whether entered
number is prime number or not.
Answer
Source code:-
{
int a;
cout<<"\t *prime number calculator*"<<endl;
cout<<"\n";
cout<<"enter the number ="<<endl;
cin>>a;
if(a%2==0||a%3==0)
cout<<"the given number is prime";
else
cout<<"the given number is not prime";
return 0;
}
3) Write a program to take two positive inputs from user and show prime
numbers between those numbers.
Answer
Source code:-
{
int a,b,c;
cout<<"\t *prime number calculator*"<<endl;
cout<<"\n";
cout<<"enter the number ="<<endl;
cin>>b;
cout<<"enter the second number=";
cin>>c;
for(a=b;a<=c;a++){
if(a%2==0||a%3==0)
{cout<<"";}
else
{cout<<a<<"\t";}
}
return 0;
}
4) Write a program that produce following result
1234
9 10 11 12
17 18 19 20
25 26 27 28
Answer

Source code:-
{
int a;
for(a=1;a<=28;a++)
{
if(a==5||a==6||a==7||a==8||a==13||a==14||a==15||a==16||
a==21||a= =22||a==23||a==24)
{cout<<"";}
else if(a==4||a==12||a==20)
{cout<<"\t"<<a<<endl;}
else
{cout<<"\t"<<a;
}
}
return 0;
}
Write a program that produce following result

111 112 113 114

115 116 117 118

119 120 121 122

123 124 125 126

Answer:-
Source code:-
{
int a;
for(a=111;a<=126;a++)
{
if(a==114||a==118||a==122)
{
cout<<"\t"<<a<<endl;
}
else if(a==115||a==123)
{
cout<<"\t\t"<<a;
}
else
{
cout<<"\t"<<a;
}
}
return 0;
}
7)Write a program that produce following result 6
67
678
6789
6 7 8 9 10
Answer
Source code:-
{
int a;
for(a=6;a<11;a++)
{
if(a==6)
{
cout<<a<<endl;
}
else if(a==7)
{
cout<<a-1<<"\t"<<a<<endl;
}
else if(a==8)
{
cout<<a-2<<"\t"<<a-1<<"\t"<<a<<endl;
}
else if(a==9)
{
cout<<a-3<<"\t"<<a-2<<"\t"<<a-1<<"\t"<<a<<endl;
}
else
{
cout<<a-4<<"\t"<<a-3<<"\t"<<a-2<<"\t"<<a-1<<"\t"<<a;
}
}
return 0;
}
7. Write a program that produce following result 6
77
888
9999
10 10 10 10 10
Answer

Source code:-

{
int a;
for(a=6;a<11;a++)
{
if(a==6)
{
cout<<a<<endl;
}
else if(a==7)
{
cout<<a<<"\t"<<a<<endl;
}
else if(a==8)
{
cout<<a<<"\t"<<a<<"\t"<<a<<endl;
}
else if(a==9)
{
cout<<a<<"\t"<<a<<"\t"<<a<<"\t"<<a<<endl;
}
else
{
cout<<a<<"\t"<<a<<"\t"<<a<<"\t"<<a<<"\t"<<a;
}
}
return 0;
}
8. Write a program that produce following result 10 10 10 10 10
9999
888
77
6
Answer
Source code:-
{
int a;
for(a=10;a>5;a--)
{
if(a==6)
{
cout<<a<<endl;
}
else if(a==7)
{
cout<<a<<"\t"<<a<<endl;
}
else if(a==8)
{
cout<<a<<"\t"<<a<<"\t"<<a<<endl;
}
else if(a==9)
{
cout<<a<<"\t"<<a<<"\t"<<a<<"\t"<<a<<endl;
}
else
{
cout<<a<<"\t"<<a<<"\t"<<a<<"\t"<<a<<"\t"<<a<<endl;
}
}
return 0;
}
9. Write a program that take one input from user and produce result in following
pattern. E.g user entered number=90
90
91 92
93 94 95
96 97 98 99
100 101 102 103 104
Answer
Source code:-
{
int a;
for(a=90;a<105;a++)
{
if(a==90)
{
cout<<a<<endl;
}
else if(a==92)
{
cout<<a<<endl;
}
else if(a==95)
{
cout<<a<<endl;
}
else if(a==99)
{
cout<<a<<endl;
}
else
cout<<a<<"\t";
}}

You might also like