0% found this document useful (0 votes)
9 views19 pages

C Programming Basics: Data Types & Logic

Uploaded by

syamantoksaha8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

C Programming Basics: Data Types & Logic

Uploaded by

syamantoksaha8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 1:

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);

if(c>=97 && c<=122)


{
printf("The character is lowercase \n");
if(c=='a' || c=='i' || c=='e' || c=='o' || c=='u')
printf("Character is a vowel");
else
printf("Character is consonant");
}

else if(c>=65 && c<=90)


{
printf("The character is upperrcase \n");
if(c=='A' || c=='I' || c=='E' || c=='O' || c=='U')
printf("Character is a vowel");
else
printf("Character is consonant");
}

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 ’.

Common questions

Powered by AI

The programs may encounter issues such as buffer overflows or incorrect data types being entered. These can lead to unexpected behavior or crashes. To mitigate these, input validation can be implemented. This includes checking that inputs meet expected formats, using safe buffer size limits, or employing functions like fgets() for strings to avoid overflow. Moreover, ensuring input values correspond to the expected data types can prevent misinterpretations .

The program checks the ASCII value of the input character to determine if it is uppercase (ASCII between 65 and 90) or lowercase (ASCII between 97 and 122). It uses nested if-else statements to further classify the character as a vowel or consonant based on a set of conditions that compare the character against specific vowel characters .

By using ASCII values, the program quickly determines the case of a character and its classification as a vowel or consonant through numeric comparison. This method reduces complexity and execution time as numerical comparisons are generally faster and require less processing power than string-based character checks, enhancing overall program efficiency .

The program uses a loop to reverse the digits of the input number. It then compares the original number with the reversed one. The process involves using the modulus and division operations to extract and combine digits in reverse order. After reversing the number, it checks if the original and reversed numbers are equal to determine if they are the same, providing a result based on this comparison .

The program uses multiple data types to store diverse information: an int for age, a float for height, a char for a name's initial, a double for weight, a long for phone number, a short for zip code, an unsigned int for the number of pets, and a signed int for the difference between income and expenses. This variety allows the program to handle different types of numerical and character data efficiently, leveraging C's capabilities to manage and manipulate distinct forms of data .

The program uses four different data types: int, float, char, and double. The int variable stores the number of items, the float variable stores the tax rate, the char variable stores the currency symbol, and the double variable stores the subtotal amount. For output, it uses specific format specifiers in the printf() function: %d for int, %.2f for float with two decimal places, %c for char, and %.2lf for double with two decimal places to format and print these variables correctly .

The program checks the validity of a triangle by ensuring the sum of its three angles is exactly 180 degrees and that none of the angles is zero. It uses conditional statements to add the angles and compare the result. If the sum is 180 and all angles are positive, the triangle is deemed valid; otherwise, it is invalid .

Logical operators in the triangle validation program simplify the conditional checks needed to verify all angles are non-zero and their sum is 180 degrees. This leads to clearer code through concise conditions. However, it might become a limitation if additional validation criteria (e.g., checking if angles are valid for specific triangle types) are introduced, requiring more complex logic or nested conditions .

The program calculates the perimeter of a large equilateral triangle by first determining the area of one of the smaller congruent triangles with a side length of 2 inches. It then multiplies this area by 9 to obtain the total area of the large triangle. The side length of the large triangle is calculated using the squares and inverse square root functions from the total area, which is then tripled to find the perimeter .

In the programs, mathematical expressions are integrated directly into printf statements to streamline calculations and output. For example, the total amount after tax is computed in-line as ((b*d)+d) within the printf statement, allowing for concise code without intermediate variables. This approach simplifies the code but requires careful precision to ensure expression correctness, clarity, and maintainability .

You might also like