Assignment 2
Assignment 2
Osama Siddiqui
Id : 6957
Assignment #2
Q.1 Write a program to ask to the user to enter the number (to find the factorial of the number
using the for loop and display the factorial result of the number on the output screen.
# include <iostream>
int main()
int i,n,factorial=1;
cin>>n;
for(i=1;i<=n;i++)
factorial*=i;
cout<<"Factorial of"<<n<<"="<<factorial<<endl;
system("pause");
return 0;
Q.2 Write a program that counts the number of words and the number of characters in a phrase
typed in by the user. Hint: an if...else statement embedded in a while loop.
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
char a[100];
int i,count=1;
cout<<"Enter a string:";
gets(a);
for(i=0;a[i]!='\0';++i)
if(a[i]==' ')
count++;
return 0;
Q.3 Write a source code in reference of array that invites the user to enter a series of six(6) values
representing widget sales for each day of the week (excluding Sunday), and then calculates the
average of these values.
# include <iostream>
using namespace std;
int main()
double sales[SIZE];
for(int j=0;j<SIZE;j++)
cin>>sales[j];
double total=0
for(int j=0;j<SIZE;j++)
total +=sales[j];
double average=total/SIZE;
cout<<"Average="<<average<<endl;
return 0;
}
Q.4 Write a program in reference of function, output given below that demonstrates a simple
function whose purpose is to print a line of 45 asterisks (*). The program should consists of two
functions: main ( ) and starline ( ). Write all other components which are necessary to add a
function to the program.
# include <iostream>
void starline();
int main()
starline();
starline();
return 0;
void starline()
cout<<"*";cout<<endl;
}
Q.5 Write a single C++ statement to output the following on the screen:
Department”
# include <iostream>
int main()
return 0;
Q.6 Demonstrates simple FOR loop that displays the squares of the numbers from 0 to 14
// fordemo.cpp
// demonstrates simple FOR loop
#include <iostream>
int main()
return 0;
Q.7 Write a program that takes input the radius of a circle. If this radius is greater than zero,
calculate the area of the circle. If the radius is less than or equal to zero, print an Error message that
area cannot be calculated for negative values
# include <iostream>
int main()
float radius,area;
cout<<"Enter radius:";
cin>>radius;
for(int i=0;i<=0;++i)
{
cout<<"radius is greater than zero:";
area=3.14*radius*radius;
cout<<area;
return 0;