KEMBAR78
Problem Solving Using C Lab Manual June 2025 | PDF | Pointer (Computer Programming) | Computer Program
0% found this document useful (0 votes)
18 views55 pages

Problem Solving Using C Lab Manual June 2025

Uploaded by

aashdeepsingh
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
18 views55 pages

Problem Solving Using C Lab Manual June 2025

Uploaded by

aashdeepsingh
Copyright
© © All Rights Reserved
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/ 55

LAB MANUAL

OF
PROBLEM SOLVING USING C
(UGCA1905)

COURSE: B.Sc.(Cyber Security)


SEMESTER: 1st
FACULTY: Ms. Rohini Mahajan
SESSION: July-Dec, 2025

PSC LAB MANUAL MS. ROHINI MAHAJAN


List of Experiments:

S.No. Experiment
1. Write a program to display your name. Write another program to print
message with inputted name.
2. Write a program to add two numbers.
3. Write a function to find largest of three numbers.
4. Write a program to calculate the average of three real numbers.
5. Write a Program to find ASCII value of a character.
6. Write a program to find the size of int, float, double and char.
7. Write a program to compute quotient and remainder.
8. Write a program to accept the values of two variables.
9. Write a program to find the simple interest, inputs are amount, period in
years and rate of interest.
10. Basic salary of an employee is input through the keyboard. The DA is 25% of
the basic salary while the HRA is 15% of the basic salary. Provident Fund is
deducted at the rate of 10% of the gross salary (BS+DA+HRA)
11. Write a program to find area of circle using PI as constant.
12. Write a program to find the volume of a cube using side as input from user.
13. Write a program using various unformatted input functions.
14. Write a program to find area of rectangle and print the result using
unformatted output
15 Write a program to find the large of two numbers.
16 Write a program to find the greatest of three numbers using nested if.
17 Write a program to find whether the given number is even or odd.
18 Write a program to generate a multiplication table using for loop.
19 Write a program to generate a multiplication table using while loop.
20 Write a program to make a calculator using switch case.
21 Write a program to find whether the given number is prime number or not.
22 Write a program using function to find the largest of three numbers.
23 Write a program using functions to print squares of first 20 numbers.
24 Write a program to find the factorial of the number.
25 Write a program to print the sum of two matrices.
26 Write a program to find the length of a string.
27 Write a program to copy string using strcpy().
28 Write a program to compare a String.
29 Write a program to reverse a String.
30 Write a program to multiply two numbers using Pointers.
31 Write a program to display address of a variable using Pointer.
PSC LAB MANUAL MS. ROHINI MAHAJAN
32 Write a program to show the memory occupied by Structure and Union.
33 Write a program to create Student I- Card using a Structure.
34 Write a program to read data from the file from a file.
35 Write a program to save Employees details in a file using File handling.

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 1
Aim: Write a program to display you name. Write another program to print
message with inputted name.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
#include <conio.h>
int main(){
charfname[30];
printf("Ramandeepsingh \n");
printf("Enter you name :- ");
scanf("%s", fname);
printf("hello, my nane is %s", fname);
return 0;
}
Output:-

SAMPLE VIVA-VOVE QUESTIONS:


1) Which type of language in C?
2) What is a User-defined Libraries.
3) What is a Compiler?
4) List some basic data types in C.

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 2

Aim: Write a program to add two numbers.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int numb1 ,numb2, sum;
printf("Enter the two number:- ");
scanf("%d %d", &numb1, &numb2);
sum = numb1 + numb2;
printf("%d + %d = %d", numb1, numb2, sum);
return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


1) What are the basic steps involved in adding two numbers in C
2) Explain the difference between using scanf and fgets to input numbers for addition in C.
3) How would you handle potential overflow when adding two integers in C?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 3

Aim: Write a program to find the square of the given number.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source code:
#include <stdio.h>
int main()
{
int number, squ;
printf("Enter a number to get a square:- ");
scanf("%d", &number);
squ = number * number;
printf("Here is the square of the enter number %d = %d",number, squ);
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1)what is Variable?
Q-2) what are the data types present in c?
Q-3) How to declare a Variable?
Q-4) How many types of operators or there in c?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 4

