The document contains summaries of 8 programming practicals involving arithmetic, relational, and ternary operators. Practicals include calculating arithmetic operations, employee salary, temperature conversion, simple interest, distance covered, currency conversion, swapping values with and without a temporary variable, and finding the greatest of two numbers using a ternary operator. The programs demonstrate the use of operators, type casting, and conditional statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
65 views10 pages
20TIC027 Practical2
The document contains summaries of 8 programming practicals involving arithmetic, relational, and ternary operators. Practicals include calculating arithmetic operations, employee salary, temperature conversion, simple interest, distance covered, currency conversion, swapping values with and without a temporary variable, and finding the greatest of two numbers using a ternary operator. The programs demonstrate the use of operators, type casting, and conditional statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10
Date: 13 Jan
Roll No. and Name: 20TIC027 and Om Gupta
Course Code and Name: 2CS101 Computer Programming Practical No.: 2(a) Aim: To scan two numbers and display result of different arithmetic operations (+, -, *, / and %) Methodology Followed: #include<stdio.h> main() { int n1, n2; float div; printf("Enter two numbers: \n"); scanf("%d %d",&n1,&n2); printf("Addition is %d \n", n1+n2); printf("Subtraction is %d \n", n1 - n2); printf("Multiplication is %d \n", n1 * n2); printf("Division is %f \n", (float)n1/n2); printf("Modulus is %d \n", n1%n2); } Theoretical Principles Used: In this program we have used arithmetic operators and type casting for converting the integer output to float output. Type casting is basically converting the data-type of the output variable from one data-type to another. Input/Output: Practical No.: 2(b) Aim: To write payment scheme of Employee Methodology Followed: #include <stdio.h> int main() { float bs, gs, da, hra; printf("Enter basic salary\n"); scanf("%f", &bs); if( bs < 5000 ) { hra = bs * 10 / 100; da = bs * 90 / 100; } else { hra = 600; da =bs * 95 / 100; } gs = bs + da + hra; printf("Gross Salary is Rs %f\n", gs); return 0; } Theoretical Principles Used: In this program we have used two kind of operators, relational operator in the if condition and arithmetic operators inside if loop. The relational operators are used to basically define a relation between two entities. Input/Output:
Practical No.: 2(c )
Aim: The driver is driving a car from city Ahmedabad to city Mumbai, in Ahmedabad temperature displays in Celsius while in Mumbai the temperature displayed in Fahrenheit, a driver wants to find the difference between the temperatures of two cities in Celsius. Methodology Followed: #include<stdio.h> int main () { float Celsius; float Fahrenheit; printf("Enter temperature in Celsius : "); scanf("%f",&Celsius); // (1.8) *Celsius +32 Fahrenheit = (1.8) *Celsius +32; printf("Celsius = %f C and Fahrenheit = %f F",Celsius, Fahrenheit); return 0; } Theoretical Principles Used: In this program we have used the arithmetic operators to convert the temperature from Celsius to Fahrenheit. Input/Output:
Practical No.: 2(d)
Aim: To calculate simple interest Methodology Followed: #include<stdio.h> int main() { int s_i; int principle,time,roi; printf("\nEnter principle : "); scanf("%d",&principle); printf("\nEnter rate of interest : "); scanf("%d",&roi); printf("\nEnter time : "); scanf("%d",&time); s_i = (principle*roi*time)/100; printf("\nSimple Interest : %d",s_i); return 0; } Theoretical Principles Used: In this program we have used the arithmetic operators to find the simple interest from the values of principle amount, rate of interest and time period entered by user. Input/Output:
Practical No.: 2(e)
Aim: A boy was punished and asked to cover 5 rounds of the circular ground. Area of the ground is 32000 sq mtr. Calculate how many kilometers the boy has covered. Methodology Followed: #include<stdio.h> #include<math.h> int main () { float pi = 3.14, ci,dis,area,rad; area = 32000.00; rad = sqrt(area/pi); printf("Radius of the circular ground is : %f mtr", rad); ci = 2 * pi * rad; dis = 5 * ci; printf("\nDistance covered by boy is : %f mtr", dis) ; return (0); } Theoretical Principles Used: In this program we have used the arithmetic operators to find the area, circumference and diameter of a circle. Input/Output:
Practical No.: 2(f)
Aim: Covert Rupee (Decimal) to Paise (Non-Decimal) value Methodology followed: #include<stdio.h> int main() { float n; int rupees,paise; printf("Enter the value in decimal : "); scanf("%f",&n); rupees = (int)n; paise = (n - rupees)*100; printf(" \nYou have %d Rupees and %d Paise.", rupees, paise); return 0; } Theoretical Principles Used: In this program we have used type casting to find the rupees and then we have used arithmetic operators to find the paise. Input/Output:
Practical No.: 2(g)
Aim: To swap the value of two numbers (i) using a temporary variable and (ii) without using a temporary variable Methodology Followed: Case1 #include<stdio.h> int main() { double first, second, temp; printf("Enter first number: "); scanf("%lf", &first); printf("Enter second number: "); scanf("%lf", &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf("\nAfter swapping, firstNumber = %.2lf\n", first); printf("After swapping, secondNumber = %.2lf", second); return 0; } Case2 #include <stdio.h> int main() { double a, b; printf("Enter a: "); scanf("%lf", &a); printf("Enter b: "); scanf("%lf", &b); a = a - b; b = a + b; a = b - a; printf("After swapping, a = %.2lf\n", a); printf("After swapping, b = %.2lf", b); return 0; } Theoretical Principles Used: In this program we have used the arithmetic operators to swap the numbers. Input/Output: Case1
Case2
Practical No.: 2(h)
Aim: To find greatest of two and three numbers using the ternary operator. Methodology Followed: #include <stdio.h> int main() { // variable declaration int n1 = 50, n2 = 150, max; // Largest among n1 and n2 max = (n1 > n2) ? n1 : n2; // Print the largest number printf("Largest number between %d and %d is %d." ,n1, n2, max); return 0; } Theoretical Principles Used: In this program we have used the ternary operator to find the largest number. This operator is similar to if-else statement but unlike that statement it takes less space. Ternary operation comes under the Misc Operators in C programming. Input/Output:
Conclusion: Hereby, we have learnt the use of different operators and type casting.