Lab Manual by Prema For C Program
Lab Manual by Prema For C Program
Aim
To develop a program that takes input of a student's name, roll number, and marks obtained
in four subjects, then calculates and displays the student's name, roll number, and the
percentage of marks secured.
Algorithm
1. Start
2. Input Student Details
o Prompt the user to enter the student's name.
o Prompt the user to enter the student's roll number.
o Prompt the user to enter the marks obtained in four subjects.
3. Calculate Total Marks
o Add the marks obtained in all four subjects to get the total marks.
4. Calculate Percentage
o Divide the total marks by the maximum possible marks (400) and multiply by
100 to get the percentage.
5. Display Results
o Print the student's name, roll number, and the calculated percentage.
6. End
Result:
Thus the program to take input of name, rollno and marks obtained by a student in 4
subjects of 100 marks each and display the name, rollno with percentage score
secured.
To develop a program that takes input of two numbers and displays the maximum number
among them.
Algorithm
1. Start
2. Input Numbers
o Prompt the user to enter the first number.
o Prompt the user to enter the second number.
3. Compare Numbers
o Compare the first number with the second number.
o Determine the maximum number between the two.
4. Display Maximum Number
o Print the maximum number.
5. End
Result:
Thus the Output Verified to write a C program to input two numbers and display the
maximum number
Aim
Algorithm
1. Start
2. Input Character
o Prompt the user to enter a character.
3. Convert Character to Lowercase
o Convert the entered character to lowercase to handle both uppercase and
lowercase inputs.
4. Check Vowel or Consonant Using Switch Statement
o Use a switch statement (or its equivalent) to check if the character is 'a', 'e', 'i',
'o', or 'u'.
o If the character matches any of these cases, it is a vowel.
o Otherwise, it is a consonant.
5. Display Result
o Print whether the character is a vowel or a consonant.
6. End.
Result:
Thus the Output Verified to find whether a character is consonant or vowel using switch
statement
4. Write a C program to print positive integers from 1 to 10.
Aim
Algorithm
1. Start
2. Initialize Counter
o Set the counter variable to 1.
3. Loop through Numbers from 1 to 10
o Use a loop to iterate from the counter value (1) to 10.
o In each iteration, print the current value of the counter.
o Increment the counter by 1.
4. End
Result:
Algorithm
1. Start
2. Input Number
o Prompt the user to enter a number.
3. Initialize Variables
o Store the original number in a variable.
o Initialize a variable to store the reversed number and set it to 0.
4. Reverse the Number
o Use a loop to reverse the number:
Extract the last digit of the number.
Append the digit to the reversed number.
Remove the last digit from the original number.
5. Compare the Original and Reversed Numbers
o If the original number is equal to the reversed number, it is a palindrome.
o Otherwise, it is not a palindrome.
6. Display Result
o Print whether the number is a palindrome or not.
7. End
Result: Thus the Output Verified to check whether a number is Palindrome or not.
Algorithm
1. Start
2. Input Number of Terms
o Prompt the user to enter the number of terms for the Fibonacci series.
3. Initialize Variables
o Initialize two variables to store the first two terms of the series (usually 0 and
1).
o Initialize a counter to keep track of the number of terms generated.
4. Generate Fibonacci Series
o Use a loop to generate the series until the desired number of terms is reached:
Print the current term.
Calculate the next term by adding the last two terms.
Update the values of the terms.
Increment the counter.
5. End
Result:
To write a C program that inserts 5 elements into an array and prints the elements of the
array.
Algorithm
1. Start
2. Declare an array of size 5
o Define an array to hold 5 integer elements.
3. Input Elements
o Use a loop to prompt the user to enter 5 elements and store them in the array.
4. Print Elements
o Use another loop to print each element of the array.
5. End
Result:
Thus the Output Verified to insert 5 elements into an array and print the elements of
the array.
Algorithm
1. Start
2. Declare an array and input its size
o Define an array and a variable to hold its size.
3. Input Array Elements
o Use a loop to prompt the user to enter elements and store them in the array.
4. Reverse the Array Elements
o Use a loop to swap elements from the start with elements from the end of the
array until the middle of the array is reached.
5. Print Reversed Array Elements
o Use another loop to print each element of the reversed array.
6. End
Result:
Algorithm
1. Start
2. Declare Variables
o Declare two character arrays (strings) to hold the input strings.
o Declare a third character array to hold the concatenated result.
3. Input Strings
o Prompt the user to enter the first string and store it in the first array.
o Prompt the user to enter the second string and store it in the second array.
4. Concatenate Strings
o Copy the first string into the result array.
o Append the second string to the result array.
5. Print Concatenated String
o Print the concatenated string.
6. End
Result:
To write a C program that compares two strings without using string library functions.
Algorithm
1. Start
2. Declare Variables
o Declare two character arrays (strings) to hold the input strings.
o Declare an integer variable to store the comparison result.
3. Input Strings
o Prompt the user to enter the first string and store it in the first array.
o Prompt the user to enter the second string and store it in the second array.
4. Compare Strings
o Use a loop to compare corresponding characters of both strings.
o If characters differ, set the comparison result accordingly and break the loop.
o If the end of both strings is reached without any differences, set the
comparison result to indicate equality.
5. Print Comparison Result
o Print whether the strings are equal or which one is greater.
6. End
Result:
Thus the Output Verified to compare two strings without using string library functions
1. Start
2. Declare Function
o Define a recursive function fibonacci(int n) that returns the nth Fibonacci
number.
o Base cases:
If n is 0, return 0.
If n is 1, return 1.
o Recursive case:
For n greater than 1, return the sum of fibonacci(n-1) and fibonacci(n-
2).
3. Input Number of Terms
o Prompt the user to enter the number of terms (n) in the Fibonacci series.
4. Generate and Print Fibonacci Series
o Use a loop to call the fibonacci function for each term from 0 to n-1 and print
the results.
5. End
Result:
Thus the Output Verified to generate Fibonacci series using recursive function
To write a C program that finds the power of any number using recursion.
Algorithm
1. Start
2. Declare Function
o Define a recursive function power(int base, int exponent) that returns the result
of base raised to the power of exponent.
o Base case:
If exponent is 0, return 1 (since any number raised to the power of 0 is
1).
o Recursive case:
If exponent is greater than 0, return base * power(base, exponent - 1).
3. Input Base and Exponent
o Prompt the user to enter the base number.
o Prompt the user to enter the exponent.
4. Calculate and Print Power
o Call the power function with the input base and exponent.
o Print the result.
5. End
Result:
Thus the Output Verified to find power of any number using recursion.
Result:
Thus the Output Verified to add, subtract, multiply and divide two integers using user defined
type function with return type
Result:
Thus the Output Verified to swap two integers using call by value and call by reference
methods of passing arguments to a function
To write a C program that finds the biggest among three numbers using pointers.
Algorithm
1. Start
2. Declare Function
o Define a function void findLargest(int *ptr1, int *ptr2, int *ptr3) that takes
three integer pointers as arguments.
o Inside the function, compare the values pointed to by ptr1, ptr2, and ptr3 to
find the largest number.
o Print the largest number.
3. Input Three Integers
o Prompt the user to enter three integers.
4. Call Function
o Pass pointers to these integers to the findLargest function.
5. End
Result:
Thus the Output Verified to find biggest among three numbers using pointer.
16. Write a C program to compare two strings using
pointers.
Aim
Algorithm
1. Start
2. Declare Function
o Define a function int compareStrings(char *str1, char *str2) that takes two
character pointers (pointers to strings) as arguments.
o Use a loop to compare characters pointed to by str1 and str2:
If characters differ, return the difference (*str1 - *str2).
If both characters are \0 (end of strings), return 0 (indicating equality).
Increment both pointers after each comparison.
3. Input Strings
o Prompt the user to enter two strings.
4. Call Function
o Pass pointers to these strings to the compareStrings function.
5. Print Comparison Result
o Print whether the strings are equal or which one is lexicographically greater.
6. End
Result:
1. Define the Structure: Define a structure using the struct keyword. In this example,
let's define a structure called Student that holds information about a student's name,
roll number, and marks.
2. Declare and Initialize Structure Variables: Declare variables of the structure type
and initialize them with sample data.
3. Access Structure Members: Access structure members using the dot (.) operator to
set and display values.
Result:
Aim
1. Define the Union: Define a union using the union keyword. In this example, let's
define a union called Data that can store either an integer or a float.
2. Declare and Initialize Union Variables: Declare variables of the union type and
initialize them with sample data.
3. Access Union Members: Access union members using the dot (.) operator or the
member access operator (->) for pointers to set and display values.
Result:
To write a C program that creates a file (emp.rec) and stores information about a person
(name, age, and salary).
Steps and Explanation
1. Define the Structure: Define a structure Person with members for name (name), age
(age), and salary (salary).
2. Open the File: Open a file emp.rec in write mode ("w"). If the file doesn't exist, it
will be created. If it exists, its contents will be overwritten.
3. Write Data to File: Write the person's information to the file using fprintf().
4. Close the File: Close the file using fclose() to ensure all data is written properly.
Result:
Thus the Output Verified to create a file called emp.rec and store information about a person,
in terms of his name, age and salary.
To write a C program that lists all files and sub-directories in a specified directory.
1. Include Header Files: Include necessary header files for standard I/O operations
(stdio.h), directory operations (dirent.h), and string operations (string.h).
2. Define Main Function: Implement the main() function where you will:
o Declare a pointer to a DIR structure (dir) for handling directory streams.
o Declare a pointer to a struct dirent structure (entry) for reading directory
entries.
3. Open Directory: Use opendir() to open the directory specified by the user input.
4. Read Directory Contents: Use readdir() to read each entry in the directory. Check if
the entry is a file or a sub-directory using DT_REG and DT_DIR macros respectively.
5. Print Directory Contents: Print each file and sub-directory name.
6. Close Directory: Use closedir() to close the directory stream.
Result:
Thus the Output Verified to list all files and sub-directories in a directory.