Aim: Write a program to calculate the average of three numbers.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int num1, num2, num3;
floatavg;
printf("Enter the three number which you have get the avg:- ");
scanf("%d %d %d" ,&num1, &num2, &num3);
avg= (num1+num2+num3)/3;
printf("\nAverage: %f", avg);
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain the purpose of each line in the program.
Q-2) Describe at least five different format specifiers?
Q-3) Define and explain scanf ( ) function?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 5

Aim: Write a program to find the ASCII value of the character.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>

int main()
{
charch = 'A';

printf("The ASCII value of %c is %d", ch, ch);


return 0;
}

Output:
The ASCII value of A is 65

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain what ASCII is and why it's important in programming.
Q-2) How does C represent characters internally, and how is this related to ASCII?
Q-3) Describe the printf function used to print the ASCII value of a character in your program.
Q-4) Explain the concept of character literals in C and how they relate to ASCII values
Q-5) What are the practical applications of knowing ASCII values in programming?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 6

Aim: Write a program to find the size of int, float, double and char.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
intintSize;
floatfloatSize;
doubledoubleSize;
charcharSize;
printf("Size of int: %zu bytes\n", sizeof(intSize));
printf("Size of float: %zu bytes\n", sizeof(floatSize));
printf("Size of double: %zu bytes\n", sizeof(doubleSize));
printf("Size of char: %zu byte\n", sizeof(charSize));
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Explain the output of the sizeof operator in C.
What is the significance of using sizeof(int) versus sizeof(float) in the program?
Discuss potential differences in size of float and double on different systems
How does the size of char relate to string handling in C?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 7

Aim: Write a program to compute quotient and remainder.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main(){
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain the difference between the division operator / and the modulus operator % in C.
Q-2) What happens if the divisor is zero in the program? How would you handle this scenario?
Q-3) Discuss the efficiency of computing quotient and remainder using division and modulus operations
versus alternative methods.
Q-4) Explain why the modulus operator % is useful in programming and provide examples of its practical
applications

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 8

Aim: Write a program to accept the value of two variables.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source Code:
#include<stdio.h>
int main() {
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %d\n", first);
printf("After swapping, secondNumber = %d", second);
return 0;}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain how scanf and printf functions work in C.
Q-2) What happens if the user enters a non-integer value when prompted to enter a value for var1 or
var2? How would you modify the program to handle such scenarios?

Q-3) Explain the significance of using & in scanf("%d", &var1); and &var2 in the program.

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 9

Aim: Write the program to find the simple interest, input are amount, period
in years and rate of interest.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
float principle, rate, sinterest;
int time;
printf("Enter Principle Amount, Rate %% per Annum and Time\n");
scanf ("%f %f %d", &principle, &rate, &time);
sinterest = (principle * rate * time)/ 100.0;
printf ("Principle Amount = %5.2f\n", principle);
printf ("Rate %% per Annum = %5.2f%\n", rate);
printf ("Time = %d years\n", time);
printf ("Simple Interest = %5.2f\n", sinterest);
return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain the purpose of using %f in scanf and printf functions for floating-point numbers in C.
Q-2) Discuss potential errors or bugs that could occur in this program and how you might resolve them.
Q-3) How would you modify the program to handle scenarios where the user enters negative values for
principal amount, rate of interest, or time period?
Q-4) Explain the significance of using %.2f in printf function to display the simple interest with two
decimal places.

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 10

Aim: Basic salary of an employee is input through the keyboard. The DA is


25% of the basic salary while the HRA is 15% of the basic salary. Provident
Fund is deducted at the rate of 10% of the gross salary(BS+DA+HRA). WRITE
A PROGRAM to calculate the net salary.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>

int main()
{
floatbasic_sal, da, hra, pf, gross_sal, net_sal;

printf("\nEnter the basic salary of the employee: Rs.");


scanf("%f", &basic_sal);

da = (basic_sal * 25) / 100;


hra = (basic_sal * 15) / 100;

gross_sal = basic_sal + da + hra;


pf = (gross_sal * 10) / 100;
net_sal = gross_sal - pf;

printf("Net salary: Rs. %.2f", net_sal);


return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain the significance of using float data type for variables gross_salary, tax_rate, insurance_amount,
and net_salary in the program.
Q-2) Discuss potential errors or bugs that could occur in this program and how you might resolve them.
Q-3) How would you modify the program to handle scenarios where the tax rate is a fixed amount rather

PSC LAB MANUAL MS. ROHINI MAHAJAN


