Showing Page:
1/19
Islamabad campus
Name: Abdusamad roghani
Roll no: SEU-F18-157
Labtask:10apr
Subject:programing fundamental
Showing Page:
2/19
1: Write a program that finds out the sum of first 10 natural numbers using a while loop.
#include<iostream>
using namespace std;
int main()
{
int i, add=0;
cout<<"the sum of the natural number is "<<endl;
do
{
i=i+1;
cout<<"\t"<<endl<<i;
cout<<endl;
add=add+i;
}while (i<10);
cout<<"\n the sum of the natural number "<<"="<<add;
return 0;
Showing Page:
3/19
2: Write a program in C++ to find prime number within a range using a do while loop
#include<iostream>
using namespace std;
int main()
{
int n,i=2,j=0;
cout<<"enter any number =";
cin>>n;
cout<<endl;
cout<<n;
cout<<endl<<endl;
while(i<n)
{
if(n%i==0)
{
j=1;
break;
}
i=i+1;
}
if(j==0)
{
Showing Page:
4/19
cout<<"the number is prime ";
}
else
{
cout<<"the number is not prime ";
}
return 0;
}
Showing Page:
5/19
3: Write a program to find the factorial of a number using a while loop
#include<iostream>
using namespace std;
int main()
{
int n;
int fact=1;
cout<<"please enter any number =";
cin>>n;
cout<<endl<<endl;
while(n>=1)
{
fact=fact*n;
n=n-1;
}
cout<<"the factorial of the number is "<<endl;
cout<<fact;
}
Showing Page:
6/19
4: Write a program to find the Greatest Common Divisor (GCD) of two numbers using a for loop.
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input
loop */
int main()
{
int m,n;
cout<<"please the first number =";
cin>>m;
cout<<endl;
cout<<"please the second number =";
cin>>n;
cout<<endl;
int mn=min (m,n);
int gcd=1;
Showing Page:
7/19
for(int i=1;i<=mn;i++)
{
if(m%i==0&&n%i==0)
{
gcd=max (gcd,i);
i=i+1;
}
}
cout<<"\n the greatest common divisor is "<<gcd;
return 0;
}
Showing Page:
8/19
5: Write a program to find the sum of digits of a given number using a while loop
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input
loop */
int main()
{
int n,r,sum=0;
cout<<"enter the number =";
cin>>n;
cout<<endl<<n;
cout<<endl<<endl;
while (n>0)
{
Showing Page:
9/19
r=n%10;
sum=sum+r;
n=n/10;
}
cout<<"sum of the digits =";
cout<<sum;
return 0;
}
Showing Page:
10/19
6: Write a program to display the cube of the given integer
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main()
{
int num,cub;
cout<<"please enter the number for taking cube =";
cin>>num;
cub=num*num*num;
cout<<endl<<endl;
cout<<"the cube of this number is =";
cout<<cub;
return 0;
}
Showing Page:
11/19
7: Write a program in C++ to display the multiplication table of the given number up to a given range of
20 multiples using a while loop.
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main()
{
int t,r;
int c=1;
cout<<"please enter any number to find a table ";
cin>>t;
while(c<=20)
{
r=t*c;
Showing Page:
12/19
cout<<t<<"*"<<c<<"="<<r<<endl;
c++;
}
return 0;
}
Showing Page:
13/19