You are on page 1of 9

Name:Muhammad Sabih Salam

Roll # : CTAI-040

PF LAB 03
Write a C program that takes two integer values as input from the user. Then swap the
values taken from the user and display the output of the variables.

#include <stdio.h>

int main(void){
int a,b,c;
printf("\nEnter the values of a and b: ");
scanf("%d %d", &a, &b);
c = b;
b = a;
a = c;
printf("\nThe new value of a is %d and b is %d", a,b);

A customer asks the IT firm to develop a program in C language, which can take tax
rate and salary from the user on runtime and then calculate the tax, the user has to
pay and the salary he/she will have after paying the tax. This information is then
provided to the user.

#include <stdio.h>
int main(void){
int tax_rate;
float old_salary, new_salary;
printf("\nEnter the current percentage of tax rate: ");
scanf("%d", &tax_rate);
printf("\nEnter your current salary income in rupees: ");
scanf("%f", &old_salary);
new_salary = (old_salary) - ((old_salary*tax_rate)/100);
printf("\nYour new salary after paying the tax of %d percent will be %.2f
rupees.", tax_rate, new_salary);
}

A car traveled for some hours. The time car traveled is taken at run time of the
program, and it must not be negative and must be between one to five hours. The
car had not traveled same distance in each hour. The distance that the car covered
must not be negative. Write a C Program that computes the Average Speed of the
Car in miles per hour. Hint: the restrictions can be displayed in the form of message
on the window.

#include <stdio.h>
int main(void){
int time;
float d1, d2, d3, d4, d5, speed, distance = 0;
printf("\nEnter the time travelled by the car(between 1 and 5 hours):
");
scanf("%d", &time);
if(time < 0){
printf("\nInvalid. The time travelled cannot be negative.");
return 1;
}
if(time > 5){
printf("\nInvalid. The time travelled cannot be greater than
5.");
return 1;
}
if(time == 0){
printf("\nInvalid. The time travelled must be in between 1 and
5 hours.");
return 1;
}
if(time >= 1){
printf("\nEnter the distance travelled by the car in hour 1: ");
scanf("%f", &d1);
if(distance < 0){
printf("\nThe distance travelled by the car must not be
negative.");
return 1;
}
distance += d1;
}
if(time >= 2){
printf("\nEnter the distance travelled by the car in hour 2: ");
scanf("%f", &d2);
if(distance < 0){
printf("\nThe distance travelled by the car must not be
negative.");
return 1;
}
if(d2 == d1){
printf("\nThe car cannot travel the same distance in each
hour.");
return 1;
}
distance += d2;
}
if(time >= 3){
printf("\nEnter the distance travelled by the car in hour 3: ");
scanf("%f", &d3);
if(distance < 0){
printf("\nThe distance travelled by the car must not be
negative.");
return 1;
}
if(d3 == d2 || d3 == d1){
printf("\nThe car cannot travel the same distance in each
hour.");
return 1;
}
distance += d3;
}
if(time >= 4){
printf("\nEnter the distance travelled by the car in hour 4: ");
scanf("%f", &d4);
if(distance < 0){
printf("\nThe distance travelled by the car must not be
negative.");
return 1;
}
if(d4 == d3 || d4 == d2 || d4 == d1){
printf("\nThe car cannot travel the same distance in each
hour.");
return 1;
}
distance += d4;
}
if(time == 5){
printf("\nEnter the distance travelled by the car in hour 5: ");
scanf("%f", &d5);
if(distance < 0){
printf("\nThe distance travelled by the car must not be
negative.");
return 1;2
}
if(d5 == d4 || d5 == d3 || d5 == d2 || d5 == d1){
printf("\nThe car cannot travel the same distance in each
hour.");
return 1;
}
distance += d5;
}
speed = (distance/time);
printf("\nThe average speed of your car after travelling for %d hours
is %.2f miles/hour.", time, speed);

The "testInteger" short integer variable is being initialized here. In this instance, it is
discovered that the default range of the data type "short int" in C is approximately
between (-32,768 and 32,767). But in this case, the value we are assigning to the
short integer variable is completely outside of its acceptable range. For this reason,
rather than the value that should be printed, a garbage value is being displayed on
the output terminal.
Construct a C program with the flowchart below. The input value of the Principle
must be between 100 Rs. To 1,000,000 Rs. The Rate of interest must be between
5% to 10% and Time Period must be between 1 to 10 years. Hint: these
restrictions can be displayed in the form of message on the window.

#include <stdio.h>

int main(void){
double principle;
float rate_of_interest;
int time_period;
printf("\nEnter the principle value in rupees: ");
scanf("%lf", &principle);
if(!(principle >= 100 && principle <= 1000000)){
printf("\nInvalid principle value. It must be in the range of 100
and 1000000 rupees.");
return 1;
}
else{
printf("\nEnter the percentage of the rate of interest: ");
scanf("%f", &rate_of_interest);
if(!(rate_of_interest >= 5 && rate_of_interest <= 10)){
printf("\nInvalid. The rate of interest must be in the
range of 5 and 10 percent.");
return 1;
}
else{
printf("\nEnter the amount of time period in years: ");
scanf("%d", &time_period);
if(!(time_period >= 1 && time_period <= 10)){
printf("\nInvalid. The time period must be in the
range of 1 to 10 years.");
return 1;
}
else{
printf("\nEverything has been entered correctly.");
}
}
}
return 0;
}

Flowchart:

Begin

Variables principle,
rate_of_income, time_period

Read
principle

If(!(principle>=100
&&principle<=10000
00)) Write “invalid” A

Read
rate_of_income

If(! Write “invalid”


(rate_of_income B
C
>=5&&rate_of_in
come<=10))

Read
time_period

You might also like