You are on page 1of 15

FINAL EXAM

Q1: Write a program to copy string to another without using buil in function.

Solution:

#include<stdio.h>
#include<string.h>
int main()
{
char original[]="This is to concatenate example";
char copystr[50];
int length,i;
length=strlen(original);
for( i=0;i<length;i++)
{
copystr[i]=original[i];
}
copystr[i]='\0';
printf("String copied = %s",copystr);
}

Q2: Write a program to convert of every word from lowercase to uppercase the string should be entered by
user.

Solution:

#include <iostream>

#include <string>

using namespace std;

string Capitalize_first_letter(string t) {

for (int a = 0; a < t.length(); a++)

{if (a == 0)

t[a] = toupper(t[a]);

else if (t[a - 1] == ' ')


{t[a] = toupper(t[a]);

}}

return t;

int main()

cout << Capitalize_first_letter("My name is adeel")<<endl;

cout << Capitalize_first_letter("string ");

return 0;

Q3.

Suppose you are working as a programmer in an IT firm. Your firm got a project to develop an
application for an Ice Cream Parlor. The application must be developed considering the following
scenario.

 The application will display a menu showing the flavors that have been offered by the Ice Cream
Parlor.

 The Parlor offers Tutti-Frutti, Chocolate, Pineapple, Kulfa, Strawberry, Mango, Orange, and
Pistachio flavors.

 The price of each flavor per scoop has been listed in the table below, for multiple scoops, the
price will variate as per the scenario stated in the table.

Three Scoops
Flavor One Scoop Two Scoops
or above
Tutti-Frutti 150 120 100
Chocolate 150 120 100
Pine Apple 150 120 100
Kulfa 150 120 100
Strawberry 150 120 100
Mango 175 150 130
Orange 200 170 150
Pistachio 150 120 100

Solution:
#include <iostream>

using namespace std;

int main()

string f;

int scoops;

cout<<"We have following flavours here\n";

cout<<"Tutti Frutti"<<" "<<"Chocolate"<<endl;

cout<<"Pineapple"<<" "<<"Kulfa"<<endl;

cout<<"Strawberry"<<" "<<"Mango"<<endl;

cout<<"Orange"<<" "<<"Pistachio"<<endl;

cout<<"which flavour do u want?"<<endl;

cin>>f;

if(f=="Choclate"||f=="choclate ")

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 150"<<endl;

cout<<"The bill is RS "<<scoops*150<<endl;

if(scoops==2)

cout<<"Price of each scoops is 120"<<endl;

cout<<"The bill is RS "<<scoops*120<<endl;

if(scoops>=3)

{
cout<<"The price of each scoop is 100"<<endl;

cout<<"The total bill is RS "<<scoops*100<<endl;

if(f=="Tutti-Frutti"||f=="tutti-frutti")

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 150"<<endl;

cout<<"bill is "<<scoops*150<<endl;

if(scoops==2)

cout<<"Price of each scoops is 120"<<endl;

cout<<" bill is RS "<<scoops*120<<endl;

if(scoops>=3)

cout<<" price of each scoop is 100"<<endl;

cout<<" total bill is RS "<<scoops*100<<endl;

if(f=="Strawberry"||f=="strawberry")

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)
{

cout<<"Price of single scoop is 150"<<endl;

cout<<"bill is RS "<<scoops*150<<endl;

if(scoops==2)

cout<<"Price of each scoops is 120"<<endl;

cout<<"bill is RS "<<scoops*120<<endl;

if(scoops>=3)

cout<<"The price of each scoop is 100"<<endl;

cout<<" bill is RS "<<scoops*100<<endl;

if(f=="Kulfa"||f=="kulfa")

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 150"<<endl;

cout<<"bill is RS "<<scoops*150<<endl;

if(scoops==2)

cout<<"Price of each scoops is 120"<<endl;

cout<<"bill is RS "<<scoops*120<<endl;

}
if(scoops>=3)

cout<<"The price of each scoop is 100"<<endl;

cout<<"Ttotal bill is RS "<<scoops*100<<endl;

if(f=="Pineapple"||f=="pineapple")

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 150"<<endl;

cout<<"bill is RS "<<scoops*150<<endl;

if(scoops==2)

cout<<"Price of each scoops is 120"<<endl;

cout<<"bill is RS "<<scoops*120<<endl;

if(scoops>=3)

cout<<"The price of each scoop is 100"<<endl;

cout<<"bill is RS "<<scoops*100<<endl;

if(f=="Pistachio"||f=="pistachio")

cout<<"How many scoops do u want? "<<endl;


cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 150"<<endl;

cout<<"bill is RS "<<scoops*150<<endl;

if(scoops==2)

cout<<"Price of each scoops is 120"<<endl;

cout<<"bill is RS "<<scoops*120<<endl;

if(scoops>=3)

cout<<"The price of each scoop is 100"<<endl;

cout<<"total bill is RS "<<scoops*100<<endl;

if(f=="Mango"||f=="mango") // mango

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 175"<<endl;

cout<<"bill is RS "<<scoops*175<<endl;

if(scoops==2)

cout<<"Price of each scoops is 150"<<endl;


cout<<"bill is RS "<<scoops*150<<endl;

if(scoops>=3)

cout<<"The price of each scoop is 130"<<endl;

cout<<"total bill is RS "<<scoops*130<<endl;

if(f=="Orange"||f=="orange") //orange

cout<<"How many scoops do u want? "<<endl;

cin>>scoops;

if(scoops==1)

cout<<"Price of single scoop is 200"<<endl;

cout<<"bill is RS "<<scoops*200<<endl;

if(scoops==2)

cout<<"Price of each scoops is 170"<<endl;

cout<<"bill is RS "<<scoops*170<<endl;

if(scoops>=3)

cout<<"The price of each scoop is 150"<<endl;

cout<<"total bill is RS "<<scoops*150<<endl;

return 0;
}

