Saturday, 10 December 2016

Write a program that inputs a number from a user and displays its factorial. It asks the user whether he wants to calculate another factorial of not. It the user inputs 1, it again inputs number and calculates factorial. If User inputs 0, program terminates.

Source Code:

#include<iostream>
using namespace std;
int main ()
{
int n,x,fact=1;
do{
cout<<"Enter number to find Factorial: "<<endl;
cin>>n;
for (int i=1;i<=n;i++)
{
fact=fact*i;
  }
cout<<"Factorial of your numbers: "<<fact<<endl;
cout<<"Do you want to continue? \nIf 'Yes' then press 1 or If 'No' then press 0..."<<endl;
cin>>x;
fact=1;
} 
while(x==1);
}

Write a program that inputs a series of 20 numbers and displays the minimum value. (Using For Loop)

Source Code:

#include<iostream>
using namespace std;
int main ()
{
int a,x,sm=0;
cout<<"Enter a series of 20 numbers: "<<endl;
for(a=1;a<=20;a++)
{
cin>>x;
if(x<sm)
{
sm=x;
}
}
cout<<"Smallest number of the Series: "<<sm;
}

Write a program that will ask the user a question with four possible answers. The question should be asked 20. After all the input is gathered, the program should output the number of times each answer was selected.

Source Code:

#include<iostream>
using namespace std;
int main ()
{
int z=1,x,a=0,b=0,c=0,d=0;
cout<<"What is your name?\n1)Ali\n2)Bravo\n3)Kumar\n4)Umair"<<endl;
cout<<"Choose any answer by pressing only 1,2,3,4 (20 times): "<<endl;
while(z<=20)
{
cin>>x;
if(x==1){
a=a+1;
}
if(x==2){
b=b+1;
}
if(x==3){
c=c+1;
}
if(x==4){
d=d+1;
}
z++;
}
cout<<"Nubmer of times you selected above options: "<<endl;
cout<<"1) Ali "<<a<<" times."<<endl;
cout<<"2) Bravo "<<b<<" times."<<endl;
cout<<"3) Kumar "<<c<<" times."<<endl;
cout<<"4) Umair "<<d<<" times."<<endl;

}

Write a program to find largest, smallest and average of "n" whole numbers. You can assume that "n" has already been set by the user. Using While Loop. (C++)

Source Code:

#include<iostream>
using namespace std;
int main ()
{
int x,a=1,z,sum=0,lar,sm;
float avg;
cout<<"How many numbers you want to enter: ";
cin>>x;
cout<<"Enter Numbers: ";
while(a<=x){
cin>>z;
if(z>lar){
lar=z;
}
if(z<lar){
sm=z;
}
sum=sum+z;
a++;
}
avg=sum/x;
cout<<"Average of your numbers: "<<avg<<endl;
cout<<"Largest Number: "<<lar<<endl;
cout<<"Smallest Number: "<<sm<<endl;
}


Introduction

My name is Umair Mushtaq. I'm almost 19 years old and currently doing BS in Software Engineering from University of Gujrat, Sialkot Campus. I have created this blog to publish my programs on Internet, So that all the students of Programming can get help from it.
(Thanks)