than a percentage of the gross salary?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 11

Aim: Write a program to find area of circle using PI as constant?


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
#define PI 3.14159
int main(){
float sum=0;
float r;
printf("Enter the radius os the circle \n");
scanf("%f", &r);
sum = PI*r*r;
printf("theare of the circle is :-%f\n",sum);
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain the purpose and usage of #define directive in C programming.
Q-2) Discuss potential errors or bugs that could occur in this program and how you might resolve them.
Q-3) How would you modify the program to calculate and display the circumference of the circle in
addition to its area?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 12

Aim: Write a program to find volume of a cube using side as input from user.

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source Code:
#include <stdio.h>
int main(){
int side=5;
int volume=side*side*side;
printf(" ----------- ");
printf("The volume of the cube = %d",volume);
printf(" ----------- ");
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain why float is used for the variables side and volume instead of int.
Q-2) What would happen if the user enters a negative value for the side length of the cube? How could you
modify the program to handle this situation?
. Q-3) How would you extend this program to calculate and display both the surface area and the volume
of the cube?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 13

Aim: Write a program using various unformatted input function.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source Code:
#include<stdio.h>
#include<conio.h>
int main(){
intch;
printf("Running getch(), enter a character : ");
ch = getch();
printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");printf("Running getche(), enter a character : ");
ch = getche();
printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");
printf("Running a getchar(), enter a character : ");
ch = getchar();
printf("The character value you have just entered : %c", ch);
return 0;}
Output:

gets() : -
#include<stdio.h>
#include<conio.h>

PSC LAB MANUAL MS. ROHINI MAHAJAN


int main(){

PSC LAB MANUAL MS. ROHINI MAHAJAN


intch;
printf("Running getch(), enter a character : ");
ch = getch();
printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");printf("Running getche(), enter a character : ");
ch = getche();
printf("\n");
printf("The character value you have just entered : %c", ch);
printf("\n");
printf("Running a getchar(), enter a character : ");
ch = getchar();
printf("The character value you have just entered : %c", ch);
return 0;}
Output:

puts() : -
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
printf("Enter your name: ");
gets(name);
printf("Your name is: ");
puts(name);
return 0;
}

Output:

PSC LAB MANUAL MS. ROHINI MAHAJAN


SAMPLE VIVA-VOVE QUESTIONS:
Q-1 Discuss potential platform-specific issues you might encounter when using getch and getche functions.
How can you ensure portability of your code across different systems?
Q-2) Explain the usage and purpose of getch and getche functions in C programming. How do they differ
from standard input functions like scanf and getchar?
Q-3) Explain the potential risks associated with using getchar_unlocked. How can you ensure safe usage of
this function in multi-threaded applications?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 14

Aim: Write a program to find area of rectangle and print the result using
unformatted output.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include<stdio.h>
int main(){
floatlength,breadth;
float area;
printf(" Enter the Length of a Rectangle : ");
scanf("%f",&length);
printf("\n Enter the Breadth of a Rectangle : ");
scanf("%f",&breadth);
area = length * breadth;
printf("\n Area of Rectangle is : %f",area);
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:

Q-1 Explain the concept of unformatted output in C programming. How does printf function fit
into this concept?

Q-2) In what scenarios would knowing how to calculate the area of a rectangle be important in
programming applications?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 15

Aim: Write a program to find the larger of two numbers.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main() {
int a, b;
printf("Please Enter Two different values\n");
scanf("%d %d", &a, &b);
if(a > b) {
printf("%d is Largest\n", a);
}else if (b > a)
{
printf("%d is Largest\n", b);
} else
{
printf("Both are Equal\n");
}
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the logic behind comparing two numbers in C programming. How does the if-else structure
help in determining the larger number?
Q-2) What are potential challenges you might encounter when comparing floating-point numbers in C?
How can you address these challenges in your programs?
. Q-3) How would you modify the program to find the largest of three numbers instead of two?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 16

Aim: Write the program to find the greatest of the three number using nested
If.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int num1, num2, num3;

printf("Enter the number 1 : ");


scanf("%d", &num1);
printf("Enter the number 2: ");
scanf("%d", &num2);
printf("Enter the number 3 : ");
scanf("%d", &num3);

if (num1 >= num2 && num1 >= num3)


