You are on page 1of 18

Question 1:

Modify the upc.c program given in section 4.1 of your textbook so that it
checks whether a UPC is valid. After the user enters a UPC, the program will display
either VALID or NOT VALID.

Code:
//computes the check digit of a UPC code and checks whether a UPC code is VALID or
INVALID

#include <stdio.h>

int main(void)
{
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total,
computed_check_digit, userEntered_check_digit;

printf("Enter the first (single) digit: ");

scanf_s("%1d", &d); //asks the user to enter first digit which is stored in
'd'

printf("Enter first group of five digits: ");

scanf_s("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); //asks the user to enter
first group of 5 digits in a UPC code

printf("Enter second group of five digits: ");

scanf_s("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); //asks the user to enter
second group of 5 digits in a UPC code

printf("Enter your check digit: ");

scanf_s("%d", &userEntered_check_digit); //asks the user to enter check digit

first_sum = d + i2 + i4 + j1 + j3 + j5; //first group is the sum of


d, i2, i4, j1, j3 and j5

second_sum = i1 + i3 + i5 + j2 + j4; //second group is the sum of


i1, i3, i5, j2 and j4

total = 3 * first_sum + second_sum; //total is equal to 3 times the


sum of first sum and second sum

computed_check_digit = 9 - ((total - 1) % 10);

printf("Check digit is %d\n", computed_check_digit); //prints the computed check


digit

if (computed_check_digit == userEntered_check_digit)
/*a UPC is valid if the computed check digit
is equal to the one entered by the user*/
printf("VALID\n");

else
printf("INVALID\n");
return 0;

Output:
Case 1:
When check digit entered by the user is not equal to the computed check
digit, the UPC code is not valid.

Case 2:
When check digit entered by the user is equal to the computed check
digit, the UPC code is valid.
Question 2:
Using Nested If statements, write a program that finds the largest and
smallest of four integers entered by the user.

Code:
//prints the largest and smallest of 4 distinct numbers

#include<stdio.h>

int main()
{
int a, b, c, d; //initializing variables
int max=0;
int min=0;
printf("Enter the value of a: ");

scanf_s("%d", &a); //asks the user to enter the value of a

printf("Enter the value of b: ");

scanf_s("%d", &b); //asks the user to enter the value of b

printf("Enter the value of c: ");

scanf_s("%d", &c); //asks the user to enter the value of c

printf("Enter the value of d: ");


scanf_s("%d", &d); //asks the user to enter the value of d

//nested if to check the largest of 4 numbers


if (a == b && a == c && a == d) {
printf("Please enter valid numbers.");
}
else {
if (a > b) {

if (a > c) {

if (a > d)

max = a; //stores 'a' in 'max'


(variable for largest number)

else max = d; //if 'a' is not greater than 'd',


it means 'd' is the largest number
}

//in case 'd' is greater than 'a', check whether d is greater than
the rest of integers or not

else {

if (c > d) //compares 'c' and 'd'

max = c;

else max = d;
}

}
else
{
if (b > c) { //compares 'b' , 'c' and 'd'
if (b > d)
max = b;
else max = d;
}

}
//nested if for checking the smallest of four numbers

if (a < b) {

if (a < c) {

if (a < d)

min = a; //stores 'a' in 'min'(variable for


smallest number)

else min = d; //if 'a' is not smaller than 'd', it means 'd'
is the smallest number
}
//in case 'd' is smaller than 'a', check whether d is smaller than
the rest of integers or not

else {

if (c < d) //compares 'c' and 'd'

min = c;

else min = d;
}
}
else {

if (b < c) { //compares 'b' , 'c' and 'd'

if (b < d)

min = b;

else min = d;
}
else {

if (c < d)

min = c;

else min = d;
}

printf("The largest number is: %d\n", max); //prints the largest number

printf("The smallest number is: %d", min); //prints the smallest number
}
return 0;
}

Output:
Case 1:
When user enters different numbers.
Case 2:
When all numbers entered by number are same.

Question 3:
The simple interest on a loan is calculated by the following formula:
Principal∗Rate∗Days
Interest=
365
In year 2020, interest rate in Pakistan varied and is given in the table below. Write a program
that takes principal (amount borrowed), month of the year (numeric value) and days for several
loans as inputs, calculate and displays the simple interest for each loan.
Month Rate
June 7%
May 8%
April 9%
March 11%
Any other month 12.5%
Code:
//prints and calculates the interest
#include<stdio.h>

int main()

int principal, month, days; /*initializing


variables*/
float rate = 0;
float interest;

printf("Enter principal (amount borrowed): "); //prompt

scanf_s("%d", &principal); //asks the user to enter principal (amount borrowed)

printf("Enter days: ");

scanf_s("%d", &days); //asks the user to enter the number of days

printf("Enter month of the year: ");

scanf_s("%d", &month); //asks the user to enter the month number, NOT THE
NAME

switch (month) { //switch statement whose controlling expression is the month


number

case 6: //for JUNE

rate = 0.07; //Rate is 7% for June. 7% is 7 divided by 100 which is


equal to 0.07

interest = (principal * rate * days) / 365; //calculates interest

printf("Interest is %f", interest); //prints interest

break;
case 5: //for MAY

rate = 0.08; //Rate is 8% for May. 8% is 8 divided by 100 which is equal


to 0.08

interest = (principal * rate * days) / 365; //calculates interest

printf("Interest is %f", interest); //prints interest

break;

case 4: //for APRIL

rate = 0.09; //Rate is 9% for April. 9% is 9 divided by 100 which is


equal to 0.09

interest = (principal * rate * days) / 365; //calculates interest

printf("Interest is %f", interest); //prints interest

break;

case 3: //for MARCH

rate = 0.11; //Rate is 11% for March. 11% is 11 divided by 100 which
is equal to 0.11

interest = (principal * rate * days) / 365; //calculates interest

printf("Interest is %f", interest); //prints interest

break;

default: //for any other month of year other than the above mentioned months

rate = 0.125; //Rate is 12.5% for all other months except the above
mentioned. 12.5% is equal to 12.5 divided by 100 which is equal to 0.125

interest = (principal * rate * days) / 365; //calculates interest

printf("Interest is %f", interest); //prints interest

break;
}
return 0;

Output:
For June:
For May:

For April:
For March:

For other months: (In this case, September)


Question 4:
Modify the following code to produce the output shown. Use proper indentation techniques. You
may not make any changes other than inserting braces.
if ( y == 8 )
if ( x == 5 )
printf( "@@@@@" );
else
printf( "#####" );
printf( "$$$$$" );
printf( "&&&&&" );

a) Assuming x = 5 and y = 8, the following output is produced.

if (y == 8)

if (x == 5)
printf("@@@@@\n");

}
else printf("#####\n");

printf("$$$$$\n");

printf("&&&&&\n");

b) Assuming x = 5 and y = 8, the following output is produced

