C Programing
C Programing
Aim: You are a cashier at a grocery store and you need to write a C program that calculates
and prints the total bill for a customer. The program should declare and initialize four
variables of different data types: int, float, char, and double. The int variable should store the
number of items purchased by the customer, the float variable should store the tax rate, the
char variable should store the currency symbol, and the double variable should store the
subtotal amount before tax. The program should also calculate and print the total amount after
tax using the appropriate format specifiers in the printf() function. For example, %d for int,
%f for float, %c for char, and %lf for double.
Hardware Requirement:
Device name LAPTOP-C19KJEDO
Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Software Requirements:
EditionWindows 11 Home Single Language
Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
Compiler Online (GDB Compiler)
Knowledge Requirement:
Used the concept of taking inputs from the user using scanf function and printing outputs
using printf function. Also used mathematical operations in printf statement itself.
Theory:
Used four data types which are int, float, double and char to take input from the users. Also
used mathematical expression to calculate the total amount from given data.
Code:
#include <stdio.h>
int main()
{
int a;
float b;
char c;
double d;
printf("Enter number of items \n");
scanf("%d",&a);
printf("Enter the tax rate \n");
scanf("%f",&b);
printf("Enter the currency symbol \n");
scanf("%s",&c);
printf("Enter the subtotal \n");
scanf("%lf",&d);
printf("\n");
printf("Number of items are: %d \n",a);
printf("Tax rate: %.2f \n",b);
printf("Subtotal: %c%.2lf \n",c,d);
printf("Total: %.2lf",((b*d)+d));
return 0;
}
Output:
Conclusion:
Learned the technique of using mathematical expression in print statement.
Reference:
Used the book which sir gave – ‘ Let us C ’.
Question 2:
Aim: Write a C program that declares and initializes six variables of different data types:
int, float, char, double, long, short, unsigned, and signed. The int variable should store the age
of a person in years, the float variable should store the height of a person in meters, the char
variable should store the first letter of the person’s name, the double variable should store the
weight of a person in kilograms, the long variable should store the phone number of the
person, the short variable should store the zip code of the person’s address, the unsigned
variable should store the number of pets owned by the person, and the signed variable should
store the difference between the person’s income and expenses in dollars. The program
should also print the values of these variables using the appropriate format specifiers in the
printf() function. For example, %d for int, %f for float, %c for char, %lf for double, %ld for
long, %hd for short, %u for unsigned, and %d for signed.
Hardware Requirement:
Device name LAPTOP-C19KJEDO
Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Software Requirements:
EditionWindows 11 Home Single Language
Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
Compiler Online (GDB Compiler)
Knowledge Requirement:
Used the concept of taking inputs from the user using scanf function and printing outputs
using printf function. Also used 8 different kinds of data types to store inputs.
Code:
#include <stdio.h>
int main()
{
int a;
float b;
char c;
double d;
long int e;
short int f;
unsigned int g;
signed int h;
printf("Enter your age \n");
scanf("%d",&a);
printf("Enter your height in meters \n");
scanf("%f",&b);
printf("Enter the first letter of your name \n");
scanf("%",&c);
printf("Enter your weight in killograms \n");
scanf("%lf",&d);
printf("Enter your phone number \n");
scanf("%ld",&e);
printf("Enter your zip zode \n");
scanf("%hd",&f);
printf("Enter number of pets owned \n");
scanf("%u",&g);
printf("The difference between income and expanses \n");
scanf("%d",&h);
printf("\n");
printf("The age of the person is %d years \n",a);
printf("The height of the person is %f meters \n",b);
printf("The initial of the person's name is %c \n",c);
printf("The weight of the person is %lf kilograms \n",d);
printf("The phone number of the person is %ld \n",e);
printf("The zip code of the person's address is %hd \n",f);
printf("The number of pets owned by the person is %u \n",g);
printf("The difference between income and expenses of the person is %d",h);
return 0;
}
Output:
Conclusion:
Learned the concept of using different kinds of data types and also their respective commands
to take input from user and store it in the declared variable.
Reference:
Used the book which sir gave – ‘ Let us C ’.
Question 3:
Aim: A five-digit number is entered through the keyboard. Write a program to obtain the
reversed number and to determine whether the original and reversed numbers are equal or
not.
Hardware Requirement:
Device name LAPTOP-C19KJEDO
Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Software Requirements:
EditionWindows 11 Home Single Language
Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
Compiler Online (GDB Compiler)
Knowledge Requirements:
Used the concept of while loop and if-else statements
Theory:
Used five integer data type variables.
Code :
#include <stdio.h>
int main()
{
int num,a,i=0,n,b=0;
printf("Enter a five digit number \n");
scanf("%d",&num);
a=num;
while(num>0)
{
n=num%10;
i=(10*i)+n;
num = num/10;
b++;
}
if(b==5)
{
printf("The reversed number is: %d \n",i);
if(i==a)
printf("The original and reversed number are equal");
else
printf("The original and reversed number are unequal");
}
else
printf("Entered number is not five digits");
return 0;
}
Output :
Conclusion:
Learned the concept of reversing a number using mathematical operators like modulus and
division.
Reference:
Used the book which sir gave – ‘ Let us C ’
Question 4:
Aim: Write a program to check whether a triangle is valid or not, when the three angles of
the triangle are entered through the keyboard. A triangle is valid if the sum of all the three
angles is equal to 180 degrees.
Hardware Requirement:
Device name LAPTOP-C19KJEDO
Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Software Requirement:
EditionWindows 11 Home Single Language
Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
Compiler Online (GDB Compiler)
Knowledge Requirements:
Used the concept of if-else statements and logical operators.
Theory:
Used 4 double data type variables.
Code:
#include <stdio.h>
int main()
{
double a,b,c,sum;
printf("Enter angle 1 of the triangle \n");
scanf("%lf",&a);
printf("Enter angle 2 of the triangle \n");
scanf("%lf",&b);
printf("Enter angle 3 of the triangle \n");
scanf("%lf",&c);
sum=a+b+c;
if(a!=0 && b!=0 && c!=0)
{
if(sum==180.00)
printf("The triangle is valid");
else
printf("The trianle is invalid");
}
else
printf("The triangle is invalid");
return 0;
}
Output:
Conclusion:
Learned the concept of using logical operators in if-else statements
Reference:
Used the book which sir gave – ‘ Let us C ’
Question 5:
Aim:
Check whether a given character is a lowercase character or upper-case character and also
print whether it is a vowel or a consonant.
Hardware Requirements:
Device name LAPTOP-C19KJEDO
Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Software Requirements:
EditionWindows 11 Home Single Language
Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
Compiler Online (GDB Compiler)
Knowledge Requirements:
Used the concept of ascii codes such that for uppercase ascii code is between 65 to 90 and for
lowercase ascii code is between 97 to 122.
Theory:
Used a character data type variable.
Code:
#include <stdio.h>
int main()
{
char c;
printf("Enter a character \n");
scanf("%c",&c);
else
printf("Invalid input");
return 0;
}
Output:
Conclusion:
Learned the concept of using ascii codes in if-else nested statements.
Reference:
Used the book which sir gave – ‘ Let us C ’.
Question 6:
Aim: Jhon was fascinated by geometric shapes. He drew a large equilateral triangle on a
piece of paper and divided it into 9 smaller congruent equilateral triangles. If each small
triangle had a side length of 2 inches, what was the perimeter of the large triangle? Write a C
program for the given problem.
Hardware Requirement:
Device name LAPTOP-C19KJEDO
Processor AMD Ryzen 7 5800H with Radeon Graphics 3.20 GHz
Installed RAM16.0 GB (15.3 GB usable)
Device ID 57D7EDC7-F482-43E0-B727-EFD96C13150E
Product ID 00342-42643-48855-AAOEM
System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display
Software Requirement:
EditionWindows 11 Home Single Language
Version 22H2
Installed on 30-07-2023
OS build 22621.2283
Experience Windows Feature Experience Pack 1000.22662.1000.0
Compiler Online (GDB Compiler)
Knowledge Requirements:
Used the concept of math function in the code for obtaining square root value which is
“#include <math.h>” .
Theory:
Used 4 float data type variables.
Code:
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,d;
a= (1.732*2*2)/4; //to find area of congurrent small triangle
b=9*a; //to find total area of the large triangle
c=sqrt((4*b)/1.732); //to find each side of the large triangle
d=3*c; //to find the total perimeter of the large triangle
printf("The perimeter of the large equilateral triangle is: %.2f",d);
return 0;
}
Output:
Conclusion:
Learned the concept of including a math function in c programming code.
Reference:
Used the book which sir gave – ‘ Let us C ’.