{
printf("The first is the greatest number");
}
if (num2 >= num1 && num2 >= num3)
{
printf("The second is the greatest number");
}
if (num3 >= num1 && num3 >= num2)
{
printf("The third is the greatest number");
}

return 0;
}
Output:

PSC LAB MANUAL MS. ROHINI MAHAJAN


SAMPLE VIVA-VOVE QUESTIONS:
Q-1 Explain the logic behind using nested if statements to find the largest of three numbers in C
programming. How does the nesting structure help in making comparisons?
Q-2) What are potential drawbacks of using nested if statements compared to other methods (e.g., using
arrays or sorting)? How can you optimize or refactor the code to handle more numbers efficiently?
. Q-3) How would you modify the program to find the smallest of three numbers instead of the largest?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 17

Aim: Write the program to find whether the given number is even or odd?
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int number;
printf("Enter any number to find even or odd: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("Entered number is Even.\n");
}
else
{
printf("Enter number is Odd.\n");
}
return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the logic behind using the modulus operator (%) to determine whether a number is even or
odd in C programming. How does it work?
Q-2) Discuss alternative approaches to determine whether a number is even or odd without using
modulus operator. When would you prefer these approaches over using modulus?
. Q-3) What are potential pitfalls or errors that could occur when using modulus operator to check for
evenness or oddness? How can you handle these situations in your programs?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 18

Aim: Write a program to generate the multiplication table using the for loop.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int number;
printf("Enter the number which you get multiple table: ");
scanf("%d", &number);

for (int i = 1; i <= 10; i++)


{
printf("%d X %d = %d\n", number, i, number * i);
}

return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain how the for loop is used to generate a multiplication table in this program. What are the
advantages of using a loop for this task?
Q-2) What modifications would you make to the program to generate multiplication tables up to a user-
specified limit (e.g., up to 12 times for each number)?
. Q-3) Discuss potential issues that could arise if the user enters a non-integer value (e.g., a floating-point
number) instead of an integer. How can you handle such cases in your program?
Q-4) How would you optimize this program to handle larger ranges of multiplication tables efficiently?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 19
Aim: Write a program to generate the multiplication table using the
while loop
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int number, i = 1;
printf("Enter the number which you get the multiplication table: ");
scanf("%d", &number);
printf("Using while loop\n");
while (i <= 10)
{
printf("%d X %d = %d\n", number, i, number * i);
i++;
}
return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the difference between using a for loop and a while loop to generate a multiplication table in C
programming. When would you prefer to use each loop construct?
Q-2)What modifications would you make to the program to generate multiplication tables up to a user-
specified limit (e.g., up to 12 times for each number) using a while loop?
. Q-3) How would you optimize this program to handle larger ranges of multiplication tables efficiently
using a while loop?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 20

Aim: Write a program to make a calculator using switch..case


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}

Output:
In the plus case: -
PSC LAB MANUAL MS. ROHINI MAHAJAN
In the subtract case: -

In the multiple case: -

In the divide case: -

SAMPLE VIVA-VOVE QUESTIONS:

Q-1 Discuss the advantages of using a switch statement over if-else statements for implementing a
calculator in C programming. Are there any limitations or considerations when using switch? Q-

2)How would you modify the program to handle additional operations such as exponentiation
(^), modulus (%), or square root (sqrt)?

Q-3) In what programming scenarios would knowing how to implement a calculator using switch
statements be important?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 21

Aim: WRITE A PROGRAM to find whether the given number is a prime


number
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter the positive interges: ");
scanf("%d", &n);

for (i = 2; i <= n / 2; i++)


{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite");
}
else
{
if (flag == 0)
{
printf("%d is a prime number", n);
}
else
{
printf("%d is not a prime number", n);
}
}

return 0;
}

PSC LAB MANUAL MS. ROHINI MAHAJAN


Output:
In case of prime is true value: -

In case of prime is false value: -

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the logic behind using a loop to determine if a number is prime in the isPrime function. Why
start the loop from 2 and check up to the square root of num?
Q-2) What are some potential optimizations you could make to the isPrime function to improve its
efficiency for large numbers?
. Q-3) Discuss the importance of handling edge cases (like numbers less than or equal to 1) when
implementing a prime number checking function.
Q-4) What steps would you take to ensure that the isPrime function handles negative numbers or non-
integer inputs gracefully?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 22

