Programming Fundamentals
Programming Fundamentals
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;
scanf("%c", &ch);
a = ch;
else{
}
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;
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);