0% found this document useful (0 votes)
21 views12 pages

Lab Manual by Prema For C Program

Uploaded by

Prema Saravanan
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)
21 views12 pages

Lab Manual by Prema For C Program

Uploaded by

Prema Saravanan
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
You are on page 1/ 12

1.

Write a C 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.

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.

2. Write a C program to input two numbers and display the


maximum number
Aim

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

3. Write a C program to find whether a character is consonant


or vowel using switch statement.

Aim

To develop a program that determines whether a given character is a consonant or a vowel


using a switch statement (or its equivalent in languages that do not support switch
statements).

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

To develop a program that prints the positive integers from 1 to 10.

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:

Thus the Output Verified to print positive integers from 1 to 10

5. Write a C program to check whether a number is


Palindrome or not.
Aim

To develop a program that checks whether a given number is a palindrome or not.

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.

6. Write a C program to generate Fibonacci series.


To develop a program that generates the Fibonacci series up to a specified number of terms.

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:

Thus the Output Verified to generate Fibonacci series

7. Write a C program to insert 5 elements into an array and


print the elements of the array.
Aim

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.

8. Write a C Program to reverse the array elements in C


Programming
Aim

To write a C program that reverses the elements of an 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:

Thus the Output Verified to reverse the array elements in C Programming

9. Write a C program to concatenate two strings


Aim

To write a C program that concatenates two strings.

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:

Thus the Output Verified program to concatenate two strings

10. Write a C program to compare two strings without


using string library functions.
Aim

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

11. Write a C program to generate Fibonacci series using


recursive function
Aim

To write a C program that generates Fibonacci series using a recursive function.


Algorithm

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

12. Write a C program to find power of any number using


recursion.
Aim

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.

13. Write a C program to add, subtract, multiply and


divide two integers using user defined type function with
return type
Aim
To write a C program that adds, subtracts, multiplies, and divides two integers using user-
defined functions with a return type.
Algorithm
1. Start
2. Declare Functions
o Define four functions:
 int add(int a, int b) to add two integers and return the result.
 int subtract(int a, int b) to subtract the second integer from the first and
return the result.
 int multiply(int a, int b) to multiply two integers and return the result.
 float divide(int a, int b) to divide the first integer by the second and
return the result as a float.
3. Input Integers
o Prompt the user to enter two integers.
4. Perform Operations
o Call each function with the input integers to perform the respective arithmetic
operation.
5. Print Results
o Print the results of each arithmetic operation.
6. End

Result:

Thus the Output Verified to add, subtract, multiply and divide two integers using user defined
type function with return type

14. Write a C program to swap two integers using call by


value and call by reference methods of passing arguments to
a function
Aim
To write a C program that swaps two integers using both call by value and call by reference
methods.
Algorithm
1. Start
2. Function for Call by Value
o Define a function void swapByValue(int a, int b) that swaps the values of a
and b using a temporary variable.
3. Function for Call by Reference
o Define a function void swapByReference(int *a, int *b) that swaps the values
of a and b using pointers.
4. Input Integers
o Prompt the user to enter two integers.
5. Call Functions
o Call swapByValue with the input integers to swap them using call by value.
o Call swapByReference with the addresses of the input integers to swap them
using call by reference.
6. Print Results
o Print the swapped integers to verify the results for both methods.
7. End

Result:

Thus the Output Verified to swap two integers using call by value and call by reference
methods of passing arguments to a function

15. Write a C program to find biggest among three


numbers using pointer.
Aim

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

To write a C program that compares two strings using pointers.

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:

Thus the Output Verified to compare two strings using pointers..

17. Write a C program to create, declare and initialize


structure
Aim

To write a C program that creates, declares, and initializes a structure.

Steps and Explanation

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:

Thus the Output Verified to create, declare and initialize structure..

18. Write a C program to declare, initialize an UNION.

Aim

To write a C program that declares, initializes, and uses a union.

Steps and Explanation

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:

Thus the Output Verified to declare, initialize an UNION..

19. Write a C program to create a file called emp.rec and


store information about a person, in terms of his name, age
and salary.
Aim

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.

20. Write a C program to list all files and sub-directories


in a directory.
Aim

To write a C program that lists all files and sub-directories in a specified directory.

Steps and Explanation

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.

You might also like