Aim: WRITE A PROGRAM using function to find the largest of three numbers.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
intlargestNumber (int a, int b, int c)
{
int largest = 0;
if (a > b && a > c)
largest = a;
else if (b > a && b > c)
largest = b;
else
largest = c;
return largest;
}
int main()
{
int a, b, c;

printf("Enter three numbers: ");


scanf("%d %d %d", &a, &b, &c);
printf("Largest number is: %d\n", largestNumber(a, b, c));

return 0;
}

Output:

PSC LAB MANUAL MS. ROHINI MAHAJAN


SAMPLE VIVA-VOVE QUESTIONS:
Q-1 Explain the significance of using a function (findMax) to determine the largest of three numbers. How
does this approach enhance code readability and maintainability?
Q-2) What modifications would you make to the findMax function to handle floating-point numbers instead
of integers?
. Q-3) Explain the differences between using int and double as data types in C programming. When would
you prefer to use double over int for numeric calculations?
Q-4) Discuss potential edge cases that you would test for when implementing a function to find the largest
of three numbers. How would you handle these cases in your program?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 23

Aim: WRITE A PROGRAM using function to print first 20 numbers and its
squares.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source Code:
#include <stdio.h>
int square(int a);
int main()
{
int i;
square(i);
}
int square(int n)
{
int i, squ;
for (i = 0; i <= 20; i++)
{
squ = (i * i);
printf("%d = %d\n", i, squ);
}
return 0;
}

Output:

PSC LAB MANUAL MS. ROHINI MAHAJAN


SAMPLE VIVA-VOVE QUESTIONS:
Q-1 Explain the role of the printNumbersAndSquares function in this program. What are the advantages of
encapsulating this functionality into a separate function?
Q-2) How would you modify the program to print the first 20 even numbers and their squares instead of
all numbers?
. Q-3) Discuss potential modifications you could make to the printNumbersAndSquares function to enhance
its functionality or performance.

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 24

Aim: WRITE A PROGRAM to find the factorial of a given number.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source Code:
#include <stdio.h>
int main()
{
int i, fact = 1, number;
printf("Enter a number: ");
scanf("%d", &number);
for (i = 1; i <= number; i++)
{
fact = fact * i;
}
printf("factorial of %d is : %d", number, fact);
return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Compare and contrast the iterative and recursive approaches to calculating the factorial of a number
in terms of performance, memory usage, and readability. When would you choose one approach over the
other?
Q-2) Discuss potential limitations of the factorial functions implemented here, such as handling very large
numbers or negative inputs. How would you modify the programs to address these limitations?
. Q-3) Explain the significance of using unsigned long long data type for storing factorial results in these

PSC LAB MANUAL MS. ROHINI MAHAJAN


programs. How does it differ from using int or long?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 25

Aim: WRITE A PROGRAM to print the sum of two matrices.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
int main()
{
int row, col, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &row);
printf("Enter the value of column (between 1 and 100): ");
scanf("%d", &col);
printf("\nEnter element of 1st matrix: \n");
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("Enter element a [%d] [%d]: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter element of 2st matrix: \n");
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("Enter element b [%d] [%d]: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}

for (i = 0; i < row; ++i)


{
for (j = 0; j < col; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("\nSum of two matrix: \n");
PSC LAB MANUAL MS. ROHINI MAHAJAN
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("%d ", sum[i][j]);
if (j == col - 1)
{
printf("\n\n");
}
}
}

return 0;
}
Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the significance of using functions (addMatrices and displayMatrix) in this program. How do
functions enhance code readability and maintainability?
Q-2) Discuss potential modifications you could make to the program to handle matrices of different
dimensions or data types (e.g., floating-point numbers).
. Q-3) How would you optimize the program to efficiently add large matrices (e.g., matrices with thousands
of rows and columns)?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 26

Aim: WRITE A PROGRAM to Find the Length of a String.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
charStr[100];
int i;

printf("Enter the string: ");


scanf("%s", Str);

printf("Lenght of Str is %ld", strlen(Str));


return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain potential edge cases you would consider when implementing the stringLength function. How
would you handle strings with special characters or empty strings?
Q-2) Discuss the significance of using a loop (while loop in this case) to find the length of a string in C. How
does the loop iterate through each character until it encounters the null terminator?
Q-3) Discuss differences between using char arrays and char pointers when working with strings in C.
When would you prefer to use each approach?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 27

