Professional Documents
Culture Documents
Assignment 04
Assignment 04
Q. No. 1
Write a program to read a number N from user and find first N primes.
#include<iostream>
using namespace std;
int main()
{
//using for loop//
int num;
cout<<"Enter the number:\t";
cin>>num;
if(num>=1){
cout<<"1\n"<<"2"<<endl;
}
for(int i=2;i<=num;i++)
{
for(int j=2;j<num;j++)
{
if(i%j==0)
{
break;
}
else{
cout<<i<<endl;
break;
}
}
}
return 0;
}
Q. No. 2
Write a program to read your age in years in an integer type of variable. Namaz was not an
obligation on you in your first 7 years. Subtract 7 years from the age and after this subtraction,
display how many Prayers in total you had to pray.
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"enter your age";
cin>>age;
cout<<"your obligation age is\t"<<(age-7)<<endl;
cout<<"your prayers as per your age is\t"<<(age*1825)-12775;
return 0;
}
Q. No. 3
Write a program to display the following outputs using for, while and do-while.
5 54321 5 54321
54 5432 54 5432
543 543 543 543
5432 54 5432 54
54321 5 54321 5
Pattern 1
#include<iostream>
using namespace std;
int main()
{
cout<<"using while loop";
int i=0,num,n=1;
cout<<"Enter the number to start:\t";
cin>>num;
while (i<num){
int j=0;
while(j<n){
cout<<num-j;
j++;
}
cout<<endl;
i++;
++n;
}
cout<<"using for loop"<<endl;
i,num,n=1;
cout<<"Enter the number to start:\t";
cin>>num;
for(i=0;i<num;i++){
int j;
for(j=0;j<n;j++){
cout<<num-j;
}
cout<<endl;
++n;
}
cout<<"using do while loop"<<endl;
i=0,num,n=1;
cout<<"Enter the number to start:\t";
cin>>num;
do{
int j=0;
while(j<n){
cout<<num-j;
j++;
}
cout<<endl;
i++;
++n;
}while(i<num);
return 0;
}
Pattern 2
#include<iostream>
using namespace std;
int main()
return 0;
}
Pattern 3
#include<iostream>
using namespace std;
int main()
{
cout<<"using while loop"<<endl;
int i=0,num,n,k=1,j;
cout<<"Enter the number to start:\t";
cin>>num;
n=num-1;
while (i<num){
int j=0;
while(j<n){
cout<<" ";
j++;
}
j=0;
while(j<k){
cout<<num-j;
j++;
}
k++;
n--;
i++;
cout<<endl;
}
Pattern 4
#include<iostream>
using namespace std;
int main()
{
cout<<"using while loop"<<endl;
int i=0,num,n,k=0;
cout<<"Enter the number to start:\t";
cin>>num;
n=num;
while (i<num){
int j=0;
while(j<k){
cout<<" ";
j++;
}
j=0;
while(j<n){
cout<<num-j;
j++;
}
k++;
n--;
i++;
cout<<endl;
}
Q. No. 4
Write a program to print a multiplication table in the following pattern using nested loops (for,
while and do-while).
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
#include<iostream>
using namespace std;
int main()
{
cout<<"\nusing for loop"<<endl;
int i;
for(i=1;i<=6;i++){
int j;
for(j=1;j<=6;j++){
cout<<j*i<<"\t";
}
cout<<endl;
}
i=1;
do{
int j=1;
while (j<=6){
cout<<j*i<<"\t";
j++;
}
cout<<endl;
i++;
} while (i<=6);
return 0;
}
Q. No. 5
Display the sum of digits of a number. For example, number = 245, the sum is 11.
#include<iostream>
using namespace std;
int sum(int x)
{
int rem,sum=0;
do{
rem=x%10;
sum=sum+rem;
x=x/10;
}while(x>0);
return sum;
}
int main()
{
int num;
cout<<"Enter the number:\t";
cin>>num;
cout<<"Sum of number is:\t"<<sum(num);
return 0;
}