Professional Documents
Culture Documents
Oops
Lab# 03
Assignment#03
R#35
Question No 1:
ASCII table
Make a program that writes a table of all characters with values from 32 to
255.
#include <iostream>
using namespace std;
int main()
{
int i;
void question2(){
float A_M,G_M,H_M;
float power=0.0,sum=0,product=1,sum_Reciprocal=0.0;
int size,i,min,max;
float num[size];
cout<<"Enter total numbers to be entered: ";
cin>>size;
cout<<"\nEnter Numbers: ";
for(i=0;i<size;i++){
cin>>num[i];
}
for(i=0;i<size;i++){
if(min>num[i]){
min=num[i];
}
}
max=num[0];
for(i=0;i<size;i++){
if(max<num[i]){
max=num[i];
}
}
for(i=0;i<size;i++){
sum=float(sum+num[i]);
product=float(product*num[i]);
}
for(i=0;i<size;i++){
sum_Reciprocal=float((sum_Reciprocal)+(1/num[i]));
}
power=(1/float(size));
cout<<"pow"<<power;
A_M=float(sum/size);
G_M=pow(product,power);
H_M=float(size/sum_Reciprocal);
min=num[0];
}
Output:
Question No 3: Write a program that continues to asks the user
to enter any number other than 5 until the user enters the number 5. Then
tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
/*
* question3.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
using namespace std;
void question3a()
{
int num;
for(int i=0;i<5;i++)
{
cout<<"\nenter any number other than 5 :";
cin>>num;
if(num==5)
{
break;
}
else if(num!=5)
{
continue;
}
}
cout<<"\nHey! you weren't supposed to enter 5!";
Output:
★ Modify the program so that after 10 iterations if the user still hasn't
entered 5 will tell the user "Wow, you're more patient then I am, you win."
and exit.
/*
* question3b.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<cstdlib>
using namespace std;
void question3b()
{
int input,a;
cout<<"enter any number other than 5:";
cin>>input;
if(input!=5)
{
for(a=0;a<10;a++)
{
cout<<"enter the next number:";
cin>>input;
if(input!=5)
{
if(a==9)
{
cout<<"you are very patient";
}
else
{
continue;
}
}
else
cout<<"you were not supposed to enter 5!";
break;
}
}
/*
* question3c.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include <iostream>
using namespace std;
#include "assignment3.h"
void question3c()
{
int i=-1,number;
cout<<"enter any other number than "<<i;
cin>>number;
if(number!=i)
{
for(i=0;i<=5;i++)
{
cout<<"enter any number other than "<<i;
cin>>number;
if(number==i)
{
break;
}
else if(i==5)
{
cout<<"good job";
}
else
continue;
}
Question No 4: Write a program that calculates a random
number 1 through 100. The program then asks the user to guess the
number. If the user guesses too high or too low then the program should
output "too high" or "too low" accordingly. The program must let the user
continue to guess until the user correctly guesses the number.
/*
* question4.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void question4()
{
int random_number,num;
random_number=rand()%100;
for(int i=0;;i++)
{
cout<<"\n enter your guess for random number :";
cin>>num ;
if(num==random_number)
{
cout<<"WOW!YOU WON :";
break;
}
else if(num<random_number)
{
cout<<"your guess is very low ";
}
else
{
cout<<"your guess is very high ";
}
}
}
★ Modify the program to output how many guesses it took the user to
correctly guess the right number.
/*
* question4.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void question4b()
{
int random_number,num,guess=0;
random_number=rand()%100;
for(int i=0;;i++)
{
cout<<"\n enter your guess for random number :";
cin>>num ;
if(num==random_number)
{
cout<<"WOW!YOU WON :";
guess++;
break;
}
else if(num<random_number)
{
cout<<"your guess is very low ";
guess++;
}
else
{
cout<<"your guess is very high ";
guess++;
}
}
★★ Modify the program so that instead of the user guessing a number the
computer came up with, the computer guesses the number that the user has
secretely decided. The user must tell the computer whether it guesed too
high or too low.
/*
* question4b.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
/*
* question4.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include <conio.h>
#include<stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int main()
{
int number, guess;
cout << "enter any number the computer will guess that number";
cin >> number;
char ch;
srand(time(NULL));
for (int i = 0;; i++)
{
else
{
break;
}
}
else
{
cout << "computer guess is very high\n ";
cout << "if you want to give computer another chance?\n";
ch = getchar();
if (ch == 'Y')
{
continue;
}
else
{
break;
}
}
}
_getche();
}
Question No 5: Write a program to enter the numbers till the
user wants and at the end it should display the count of positive, negative
and zeros entered.
/*
* question5.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include <conio.h>
using namespace std;
int main()
{
int number, positive = 0, negative = 0, zeros = 0;
char ch='Y';
cout << "enter as many numbers as you want";
cout << "you may press % for exit";
do {
{
zeros++;
}
ch = getchar();
} while (ch != '%');
cout << "the positive numbers are:" << positive << endl;
cout << "the negative numbers are:" << negative << endl;
cout << "number of zeros are:" << zeros - 1 << endl; //ZEROS-1
BECAUSE THE GET CHARACTER FUNCTION IS CAUSING INCREASE IN THE
VALUE OF ZEROS BECAUSE IT READS THE
//'\n' as character when we
enter % the value in the number is zero
_getche();
return 0;
}
Output:
I have used visual studio for debugging my program it was
showing problem in the calculation of zeros.
Question No 6: Write a program to print all prime numbers from
1 to 300.
/*
* question6.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include <iostream>
using namespace std;
void question6()
{
int i;
int j;
cout<<"the prime numbers are: ";
for( i=2;i<=300;i++)
{
for( j=2;j<=i;j++)
{
if(!(i%j)&&(i!=j))
{
break;
}
if(i==j)
cout<<i<<endl;
}
}
}
Question No 7: Write a program to add first seven terms of the
following series using loop:
/*
* question7.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
using namespace std;
void question7()
{
int i,fact =1;
float result =0.0;
int j;
for(i=1;i<=7;i++)
{
fact = 1;
for(j=1;j<=i;j++)
{
fact = fact*j;
}
result = result + (float(i)/float(fact));
}
cout<<"\n"<<result;
Output:
Question No 8: Write a program to calculate the factorial value of
any integer entered through the keyboard.
/*
* question8.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include <iostream>
using namespace std;
void question8()
{
int num;
unsigned int fact=1;
cout<<"\n enter any number to find its factorial :";
cin>>num;
for(int i=1;i<=num;i++)
{
fact =fact*i;
}
cout<<fact;
}
Question No 9: Write a program to obtain the first n numbers of
Fibonacci sequence. Where n will be taken from user. In a Fibonacci sequence
the sum of two successive terms gives the third term.
Example: 1 1 2 3 5 8 13 21 34 …
/*
* question9.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
void question9()
{
int n_term,i;
cout<<"Enter nth term of sequence: ";
cin>>n_term;
int feb[n_term];
feb[0]=feb[1]=1;
cout<<endl;
cout<<feb[0]<<" "<<feb[1]<<" ";
for(i=2;i<=n_term;i++)
{
feb[i]=feb[i-2]+feb[i-1];
cout<<feb[i]<<" ";
}
}
Output:
#include <iostream>
using namespace std;
void question10() //taken from internet
{
int rows,number=1,blank_space,i,j;
cout<<"Enter number of rows: ";
cin>>rows;
for(i=0;i<rows;i++)
{
for(blank_space=1;blank_space<=rows-i;blank_space++)
cout<<" ";
for(j=0;j<=i;j++)
{
if (j==0||i==0)
number=1;
else
number=number*(i-j+1)/j;
cout<<" "<<number;
}
cout<<endl;
}
}
Question No 11: Write a Program to find the binary equivalent of
a given decimal integer and display it.
/*
* question11.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include <iostream>
using namespace std;
void question11()
{
int number;
cout<<"enter any number in decimal please "<<endl;
cin>>number;
while(number>=1)
{
cout<<number%2;
number=number/2;
}
Output:
else
{
cout<<" ";
}
}
cout<<"\n";
Pattern#2:
/*
* question12.2.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern2()
{
for(int row=1;row<=15;row++)
{
for(int col=1;col<=15;col++)
{
if(row==1 || col==1 ||row==15 || col==15||row+col==16 ||row==col)
{
cout<<" *";
}
else
{
cout<<" ";
}
}
cout<<"\n";
Output:
Pattern #03
/*
* question12.3.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern3()
{
for(int col=1;col<=11;col++)
{
for(int row=1;row<=11;row++)
{
if(row>col)
{
cout<<" *";
}
else
{
cout<<"";
}
}
cout<<"\n";
Output:
Pattern #4
/*
* question12.8.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern4()
{
for(int row=1;row<=11;row++)
{
for(int col=1;col<=11;col++)
{
if(row+col==7||row+col==17||col-5==row||row-5==col)
{
cout<<" *";
}
else
{
cout<<" ";
}
}
cout<<"\n";
Output:
Pattern#5
/*
* question12.5.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern5()
{
for(int row=1;row<=11;row++)
{
for(int col=1;col<=11;col++)
{
if(row==col||col==1||col==11)
{
cout<<" *";
}
else
{
cout<<" ";
}
}
cout<<"\n";
Output:
Pattern#6
/*
* question12.6.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern6()
{
for(int row=1;row<=11;row++)
{
for(int col=1;col<=11;col++)
{
if(row==col||col==1||col==11||row+col==12)
{
cout<<" *";
}
else
{
cout<<" ";
}
}
cout<<"\n";
Output:
Pattern#07
/*
* question12.7.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern7(void)
{
for(int row=11;row>=1;row--)
{
for(int col=1;col<=11;col++)
{
if(row<=col)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<"\n";
Output:
Pattern#08
/*
* question12.4.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
void pattern8(){
int row,col;
for(row=1;row<=11;row++){
for(col=1;col<=11;col++){
if(row+5<col||row-5>col||row+col<7||row+col>17){
cout<<" ";
}
else{
cout<<"*"; }
}
cout<<endl;
}
Output:
Pattern#09
/*
* question12.9.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
void pattern9()
{
for(int row=1;row<=11;row++)
{
for(int col=1;col<=11;col++)
{
if(col==1||row==1||row==1||row==6||row==11)
{
cout<<" *";
}
else
{
cout<<" ";
}
}
cout<<"\n";
Output:
Pattern#10
/*
* question10.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include <iostream>
using namespace std;
void question10() //taken from internet
{
int rows,number=1,blank_space,i,j;
cout<<"Enter number of rows: ";
cin>>rows;
for(i=0;i<rows;i++)
{
for(blank_space=1;blank_space<=rows-i;blank_space++)
cout<<" ";
for(j=0;j<=i;j++)
{
if (j==0||i==0)
number=1;
else
number=number*(i-j+1)/j;
cout<<" "<<number;
}
cout<<endl;
}
}
Output:
Pattern#11
/*
* question12.11.cpp
*
* Created on: 14-Mar-2019
* Author: USAMA ABBASI
*/
#include<iostream>
void pattern11(){
int row,col;
for(row=1;row<=11;row++){
for(col=1;col<=11;col++){
if(row<=col){
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
}
Output:
Pattern#12
#include<iostream>
void pattern12(void){
int row,col;
for(row=1;row<=11;row++){
for(col=1;col<=11;col++){
if(row+4<col||row-4>col||row+col<8||row+col>16){
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
}
Output:
Header file:
/*
* assignment3.h
*
* Created on: 14-Feb-2019
* Author: USAMA ABBASI
*/
#ifndef ASSIGNMENT3_H_
#define ASSIGNMENT3_H_
void greater_no();
void classes();
void question2();
void question3a();
void question3b();
void question3c();
void question4a();
void question4b();
void question4c();
void question5();
void question6();
void question7();
void question8();
void question9();
void question10() ;
void question11();
void pattern1();
void pattern2();
void pattern3();
void pattern4();
void pattern5();
void pattern6();
void pattern7();
void pattern8();
void pattern9();
void pattern10();
void pattern11();
void pattern12();
#endif /* ASSIGNMENT3_H_ */
Main file:
/*
* main.cpp
*
* Created on: 14-Feb-2019
* Author: USAMA ABBASI
*/
#include <iostream>
using namespace std;
#include "assignment3.h"
int main()
{
question2();
question3a();
question3b();
question3c();
question4a();
question4b();
question4c();
question5();
question6();
question7();
question8();
question9();
question10() ;
question11();
pattern1();
pattern2();
pattern3();
pattern4();
pattern5();
pattern6();
pattern7();
pattern8();
pattern9();
pattern10();
pattern11();
pattern12();
return 0;
}