Aim: WRITE A PROGRAM to Copy String using strcpy()


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "Ramandeep";
char s2[50];
strcpy(s2, s1);
printf("The first string is: %s\n", s1);
printf("The copied string: %s\n", s2);
return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain how the strcpy() function works in C. What are the advantages of using strcpy() over manually
copying each character of a string?
Q-2) Discuss potential risks or pitfalls of using strcpy() when working with strings in C. How can you
prevent buffer overflow or unintended behavior?
Q-3) How would you modify the program to copy only a part of the source string to the target string? For
example, copying the first 10 characters.
Q-4) Compare and contrast the use of strcpy() with other string manipulation functions like strncpy() and
memcpy(). When would you prefer to use each function?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 28

Aim: WRITE A PROGRAM to Compare a string.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100], i;

printf("Enter the first string: \n");


scanf("%s", a);

printf("Enter the second string: \n");


scanf("%s", b);

strcmp(a, b);

if (strcmp(a, b) == 0)
{
printf("The string are equal.\n");
}
else
{
printf("The string are not equal.\n");
}

return 0;
}

PSC LAB MANUAL MS. ROHINI MAHAJAN


Output:
Case 1: - when the two string is same.

Case 2: - when the two string is not equal.

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the significance of using a loop (while loop in this case) to compare strings in C. How does the
loop iterate through each character until a difference is found or the null terminator is reached?
Q-2) Discuss potential edge cases you would consider when implementing the compareStrings function.
How would you handle strings with different lengths or special characters?
Q-3) How would you modify the program to compare strings ignoring case sensitivity (i.e., consider
"Hello" and "hello" as equal)?
Q-4) What steps would you take to optimize the compareStrings function for efficiency, especially when
dealing with very large strings?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 29

Aim: WRITE A PROGRAM to Reverse a string.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char r[50];

printf("Enter the string to reverse\n");


scanf("%s", &r);

strrev(r);

printf("Reverse of the string: %s\n", r);


return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the approach used in the reverseString function to reverse a string in C. How does the
swapping mechanism work, and why does it iterate until start < end?
Q-2) Discuss potential edge cases you would consider when implementing the reverseString function. How
would you handle strings with special characters or an empty string?
Q-3 )What steps would you take to optimize the reverseString function for efficiency, especially when
dealing with very large strings?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 30

Aim: WRITE A PROGRAM to multiply two numbers using pointers


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source code:
#include <stdio.h>
int main()
{
int a, b, *p, *q, multi;
printf("enter the two number which you have get multiple:- ");
scanf("%d %d", &a, &b);

p = &a;
q = &b;

multi = *p * *q;

printf("the multiple value of %d , %d is %u", a, b, multi);


return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the use of pointers in the context of multiplying two numbers in C. How do pointers facilitate
indirect manipulation of data?
Q-2) Discuss potential advantages of using pointers for passing arguments to functions, as demonstrated
in the multiply function. How does passing by reference differ from passing by value in terms of memory
usage and efficiency?
Q-3 ) How would you modify the program to handle multiplication of floating-point numbers instead of
integers? What changes would be required in the function definition and input/output handling?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 31

Aim: WRITE A PROGRAM to display address of variable using pointers


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source code:
#include <stdio.h>
int main()
{
int a = 10;
int *p;
p = &a;
// here i use the pointer as a variable.
printf("the value of the a in the pointer is %u\n", p);

// here i use hte pointer as a star


printf("the value of the a in the pointer is %u", *p);
return 0;
}

Output:

SAMPLE VIVA-VOVE QUESTIONS:


Q-1 Explain the significance of using pointers to access the address of a variable in C. How does this
capability contribute to memory management and efficiency in programming?
Q-2) How would you modify the program to display the size of each variable (num, pi, ch) in bytes, using
the sizeof operator?
Q-3 ) What steps would you take to handle cases where a pointer contains a NULL value or an uninitialized
value when attempting to access variable addresses?
Q-4) How could you extend this program to dynamically allocate memory for a variable and then display
its address using pointers?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 32

Aim: WRITE A PROGRAM to show the memory occupied by Structure and


Union

Software Used: Tool :Turbo C/ gcc compiler


Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source code:
#include <stdio.h>
struct str1
{
int a;
float b;
};
union uin1
{
int x;
float y;
};

