24ESPL101 - Programming in C Laboratory Record PDF

Summary

This is a record of a programming in C laboratory course for the first semester of the 2024-2025 academic year at Sai Ram Engineering College, India. It includes experiment details, aims, algorithms, programs, output, and results for various programming exercises.

Full Transcript

DEPARTMENT OF MECHANICAL ENGINEERING (2024-2028) Batch FIRST SEMESTER 24ESPL101 - PROGRAMMING IN C LABORATORY 2024- 2025 Department of Mechanical Engineering Bonafide Certificate...

DEPARTMENT OF MECHANICAL ENGINEERING (2024-2028) Batch FIRST SEMESTER 24ESPL101 - PROGRAMMING IN C LABORATORY 2024- 2025 Department of Mechanical Engineering Bonafide Certificate REGISTER NUMBER: This is to certify that this is the Bonafide record of the work done by __________________________________________________of B.E., Mechanical Engineering in the 24ESPL101-PROGRAMMING IN C Laboratory during the academic year 2024-2025. Station: Chennai – 600 044 Date : STAFF IN-CHARGE HEAD OF THE DEPARTMENT Submitted for End semester Practical Examination held on at Sri Sairam Engineering College, Chennai – 600 044. INTERNAL EXAMINER EXTERNAL EXAMINER INDEX EXP DATE NAME OF THE EXPERIMENT PAGE SIGNATURE NO. NO. 1 Using I/O Statements and Expressions 2 Using Decision Making Constructs 3 Checking for Leap Year 4 Implementing Simple Arithmetic Calculator 5 Checking for Armstrong Number 6 Checking for Odd/Even Number 7 Finding Factorial of a given Number 8 Finding the Average of 4 given numbers 9 Print Half pyramid of * 10 Display array elements using two-dimensional array 11 Swapping two numbers using Functions 12 Generating Prime Numbers between an interval using Functions 13 Towers of Hanoi using Recursion 14 Largest element in an array using Functions 15 Concatenate two strings 16 Find the Length of the string without using library function 17 Find the Frequency of a Character in the string 18 Store and display the Student Information using a Structure 19 Processing student information using an Array of Structures. 20 File Operations EX NO: 1 I/O statements and expressions. Date: a. C Program to display the values using Data types Aim: To write a C program to display the values using Data types. Algorithm: Step 1: Start the Program. Step 2: declare the variables as int, float and char. Step 3: get the values using the data types. Step 4: Display the values using data types. Step 5: Stop the program. Program: #include int main() { int ivar; float fvar; char svar; // Input integer value printf("\nEnter the integer value: "); scanf("%d", &ivar); // Input float value printf("\nEnter the float value: "); scanf("%f", &fvar); // Input string value printf("\nEnter the string value: "); scanf("%s", svar); // Output the entered values printf("\n"); printf("\nInteger value is: %d", ivar); printf("\nFloat value is: %f", fvar); printf("\nEntered string is: %s\n", svar); return 0; } Output: Result: Thus a C program using I/O statements and expressions to display the values using Data types has been executed successfully and its output is verified. b. Evaluate area of triangle Aim: To evaluate the area of a triangle using the formula area=sqrt(s(s-a)(s-b)(s-c)) where a,b,c are the sides of the triangle and s=(a+b+c)/2. Algorithm: Step1: start Step2: input a,b,c Step3: s=(a+b+c)/2 Step4: A=sqrt(s*(s-a)(s-b)(s-c)) Step5: Print A Step 6: stop Program: #include #include int main() { int a, b, c; float s, area; // Input values of the sides of the triangle printf("Enter the values of a, b, c: "); scanf("%d%d%d", &a, &b, &c); // Calculate the semi-perimeter s = (a + b + c) / 2.0; // Calculate the area using Heron's formula area = sqrt(s * (s - a) * (s - b) * (s - c)); // Output the area of the triangle printf("The area of the triangle is = %f\n", area); return 0; } Output: Result: Thus a C program using I/O statements and expressions to evaluate the area of a triangle has been executed successfully and its output is verified. EX NO: 2. Programs using decision-making constructs. Date: a) Positive, negative or zero Aim: To write a C program to check positive, negative or zero using a simple if statement. Algorithm: Step 1: Start the Program. Step 2: Declare the variables as int. Step 3: Check whether the given number is greater than zero then print Number is POSITIVE. Step 4: If the number is less than zero then print Number is NEGATIVE. Step 5: If the number is zero then print ZERO. Step 6: Stop the program. Program: #include int main() { int num; // Input number from user printf("Enter any number: "); scanf("%d", &num); // Check if the number is positive, negative, or zero if(num > 0) { printf("Number is POSITIVE\n"); } else if(num < 0) { printf("Number is NEGATIVE\n"); } else { printf("Number is ZERO\n"); } return 0; } Output: Result: Thus a C program using decision making constructs to check whether the number is positive, negative or zero using simple if statements has been executed successfully and its output is verified. b). Greatest among two numbers Aim: To write a C program to check the greatest among two numbers using the if else statement. Algorithm: Step 1: Start the Program. Step 2: Declare the variables x and y as int. Step 3: Get the values for x and y from the user. Step 4: Compare the two values x and y Step 5: If the value of x is greater when compared to y then print x is Greater. Step 6: If the value of y is greater than x then print y is Greater using Else statement. Step 7: Stop the program. Program: #include int main() { int x, y; // Input the value of x printf("Enter the value of x:\n"); scanf("%d", &x); // Input the value of y printf("Enter the value of y:\n"); scanf("%d", &y); // Compare the values of x and y if (x > y) { printf("x is Greater\n"); } else if (x < y) { printf("y is Greater\n"); } else { printf("x and y are Equal\n"); } return 0; } Output: Result: Thus a C program using decision making constructs to check greatest among two numbers using a simple if else statement has been executed successfully and its output is verified. c). Greatest among three numbers. Aim: To write a C program to check greatest among three numbers using Nested if.else statement. Algorithm: Step 1: Start the program Step 2: Declare variable a, b, c. Step 3: If a > b go to step 4 Otherwise go to step 5 Step 4: If a > c then print a is greatest Otherwise print c is greatest Step 5: If b > c then print b is greatest Otherwise print c is greatest Step 6: Stop Program: #include int main() { int a, b, c; // Input 3 numbers from the user printf("Enter 3 numbers: "); scanf("%d%d%d", &a, &b, &c); // Compare the numbers and determine the greatest if(a > b) { if(a > c) { printf("a is the greatest\n"); } else { printf("c is the greatest\n"); } } else { if(b > c) { printf("b is the greatest\n"); } else { printf("c is the greatest\n"); } } return 0; } Output: Result: Thus a C program using decision making constructs to check greatest among three numbers using a simple Nested if else statement has been executed successfully and its output is verified. d). C program to check a given number is divisible by both 5 and 8 using else if ladder statement. Aim: To write a C program to check a given number is divisible by both 5 and 8 using else.if ladder statement. Algorithm: Step 1: Start the program Step 2: Declare variable a and get the value from the user. Step 3: If a%5 == 0 && a%8 == 0 then print “Divisible by both 5 and 8” Otherwise go to step 4 Step 4: If a%8 == 0 then print “Divisible by 8” Otherwise go to step 5 Step 5: If a%5 == 0 then print “Divisible by 5” Otherwise go to step 6 Step 6: print "Divisible by none". Step 7: End. Program: #include int main() { int a; // Input a number from the user printf("Enter a number: "); scanf("%d", &a); // Check divisibility by both 5 and 8, or by 5 or 8 individually if(a % 5 == 0 && a % 8 == 0) { printf("Divisible by both 5 and 8\n"); } else if(a % 8 == 0) { printf("Divisible by 8\n"); } else if(a % 5 == 0) { printf("Divisible by 5\n"); } else { printf("Divisible by none\n"); } return 0; } Output: Result: Thus a C program using decision making constructs to check a given number is divisible by both 5 and 8 using else.. if the ladder statement has been executed successfully and its output is verified. EX NO 3. Check for leap year Date: Aim: To develop a C Program to find whether the given year is a leap year or not. Algorithm: Step:1 Start. Step:2 Get the year Step:3 Check if the year Modulo 4 equal to 0 and Modulo 100 not equal to 0. Step:4 Check if the year Modulo 400 equal to 0 Step:5 if YES print leap year Step:6 Else print Not Leap Year Step:7 Stop Program: #include int main() { int year; // Input the year from the user printf("Enter the Year (YYYY): "); scanf("%d", &year); // Check if the year is a leap year if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { printf("\nThe Given year %d is a Leap Year\n", year); } else { printf("\nThe Given year %d is Not a Leap Year\n", year); } return 0; } Output: Result: Thus a C program to find whether the given year is a leap year or not has been executed successfully and its output is verified. EX NO: 4 Simple Arithmetic Calculator. Date: Aim: To develop a C Program to perform the Calculator operations, namely, addition, subtraction, multiplication, division and square of a number. Algorithm: Step 1 Start Step 2 Display the menu with options for addition, subtraction, multiplication, division, and square. Step 3 Take user input for the operation choice. Step 4 If the user selects addition (option 1): a. Input two numbers. b. Calculate the sum. c. Display the result. Step 5 If the user selects subtraction (option 2): a. Input two numbers. b. Calculate the difference. c. Display the result. Step 6 If the user selects multiplication (option 3): a. Input two numbers. b. Calculate the product. c. Display the result. Step 7 If the user selects division (option 4): a. Input two numbers. b. If the divisor is not zero, calculate the quotient. c. Else, display an error message for division by zero. Step 8 If the user selects square (option 5): a. Input a number. b. Calculate the square. c. Display the result. Step 9 End Program #include int main() { int choice; float num1, num2, result; printf("Calculator Operations:\n"); printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); printf("5. Square of a Number\n"); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: // Addition printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); result = num1 + num2; printf("Result: %.2f\n", result); break; case 2: // Subtraction printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); result = num1 - num2; printf("Result: %.2f\n", result); break; case 3: // Multiplication printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); result = num1 * num2; printf("Result: %.2f\n", result); break; case 4: // Division printf("Enter two numbers: "); scanf("%f %f", &num1, &num2); if (num2 != 0) { result = num1 / num2; printf("Result: %.2f\n", result); } else printf("Error: Division by zero is not allowed.\n"); break; case 5: // Square of a Number printf("Enter a number: "); scanf("%f", &num1); result = num1 * num1; printf("Square: %.2f\n", result); break; default: printf("Invalid choice.\n"); } return 0; } Output: Result: Thus a C program to create a Simple Calculator using switch case has been executed successfully and its output is verified. EX NO 5. Armstrong number Date: Aim: To write a C program to check whether a given number is an Armstrong number or not. Algorithm: Step 1: Start the Program. Step 2: Get the number to be checked as Armstrong number or not. Step 3: Check whether the number is Armstrong or not. Step 4: Print whether the number is Armstrong number or not. Step 5: Stop the Program. Program: #include int main() { int num, originalNum, remainder, result = 0; // Input a three-digit integer printf("Enter a three-digit integer: "); scanf("%d", &num); // Check if the input is a three-digit number if (num < 100 || num > 999) { printf("Please enter a valid three-digit integer.\n"); return 1; // Exit with an error code } originalNum = num; // Calculate the Armstrong number while (originalNum != 0) { // remainder contains the last digit remainder = originalNum % 10; result += remainder * remainder * remainder; // Removing last digit from the original number originalNum /= 10; } // Check if the original number is equal to the calculated result if (result == num) printf("%d is an Armstrong number.\n", num); else printf("%d is not an Armstrong number.\n", num); return 0; } Output: Result: Thus a C program to check whether a given number is Armstrong number or not has been executed successfully and its output is verified EX NO 6. Odd or even Date: Aim: To write a c program to check whether a given number is odd or even and verify the output. Algorithm: Step 1 : Start the program Step 2 : Declare a variable N and get the value from the user Step 3 : If N%2==0 print "It is an Even Number". Otherwise go to step 4 Step 4 : Print "It is an Odd Number" Step 5 : Stop the program Program: #include int main() { int num; // Input prompt for the user printf("\nOdd or Even Number\n"); printf("\nEnter an integer you want to check: "); scanf("%d", &num); // Check if the number is even or odd if ((num % 2) == 0) { printf("%d is an Even number\n", num); } else { printf("%d is an Odd number\n", num); } return 0; } Output: Result: Thus, a C program has been developed which checks if the given number is odd or even and its output has been verified. EX NO 7. Factorial of a given number Date: Aim: To check a C program to find the factorial of a given number. Algorithm: Step 1:Start Step 2:Declare variables num,fact and i. Step 3:Initialize Variables fact=1,i=1 Step 4:Read value of num Step 5:Repeat the steps until i=num fact=fact*i i=i+1 Step 6:Display factorial Step 7:Stop Program: #include int main() { int i, num; long fact = 1; // Corrected type from longint to long // Input prompt for the user printf("ENTER A NUMBER: "); scanf("%d", &num); // Calculate factorial using a for loop for (i = 1; i

Use Quizgecko on...
Browser
Browser