0% found this document useful (0 votes)
10 views

PF Programs

Uploaded by

mi86822370
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PF Programs

Uploaded by

mi86822370
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

`Q-1. ALI’s basic salary is input through the keyboard.

His
dearness allowance is 40% of basic salary, and house rent
allowance is 20% of basic salary. Write a program to calculate his
gross salary.

PROGRAM:

#include<stdio.h>

using namespace std;

int main()

float BSal, GSal, HRent, DAllow;

printf(“Enter Basic Salary of Baloch = ");

scanf(“%f”,&BSal);

DAllow=BSal * .40;

HRent=BSal * .20;

GSal=BSal + DAllow + HRent;

Printf("\nGraoss Salary = %f",GSal);

return(0);

Q-2. The distance between two cities (in km.) is input through the
keyboard. Write a program to convert and print this distance in
meters, feet, inches and centimeters.

PROGRAM:

#include<stdio.h>

using namespace std;

int main()
{

float DKM,DM,DF,DI,DCM;

printf("Enter Distant between Two Cities in Kilometer = ");

scanf("%f",&DKM);

DM = DKM * 1000;

DF = DM * 3,28;

DI = DF * 12;

DCM= DI * 2.5;

system("CLS");

printf("\nDistance in Kilometer = %.2f",DKM);

printf("\nDistance in Meter = %.2f",DM);

printf("\nDistance in Feet = %.2f",DF);

printf("\nDistance in Inches = %.2f",DI);

printf("\nDistance in Centimeter = %.2f",DCM);

return(0);

Q-3. If the marks obtained by a student in five different subjects


are input through the keyboard, find out the aggregate marks and
percentage marks obtained by the student. Assume that the
maximum marks that can be obtained by a student in each subject
is 100.

#include<iostream>

using namespace std;

int main()

float S1,S2,S3,S4,S5,Obtained,Per;
printf("Enter Students Marks in Five Subjects = ");

scanf("%f %f %f %f %f",&S1,&S2,&S3,&S4,&S5);

Obtained=S1+S2+S3+S4+S5;

Per=Obtained/500*100;

system("CLS");

printf("\nObtained Marks = %.2f",Obtained);

printf("\nPercentage Marks = %.2f",Per);

return(0);

Q-4. Temperature of a city in Fahrenheit degrees is input through


the keyboard. Write a program to convert this temperature into
Centigrade degrees.

#include<stdio.h>
using namespace std;
int main()
{
clrscr();
float celsius,fahrenhiet;
printf("Enter Temperature in celsius=");
scanf("%f",&celsius);
//fahrenhiet=(9/5)*celsius+32;
fahrenhiet=(9*celsius)/5+32;
printf("\nTemperature In Fahrenheit is=%f",fahrenhiet);
return(0);
}

Q-5. The length & breadth of a rectangle and radius of a circle are
input through the keyboard. Write a program to calculate the area
& perimeter of the rectangle, and the area & circumference of the
circle.
#include <stdio.h>
using namespace std;
int main()
{ float L,B,R,RA,RP,CA,CC;
printf("Enter Length and Breadth of Rectangle and Radius of
Circle: ");
scanf("%f %f %f",&L,&B,&R);
RA=L*B;
RP=2*(L+B);
CA=3.14 * R * R;
CC=2 * 3.14 * R;
printf("Area of Rectangle = %.2f\n",RA);
printf("Perimeter of Rectangle = %.2f\n",RP);
printf("Area of Cicle = %.2f\n",CA);
printf("Circumference of Circle = %.2f\n",CC);
return(0);
}
Q-6. Two numbers are input through the keyboard into two
locations C and D. Write a program to interchange the contents of
C and D.

#include <stdio.h>
using namespace std;
int main()
{ int C,D,T;
printf("Enter two integer number:");
scanf("%d %d",&C,&D);
T=C;
C=D;
D=T;
printf("First New Number = %d\n",C);
printf("Second New Number = %d\n",D);
return(0);
}

Q-7. If a five-digit number is input through the keyboard,

(a) write a program to calculate the sum of its digits. (Hint: Use
the modulus operator ‘%’)

Q-7(a)
# include <stdio.h>
using namespace std;
int main()
{
long int N,S=0;
printf("Enter the five digit number=");
scanf("%ld",&N);
S=S+N%10;
N=N/10;
S=S+N%10;
N=N/10;
S=S+N%10;
N=N/10;
S=S+N%10;
N=N/10;
S=S+N;
printf("Sum of Digits of number = %ld\n\n",S);
return(0);
}
------------------------------------------- -----------------------------
Q-7 (b) If a five-digit number is input through the keyboard, write a
program to reverse the number.

Q-7(b)
# include <iostream>
using namespace std;
int main()
{ long int R=0,N;
printf("Enter the five digit number=");
scanf(“%ld”,&N);
R=N%10*10000;
N=N/10;
R=R+N%10*1000;
N=N/10;
R=R+N%10*100;
N=N/10;
R=R+N%10*10;
N=N/10;
R=R+N;
printf("Reverse number= %ld",R);
return(0);
}
Q-8. If a four-digit number is input through the keyboard, write a
program to obtain the sum of the first and last digit of this number.

# include <stdio.h>
using namespace std;
int main()
{
int N,S=0;
printf("Enter the four digit number=");
scanf("%d",&N);
S=S+N%10;
N=N/10;
N=N/10;
N=N/10;
S=S+N;
printf("Sum of first and last Digits= %d",S);
return(0);
}

Q-9. A cashier has currency notes of denominations 10, 50 and


100. If the amount to be withdrawn is input through the keyboard
in hundreds, find the total number of currency notes of each
denomination the cashier will have to give to the withdrawer.

#include<stdio.h>
using namespace std;
int main()
{ int Amount,H,F,T;
printf("Enter Amount to be drawn :");
scanf("%d",&Amount);
H=Amount/100;
Amount=Amount - H * 100;
F=Amount/50;
Amount=Amount - F * 50;
T=Amount/10;
printf("\nNumber of 100 Notes = %d",H);
printf("\nNumber of 50 Notes = %d",F);
printf("\nNumber of 10 Notes = %d",T);
return(0);
}
Q-10. If the total selling price of 15 items and the total profit
earned on them is input through the keyboard, write a program to
find the cost price of one item.

#include<stdio.h>
using namespace std;
int main()
{ float SP,Profit,CP,Item_CP;
printf("Enter Selling Price of 15 items :");
scanf("%f",&SP);
printf("Enter Profit Earned on 15 items :");
scanf("%f",&Profit);
CP=SP-Profit;
Item_CP=CP/15;
printf("\nCost Price of One Item = %.2f",Item_CP);
return(0);
}
==========================================================================

Q-11. Write a program, which will take two integer values as


inputs, then using if-else statement find the larger among them
and print it.

#include<stdio.h>
using namespace std;
int main()
{
int N1,N2,L;
printf("Enter Two Integer values please : ");
scanf("%d %d",&N1, &N2);
if(N1>N2)
L=N1;
else
L=N2;
printf("Larger Value = %d",L);
return(0);
}
Q-12. Write a program, which will accept F.Sc Marks of a
student as inputs, find the percentage marks and using
percentage marks find whether the students is PASS or FAIL

#include<stdio.h>
using namespace std;
int main()
{
float Obtain,Per;
printf("Enter Obtain Marks of F,Sc = ");
scanf("%f",&Obtain);
Per = Obtain/1100 *100;
if(Per >= 50)
printf(“Student is PASS\n”);
else
printf(“Student is FAIL\n”);
return(0);
}

Q-13. In a company an employee is paid as under:

If his basic salary is less than Rs. 1500, then HRA = 10% of basic
salary and DA = 90% of basic salary. If his salary is either equal to
or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic
salary. If the employee's salary is input through the keyboard write
a program to find his gross salary.

#include<stdio.h>
using namespace std;
int main()
{
float bs,gs,da,hra;
printf("Enter basic salary please : ");
scanf("%f",&bs);
if(bs<1500)
{
hra=bs*10/100;
da=bs*90/100;
}
else
{
hra=500;
da=bs*98/100;

}
gs=bs+hra+da;
printf("Gross Salary = Rs %f",gs);
return(0);
}

Q-14. The marks obtained by a student in 5 different subjects


are input through the keyboard. The student gets a division as per
the following rules: Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division Percentage
between 40 and 49 - Third division Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.

#include<stdio.h>
using namespace std;
int main()
{
int m1,m2,m3,m4,m5;
float per;
printf("Enter Marks of student in five subjects : ");
scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
per=(m1+m2+m3+m4+m5)/500.0 * 100;
if(per>=60)
printf("You Got First Division\n");
else
{
if(per>=50)
printf("You Got Second Division\n");
else
{
if(per>=40)
printf("You Got Third Division\n");
else
printf("You are Failed\n");
}

return(0);
}

Q-15. A company insures its drivers in the following cases: −


If the driver is married. − If the driver is unmarried, male & above
30 years of age. − If the driver is unmarried, female & above 25
years of age. In all other cases the driver is not insured. If the
marital status, sex and age of the driver are the inputs, write a
program to determine whether the driver is to be insured or not.

#include<stdio.h>
using namespace std;
int main()
{
char sex,ms;
int Age;
printf("Enter Age, Sex, marital status of driver : ");
scanf("%d %c %c",&Age,&sex,&ms);
if(ms == 'M')
printf("Driver is insured");
else
{
if(sex == 'M')
{
if (Age > 30)
printf("Driver is insured");
else
printf("Driver is not insured");
}
else
{
if(Age > 25)
printf("Driver is insured");
else
printf("Driver is not insured");
}
}
return(0);
}

USE of LOGICAL ‘&&’ Operator:

Q-16. The marks obtained by a student in 5 different subjects


are input through the keyboard. The student gets a division as per
the following rules: Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division Percentage
between 40 and 49 - Third division Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.

#include<stdio.h>
using namespace std;
int main()
{
int m1,m2,m3,m4,m5;
float per;
printf("Enter Marks of student in five subjects : ");
scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
per=(m1+m2+m3+m4+m5)/500.0 * 100;
if(per>=60)
printf("You Got First Division\n");
if(per>=50 && per<60)
printf("You Got Second Division\n");
if(per>=40 && per<50)
printf("You Got Third Division\n");
if(per<40)
printf("You are Failed\n");
return(0);
}

USE of LOGICAL ‘||’ Operator


Q-17. A company insures its drivers in the following cases: −
If the driver is married. − If the driver is unmarried, male & above
30 years of age. − If the driver is unmarried, female & above 25
years of age. In all other cases the driver is not insured. If the
marital status, sex and age of the driver are the inputs, write a
program to determine whether the driver is to be insured or not.

#include<stdio.h>
using namespace std;
int main()
{
char sex,ms;
int Age;
printf("Enter Age, Sex, marital status of driver : ");
scanf("%d %c %c",&Age,&sex,&ms);
if(ms == 'M' || ms == 'U' && sex == ‘M’ && Age >=30 || ms == 'U'
&& sex == ‘F’ && Age >=25)
printf("Driver is insured");
else
printf("Driver is not insured");
return(0);
}

How to put Comments is the program code to explain its coding:

Q-18. If cost price and selling price of an item is input through


the keyboard, write a program to determine whether the seller has
made profit or incurred loss. Also determine how much profit he
made or loss he incurred.

#include<stdio.h>
#include<conio.h>
int main()
{
float cost_price, selling_price, profit, loss;
printf("Enter the cost price of an item: ");
scanf("%f", &cost_price);
printf("Enter the selling price of an item: ");
scanf("%f", &selling_price);
if (selling_price > cost_price) //here we get profit on selling item
{
//calculate profit via selling price - cost price
profit = selling_price - cost_price;
printf("We earn %.2f profit by selling item.", profit);

else
if (selling_price < cost_price) //here we get loss on selling item
{
//calculate loss via cost price - selling price
loss = cost_price - selling_price;
printf("\nWe incurred %.2f loss on selling item.", loss);
}

else
//here we don't get any loss and profit on selling item when (cost price == selling price)
{
printf("\nWe don't get any loss and profit on selling item");
}
}

Q-19. Any integer is input through the keyboard. Write a


program to find out whether it is an odd number or even number.

#include<stdio.h>
using namespace std;
int main()
{ int N;
printf("Enter an Integer : ");
scanf("%d",&N);
if((N%2)==0)
printf("\n%d is an EVEN Integer", N);
else
printf("\n%d is an ODD Integer", N);
return(0);
}

======================================================================

Q-20. Write a program to find the factorial value of any


number entered through the keyboard.

#include<iostream>
using namespace std;
int main()
{
long int N,j,F;
cout<<"Enter the number=";
cin>>N;
F=1;
for(j=N;j>=1;j--)
F=F*j;
cout<<"factorial="<<F<<endl;
return(0);
}

Q-21. Two numbers are entered through the keyboard. Write


a program to find the value of one number raised to the power of
another.

#include<iostream>
using namespace std;
int main()
{
long int N,P,j,R;
cout<<"Enter the number and its power=";
cin>>N>>P;
R=1;
for(j=1;j<=P;j++)
R=R*N;
cout<<"Power Value="<<R<<endl;
return(0);
}

Q-22. Write a program to find the sum of first 10 natural


number using while loop.

#include<iostream>
using namespace std;
int main()
{
int j,N,S;
S=0;
cout<<"Enter the value of N =";
cin>>N;
for(j=1;j<=N;j++)
{
S=S+j;
cout<<j<<endl;
}
cout<<"=============\n"<<S;
return(0);
}

Q-23. Write a Program to find the following output using


nested for loop.

*
**
***
****
*****
******
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
return(0);
}

Q-24. Write a Program to find the following output using


nested for loop.

0
-1
--2
---3
----4
-----5
------6
-------7
--------8
---------9

#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=0;i<=9;i++)
{
for(j=1;j<=i;j++)
cout<<" ";
cout<<i<<endl;
}
return(0);
}
====================================================================

You might also like