int main()
{
struct str1 Str = {45, 23.2};

union uin1 Uin;

Uin.x = 12;
Uin.y = 23.34;

printf("the size of the structure is: %d\n", sizeof(Str));


printf("the size of the union is: %d\n", sizeof(Uin));
return 0;
}

Output:

PSC LAB MANUAL MS. ROHINI MAHAJAN


PSC LAB MANUAL MS. ROHINI MAHAJAN
SAMPLE VIVA-VOVE QUESTIONS:
Q-1 Explain the concept of memory allocation in structures and unions in C. How does padding and
alignment affect the memory occupied by structures?
Q-2) Discuss the similarities and differences between structures and unions in C. When would you prefer
to use a structure over a union, and vice versa?
Q-3 ) How would you modify the program to calculate and print the sizes of individual members (empId,
empName, empSalary in the structure and integer, floating, character in the union)?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 33

Aim: WRITE A PROGRAM to create Student I-Card using a Structure.


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source code:
#include <stdio.h>
struct student
{
char name[50];
int roll;
int class;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter class: ");
scanf("%d", &s.class);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("class: %d\n", s.class);
return 0;
}
Output:

PSC LAB MANUAL MS. ROHINI MAHAJAN


SAMPLE VIVA-VOVE QUESTIONS:
Q-1 Explain how structures are used to organize and store related data in C. How does defining a
structure (struct Student in this case) help in managing and accessing multiple pieces of data
efficiently?
Q-2) How would you modify the program to include additional information on the student ID card, such
as address, contact number, and GPA? What changes would be required in the structure definition
and input/output handling?
Q-3 ) What steps would you take to ensure input validation and error handling when receiving user input
for student details?empName, empSalary in the structure and integer, floating, character in the union)?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 34

Aim: Write a program to read data from a file from a file


Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120
Source code:
#include <stdio.h>
#include <stdlib.h> // for exit() function
int main()
{
char c[1000];
FILE *fptr;

if ((fptr = fopen("include_file.txt", "r")) == NULL)


{
printf("Erro! opening file.");
exit(1);
}

fscanf(fptr, "%[^\n]", c);

printf("This is data from the another file:\n%s", c);

return 0;
}
Output:

In text file: -

PSC LAB MANUAL MS. ROHINI MAHAJAN


SAMPLE VIVA-VOVE QUESTIONS:

Q-1 Explain the significance of file pointers (FILE *) in C programming when dealing with files.
How are they used to facilitate reading and writing operations on files?

Q-2) Discuss potential error conditions that might occur when opening or reading from a file (fopen,
fgetc). How would you handle these errors to ensure robust file handling in your programs?

Q-3 ) What steps would you take to handle cases where the file size is very large or when dealing
with files containing binary data instead of text?

PSC LAB MANUAL MS. ROHINI MAHAJAN


EXPERIMENT- 35

Aim: Write a program to save Employee details in a file using file handling.
Software Used: Tool :Turbo C/ gcc compiler
Operating system : Windows 10 professional
Hardware : P-IV, RAM-2 GB , SSD 120

Source code:
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fptr;
char name[20];
charcompName[50];
floatsal;

fptr = fopen("emp.txt", "w");

if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}

printf("Enter the employee name:\n");


scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);

printf("Enter the company name:\n");


scanf("%s", compName);
fprintf(fptr, "Company Name = %s\n", compName);

printf("Enter the employee salary:\n");


scanf("%f", &sal);
fprintf(fptr, "salary = %f\n", sal);

fclose(fptr);
}

PSC LAB MANUAL MS. ROHINI MAHAJAN


Output:
In console side: -

In text file: -

SAMPLE VIVA-VOVE QUESTIONS:


Q-1) Explain the steps involved in file handling in C programming when writing data to a file. How does
opening a file in different modes ("w", "r", "a") affect file operations?
Q-2) Discuss potential error conditions that might occur when writing to a file (fopen, fprintf). How would
you handle these errors to ensure robust file handling in your programs?
Q-3 ) How would you modify the program to save multiple employee records into the same file? What
changes would be needed in the structure definition, input handling, and file output?
Q-4) What steps would you take to ensure data integrity and security when writing sensitive information
(such as employee details) to a file?

PSC LAB MANUAL MS. ROHINI MAHAJAN

You might also like