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

Programming Fundamentals

The document contains code for 4 programs in C: 1) A program to check if a character entered by the user is an alphabet, digit or special character. 2) A program to calculate discount on shopping items based on total cost and print final amount. 3) A program to calculate the roots of a quadratic equation given coefficients a, b, c. 4) A program to calculate percentage of calories from fat in a food and check if it is low in fat.

Uploaded by

Sabih Salam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Programming Fundamentals

The document contains code for 4 programs in C: 1) A program to check if a character entered by the user is an alphabet, digit or special character. 2) A program to calculate discount on shopping items based on total cost and print final amount. 3) A program to calculate the roots of a quadratic equation given coefficients a, b, c. 4) A program to calculate percentage of calories from fat in a food and check if it is low in fat.

Uploaded by

Sabih Salam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Muhammad Sabih Salam

Roll #: CTAI-040

LAB 04
Write a C program to input a character from user and check whether given character
is small alphabet, capital alphabet, digit or special character, using if else.
#include <stdio.h>

int main(void){

char ch;

int a;

printf("\nEnter any single character: ");

scanf("%c", &ch);

a = ch;

if(a >= 65 && a <= 90){

printf("\n%c is an uppercase alphabet.", ch);

else if(a >= 97 && a <= 122){

printf("\n%c is a lowercase alphabet.", ch);

else if(ch >= '0' && ch <= '9'){

printf("\n%c is a digit.", ch);

else{

printf("\n%c is a special character.", ch);

}
An online shopping store is providing discounts on the items due to the Eid. If the cost of
items is more than 1999 it will give a discount up to 50%. If the cost of shopping is 2000 to
4000, a 20% discount will be applied. If the cost of shopping is 4001 to 6000, a 30% discount
will be applied. If it's more than 6000 then 50% discount will be applied to the cost of
shopping. Print the actual amount, saved amount and the amount after discount.

Source Code:
#include <stdio.h>
int main(void){
int items;
float cost, old_total, new_total, saved_price;
printf("\nEnter the total number of items: ");
scanf("%d", &items);
printf("\nEnter the price of each item: ");
scanf("%f", &cost);
old_total = cost * items;
if(old_total > 1999){
if(old_total >= 2000 && old_total <= 4000){
new_total = old_total - ((old_total*20)/100);
saved_price = old_total - new_total;
printf("\nYour old total was %.2f rupees, your new total is %.2f rupees
and you saved %.2f rupees.", old_total, new_total, saved_price);
}
else if(old_total >= 4001 && old_total <= 6000){
new_total = old_total - ((old_total*30)/100);
saved_price = old_total - new_total;
printf("\nYour old total was %.2f rupees, your new total is %.2f rupees
and you saved %.2f rupees.", old_total, new_total, saved_price);
}
else if(old_total > 6000){
new_total = old_total - ((old_total*50)/100);
saved_price = old_total - new_total;
printf("\nYour old total was %.2f rupees, your new total is %.2f rupees
and you saved %.2f rupees.", old_total, new_total, saved_price);
}
}
else{
printf("\nYou do not get any discount. Your total is %.2f rupees.", old_total);
}
Write a C program to find all roots of a quadratic equation by using the given formula; it
is required to take user input for a, b and c values.

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

int main(void){
float a, b ,c, root_one, root_two;

printf("\nEnter the values of a, b and c: ");


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

root_one = (-b+sqrt(b*b-(4*a*c)))/(2*a);
root_two = (-b-sqrt(b*b-(4*a*c)))/(2*a);

printf("\nThe first root is %.2f and the second root is %.2f", root_one, root_two);

Write a program that asks for the number of calories and fat grams in a food. The program should display
the percentage of calories that come from fat. If the calories from fat are less than 30% of the total
calories of the food, it should also display a message indicating that the food is low in fat. One gram of
fat has 9 calories, so Calories from fat = fat grams * 9. The percentage of calories from fat can be
calculated as: calories from fat/total calories Input validation: Make sure the number of calories and fat
grams are not less than 0. Also, the number of calories from fat cannot be greater than the total number
of calories. If that happens, display an error message indicating that either the calories or fat grams were
incorrectly entered.

#include<stdio.h>
int main(void){
float cal, fat_grams, fat_cal, fat_cal_perc;
printf("\nEnter the total number of calories in your food: ");
scanf("%f", &cal);
printf("\nEnter the total amount of fat in your food(in grams): ");
scanf("%f", &fat_grams);

fat_cal = fat_grams * 9;
fat_cal_perc = ((fat_cal/cal)*100);

if(!(cal < 0 || fat_grams < 0)){


if(cal > fat_cal){
if(fat_cal_perc < 30){
printf("\nYour food is low in fat.");
}
else{
printf("\nYour food is high in fat.");
}
}
}
if(cal < 0 && fat_grams < 0){
printf("\nError.The calories of your food and grams of fat can never be
negative.");
return 1;
}
if(cal < 0){
printf("\nError. The calories of your food can never be negative.");
return 1;
}
else if(fat_grams < 0){
printf("\nError. The grams of fat in your food can never be negative.");
return 1;
}
if(fat_cal > cal){
printf("\nError. Fat calories = %.2f\nThe calories from fat can never be
greater than the calories of your food.", fat_cal);
return 1;
}

You might also like