University of Sialkot,Pakistan
Assignment no. 2
Subject: PROGRAMMING
FUNDAMENTALS LAB
NAME: JOVARIYA GULZAR
ROLL NUMBER: 054
DATE: 10-2-25
DEPARTMENT: BSCS GREY
SEMESTER: 1
1- Write a program that displays “Pakistan” for five times using while loop.
#include <iostream>
using namespace std;
int main()
{
int i=1;
while(i<=5)
{
cout<<"Pakistan"<<endl;
i++;
}
return 0;
}
Result:
2- Write a program that displays counting from 1 to 10 using while loop.
#include <iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10)
{
cout<<i<<endl;
i++;
}
return 0;
}
Result:
3- Write a program that displays first five numbers and their sums using
while loop.
#include <iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while(i<=5)
{
cout<<i<<endl;
sum=sum+i;
i++;
}
cout<<"Sum = "<<sum;
return 0;
}
Result:
4- Write a program that displays first five numbers with their squares using
while loop.
#include <iostream>
using namespace std;
int main()
{
int i=1;
while(i<=5)
{
cout<<i<<" "<<i*i<<endl;
i++;
}
return 0;
}
Result:
5-Write a program that inputs a number from the user and displays a table of
that number using while loop.
#include <iostream>
using namespace std;
int main()
{
int i=1,j;
cout<<"Enter a number: ";
cin>>j;
while(i<=10)
{
cout<<j<<" * "<<i<<" = "<<j*i<<endl;
i++;
}
return 0;
}
Result:
6- Write a program that inputs a number from the user and displays the sum
of its digits using while loop.
#include <iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"Enter a number: ";
cin>>n;
while(n!=0)
{
sum=sum+n%10;
n=n/10;
}
cout<<"Sum of digits = "<<sum<<endl;
return 0;
}
Result:
7-Write a program that inputs a number from the user and displays the
factorial of that number using while loop.
#include <iostream>
using namespace std;
int main()
{
int f=1,c=1,n;
cout<<"Enter a number: ";
cin>>n;
while(c<=n)
{
f=f*c;
c++;
}
cout<<"Factorial of n = "<<f<<endl;
return 0;
}
Result:
8-Write a program that displays degree to radian table using while loop.
#include <iostream>
using namespace std;
int main()
{
float radian,degree=0,PI=3.14;
cout<<"Degrees to Radian: "<<endl;
cout<<"*******************"<<endl;
while(degree<=360)
{
radian=degree*PI/180;
degree=degree+10;
cout<<" "<<degree<<" "<<radian<<endl;
}
return 0;
}
Result:
9-Write a program that displays the sum of the following series using while
loop. 1+1/2+1/4+1/6+1…...+1/100
#include <iostream>
using namespace std;
int main()
{
float c=2.0,r=1.0;
while(c<=100)
{
r=r+1.0/c;
c=c+2;
}
cout<<"Sum of the series= "<<r<<endl;
return 0;
}
Result:
10-Write a program that inputs a number from the user and displays the sum
of all odd numbers and even numbers using while loop.
#include <iostream>
using namespace std;
int main()
{
int n,evensum=0,oddsum=0;
cout<<"Enter a number: ";
cin>>n;
while(n>=0)
{
if(n%2==0)
evensum=evensum+n;
else
oddsum=oddsum+n;
n--;
}
cout<<"Evensum= "<<evensum<<endl;
cout<<"Oddsum= "<<oddsum<<endl;
return 0;
}
Result:
11-Write a program that inputs a number from the user and checks whether
the number is Armstrong or not using while loop.
#include <iostream>
using namespace std;
int main()
{
int num,n,sum=0,r;
cout<<"Enter a number: ";
cin>>num;
n=num;
while(n!=0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==num)
cout<<"It is an Armstrong number"<<endl;
else
cout<<"It is not an Armstrong number"<<endl;
return 0;
}
Result:
12-Write a program that inputs a number until the user enters a negative
number. The program calculates the average, max and min of all positive
numbers using while loop.
#include <iostream>
using namespace std;
int main()
{
int n,sum=0,max,min,avg,count=0;
cout<<"Enter a Positive number: ";
cin>>n;
max=n;
min=n;
while(n>=0)
{
sum=sum+n;
count++;
if(n>max)
max=n;
else if(n<min)
min=n;
cout<<"Enter a Positive number: ";
cin>>n;
}
avg=sum/count;
cout<<"Average= "<<avg<<endl;
cout<<"Maximum= "<<max<<endl;
cout<<"Minimum= "<<min<<endl;
return 0;
}
Result:
13-Write a program that inputs a number from the user and counts the
number of words and characters in the sentence using while loop.
#include <iostream>
using namespace std;
int main()
{
string sentence;
int charcount=0, wordcount=1,i=0;
cout<<"Enter a sentence: ";
getline(cin,sentence);
while(sentence[i] != '\0')
{
if(sentence[i] !=' ')
charcount++;
else if (sentence[i] == ' ')
wordcount++;
i++;
}
cout<<"Words= "<<wordcount<<endl;
cout<<"Character= "<<charcount<<endl;
return 0;
}
Result:
14-Write a program that inputs starting and ending number from the user
and displays all even numbers in the given range using while loop.
#include <iostream>
using namespace std;
int main()
{
int n,s,e;
cout<<"Enter starting number: ";
cin>>s;
cout<<"Enter ending number: ";
cin>>e;
n=s;
while(n<=e)
{
if(n%2==0)
cout<<n<<endl;
n++;
}
return 0;
}
Result:
15-Write a program that inputs the number from the user using while loop.
The loop is terminated when the user enters -1.
#include <iostream>
using namespace std;
int main()
{
int n=1;
while(n!=-1)
{
cout<<"Enter a number: ";
cin>>n;
}
cout<<"End of Program! ";
return 0;
}
Result:
16-Write a program that inputs the number from the user and displays n
Fibonacci terms using while loop.
#include <iostream>
using namespace std;
int main()
{
int a=0,b=1,n,next,count=2;
cout<<"Enter a number: ";
cin>>n;
cout<<a<<"\t"<<b;
while(count<n)
{
next=a+b;
cout<<"\t"<<next;
count++;
a=b;
b=next;
}
return 0;
}
Result:
17-Write a program that inputs the number from the user and checks if it is a
Fibonacci number or not using while loop.
#include <iostream>
using namespace std;
int main()
{
int a=0,b=1,n,next;
cout<<"Enter a number: ";
cin>>n;
if(n==0 || n==1)
cout<<"It is a Fibonacci number ";
else
next=a+b;
while(next<n)
{
next=a+b;
a=b;
b=next;
}
if(next==n)
cout<<"It is a Fibonacci number";
else
cout<<"It is not a Fibonacci number ";
return 0;
}
Result:
18- Write a program that repeatedly inputs a number from the user and
prints its square. The loop terminates if the user enters 0 using while loop.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<" Enter a number: ";
cin>>n;
while(n!=0)
{
cout<<"Square of "<<n<<" = "<<n*n<<endl;
cout<<"Enter a number: ";
cin>>n;
}
cout<<"Good Bye!"<<endl;
return 0;
}
Result:
19-Write a program that displays the sum of the following series using while
loop. 13+23+33….503
#include <iostream>
using namespace std;
int main()
{
int n=1,sum=0;
while(n<=50)
{
sum=sum+(n*n*n);
n++;
}
cout<<"Sum = "<<sum;
return 0;
}
Result:
20- Write a program that displays back counting from 10 to 1 using while
loop.
#include <iostream>
using namespace std;
int main()
{
int n=10;
while(n>=1)
{
cout<<n<<endl;
n--;
}
return 0;
}
Result:
21-Write a program that gets two numbers from the user and the result of
number raise to the power of second number using while loop.
#include <iostream>
using namespace std;
int main()
{
int a,b,c=1,r=1;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
do
{
r=r*a;
c=c+1;
}
while(c<=b);
cout<<"Result= "<<r;
return 0;
}
Result:
22- Write a program that gets two numbers from the user and displays their
product. It then asks the user to continue or not. The program inputs two
numbers again if the user enters ‘x’ and exists if the user enter ‘n’ using while
loop.
#include <iostream>
using namespace std;
int main()
{
int a,b;
char c;
do
{
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"Product of the numbers= "<<a*b<<endl;
cout<<"Do you want to continue (x/n)= ";
cin>>c;
}
while(c!='n');
cout<<"Invalid Entery!";
return 0;
}
Result: