C Programming Projects
C Programming Projects
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;
scanf_s("%1d", &d); //asks the user to enter first digit which is stored in
'd'
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
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
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: ");
if (a > c) {
if (a > d)
//in case 'd' is greater than 'a', check whether d is greater than
the rest of integers or not
else {
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)
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 {
min = c;
else min = d;
}
}
else {
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()
scanf_s("%d", &month); //asks the user to enter the month number, NOT THE
NAME
break;
case 5: //for MAY
break;
break;
rate = 0.11; //Rate is 11% for March. 11% is 11 divided by 100 which
is equal to 0.11
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
break;
}
return 0;
Output:
For June:
For May:
For April:
For March:
if (y == 8)
if (x == 5)
printf("@@@@@\n");
}
else printf("#####\n");
printf("$$$$$\n");
printf("&&&&&\n");
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;
case 74800:
break;
case 54000:
break;
case 44000:
case 25000:
break;
case 87300:
break;
default:
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.