Q4: Write a C++ Program that change first letter of every word from lowercase to uppercase, the string
should be entered by the user.

SOLUTION:

#include <iostream>

using namespace std;

int main()

int a=1 , b=4, c;

cout<<"Before swap :";

cout<<"\n"<<"a= "<<a<<" b= "<<b<<endl;

c=a;

a=b;

b=c;

cout<<"After Swap ";

cout<<"\n"<<"a= "<<a<<" b="<<b<<endl;

return 0;

Q5: Write a program in C++ that takes a string from user and your program will count number of words of it
also count the total character in a string.

Solution:

#include<iostream>

#include<stdio.h>
using namespace std;

int main()

char arr[100];

int i,c=1;

cout<<"Enter a string:";

cin>>(arr);

for(i=0;arr[i]!='\0';++i)

if(arr[i]==' ')

c++;

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

return 0;}

Q6: A car with a 20-gallon gas tank average 21.5 miles per gallon when drive in town and 26.8 miles per
gallon when driven on the highway. Write a program that calculates and displays the distance the car can
travel on one tank of gas when driven in town and when driven on the highway.

Solution:

#include <stdio.h>

#include <math.h>

int main()

{double miles,d;//d=distance

int gallons;

printf(" When car drive in town \n");

miles=21.5;

gallons=20;

d= miles*gallons ;

printf(" Distance is %f",d);


printf("\n");

printf(" When car driven in highway \n");

miles=26.8;

gallons=20;

d= miles*gallons ;

printf(" Distance is %f",d);

return 0;

Q7; Write a program that input electricity units consumed and calculate bill according to following
conditions if the units are less than 100 the unit price is Rs. 5 and if the units are more than 100 and less
than 250 then first 100 are charged for Rs. 5 but the rest of the units are charged at Rs 10. If the units are
more than 250 then the first 100 units are charged at Rs. 5 and the rest of the units up to 250 will be
charged at Rs. 10 and the rest units are charged at Rs. 17 and then calculate the total bill for all units.

Solution:

#include <iostream>

using namespace std;

int main()

int units;

cout<<"enter electricity consumed ";

cin>>units;

int Rs;

if(units<100)

Rs=units*5;

}else if(units>100 && units<250)


{

int unit, a,b;

unit=units-100;

a=100*5;

b=unit*10;

Rs=a+b;

}else if(units>250)

int unit,a,b;

int c,d,e;

unit=units-250;

a=unit-100;

c=100*5;

d=250*10;

e=a*17;

Rs=c+d+e;

}return 0;

Q9.Write a program in C++ that take the array elements from the user and search the specific element in
the array. The specific element should be entered from the user which is to be searched. The size of the
array should be 5.

#include<iostream>

using namespace std;

const int size=50;


int main()

int a[size],b,i,c,j,e,f;

cout<<"How many array you want : ";

cin>>b;

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

cout<<"Enter the "<<i<<" index element :";

cin>>a[i];

cout<<"Enter the number you want to find: ";

cin>>c;

for(j=0;j<b;j++)

e=a[j];

if(e==c)

f=j;

cout<<"Enter the number is at "<<f+1<<" position";

return 0;

Q10. Write a program that will find the roots of quadratic equation if the roots of equation are imaginary
your program will also display those roots.

#include <math.h>
#include <stdio.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imagPart;

printf("Enter coefficients a, b and c: ");

scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

// condition for real and equal roots

else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("root1 = root2 = %.2lf;", root1);

// if roots are not real

else {

realPart = -b / (2 * a);

imagPart = sqrt(-discriminant) / (2 * a);

printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);

}
return 0;

You might also like