if (y == 8)

if (x == 5)

printf("@@@@@\n");

}
else {
printf("#####\n");

printf("$$$$$\n");

printf("&&&&&\n");

}
c) Assuming x = 5 and y = 8, the following output is produced

if (y == 8)

if (x == 5)

printf("@@@@@\n");

}
else {
printf("#####\n");

printf("$$$$$\n");
}

printf("&&&&&\n");
Question 5:
The following table shows the postcode of different cities of Pakistan. Write a switch statement
whose controlling expression is the postcode. If the value of postcode is in the table, the switch
statement will print the corresponding city name. Otherwise, the switch statement will display
the message “Postcode is not valid”

Postcode City
74800 Karachi
54000 Lahore
44000 Islamabad
25000 Peshawar
87300 Quetta

Code:
//prints the name of a city corresponding to its postcode

#include<stdio.h>

int main()

int postcode;

printf("Enter postcode (74800,54000,44000,25000,87300): ");

scanf_s("%d", &postcode); //asks the user to enter postcode


switch (postcode) //switch statement whose controlling expression is
"postcode" begins here
{

case 74800:

printf("Karachi"); //prints Karachi if postcode is 74800

break;
case 54000:

printf("Lahore"); //prints Lahore if postcode is 44000

break;

case 44000:

printf("Islamabad"); //prints Islamabad if postcode is 54000


break;

case 25000:

printf("Peshawar"); //prints Peshawar if postcode is 25000

break;

case 87300:

printf("Quetta"); //prints Quetta if postcode is 87300

break;

default:

printf("Postcode is invalid"); //if postcode is other than the above


mentioned postcodes, it will print "Postcode is not valid"

break;
} //end of switch statement

return 0;

}
Output:
For Karachi:
For Lahore:

For Islamabad:
For Peshawar:

For Quetta:
For any other city:

//These codes have been written in Visual Studio 2019. In VS2019, scanf_s works instead of
scanf.

You might also like