Sie sind auf Seite 1von 8

Name : Muhammad

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>

using namespace std;

int main()

int i,n,factorial=1;

cout<<"Enter a positive integer:";

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++;

cout<<"\nThere are "<<count<<" words in the given string";

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()

const int SIZE=6;

double sales[SIZE];

cout<<"Enter widget sales for 6 days\n";

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.

\The output of the program should be look like this


*********************************************

Data type Range


*********************************************
Char -128 to127
Short -32,768 to32,767
Int System dependent
Long -2,147,483,648 to2,147,483,647
*********************************************

# include <iostream>

using namespace std;

void starline();

int main()

starline();

cout<<"Data type range"<<endl;

cout<<"char -128 to 127"<<endl

<<"short -32,768 to 32,767"<<endl

<<"int system dependent"<<endl

<<"long -2,147,483,648 to 2,147,483,647"<<endl;

starline();

return 0;

void starline()

for(int j=0; j<45; j++)

cout<<"*";cout<<endl;

}
Q.5 Write a single C++ statement to output the following on the screen:

My name is “Your Name”

And my roll number is “00Your_roll_no”

I am a student of ‘Iqra University in Computer Science

Department”

# include <iostream>

using namespace std;

int main()

cout<<"My name is: Osama\t";

cout<<"My Roll No: 6957\t";

cout<<"I am a student of IQRA UNIVERSITY in Computer Science Department\t";

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>

using namespace std;

int main()

int j; //define a loop variable

for(j=0; j<15; j++) //loop from 0 to 14,

cout << j * j << " "; //displaying the square of j

cout << endl;

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>

using namespace std;

int main()

float radius,area;

cout<<"Enter radius:";

cin>>radius;

for(int i=0;i<=0;++i)

{
cout<<"radius is greater than zero:";

cout<<"area cannot be calculate for negative values:";

area=3.14*radius*radius;

cout<<area;

return 0;

Das könnte Ihnen auch gefallen