0% found this document useful (0 votes)
14 views138 pages

Final C Project 2

This document is a lab manual for the BCA-I (1st Semester) course at DAV College for Women, detailing various programming exercises in C for the academic session 2024-2025. It includes a comprehensive index of 150 programming tasks ranging from basic operations like printing 'Hello World' to more complex tasks involving data structures and file handling. The manual serves as a guide for students to practice problem-solving skills through coding in C.

Uploaded by

kayitis468
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)
14 views138 pages

Final C Project 2

This document is a lab manual for the BCA-I (1st Semester) course at DAV College for Women, detailing various programming exercises in C for the academic session 2024-2025. It includes a comprehensive index of 150 programming tasks ranging from basic operations like printing 'Hello World' to more complex tasks involving data structures and file handling. The manual serves as a guide for students to practice problem-solving skills through coding in C.

Uploaded by

kayitis468
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/ 138

DAV College for Women

Sector 36-A,
Chandigarh
DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS

SESSION 2024-2025

LAB MANUAL FOR BCA-I (1st SEMESTER)


SUBJECT NAME: PROBLEM SOLVING THROUGH C
SUBJECT CODE: BCA-103 (LAB BASED ON BCA-103P)
SUBMITTED TO: MS. RITIKA MA’AM (Assistant Professor)
SUBMITTED BY: SANSKRITI (6705)
INDEX
Sr. No. Program Page No.

1 Write a program to print hello world

2 Write a program to find SUM of three numbers

3 Write a program to find cube of a number

4 Write a program to calculate SUM difference and quotient

5 Write a program to calculate simple interest

6 Write a program to find average marks by four different


students

7 Write a program to find aggregate marks and percentage in


five subjects

8 Write a program to convert temperature into Celsius from


Fahrenheit

9 Write a program to find area and perimeter of a rectangle and


area and circumference of a circle

10 Write a program to print interchange of two numbers

11 Write a program to find the sum of first and fourth digit of four
digit number

12 Write a program to find the sum of all digits of five digit


number

13 Write a program to find the reverse of five digit number

14 Write a program to demonstrate automatic operators

15 Write a program to convert days into month

16 Write a program to print the interchange of two numbers using


third variable

17 Write a program to interchange of two numbers without using


third variable

18 Write a program to calculate compound interest

19 Write a program to demonstrate relational operator

20 Write a program to find greater of two numbers using


conditional operators

21 Write a program to check whether the number is positive or


not

2
22 In a company an employee is paid as under. If his basic salary
is less than Rs.1500 then HRA =10% of bs and DA = 90% of
bs. If his salary is either equal to or above Rs. 1500 then HRA

Sr. No. Program Page No.


23
=Rs. 500 and DA = 98% of basic salary. If the employee salary
is input through the keyboard, Write a program to find his
gross salary

24 Write a program to check whether the number is negative

25 Write a program to calculate tax at the rate 20%

26 Write a program to find bigger out of two numbers

27 Write a program to find whether the entered number is even or


odd

28 Write a program to find the biggest of three number using


nested if else

29 Write a program to find the biggest of three numbers using


logical operators

30 Write a program to find the division of a student in college

31 Write a program to find the day of the week using else if


statement

32 Write a program to give insurance of driver without logical


operator

33 Write a program to give insurance of driver with logical


operators

34 Write a program to find salary according to year of services


and qualification

35 Write a program to compute income tax for a given salary

36 Write a program to calculate profit or loss

37 Write a program to check whether the year is a leap year or not

38 Write a program to calculate gross salary with different values

39 Write a program to check whether number is a palindrome

40 If the age of Ram , Sham and Ajay are input through keyboard,
Write a program to determine the youngest of three

41 Write a program to check whether a triangle is valid or not

42 Write a program to check whether the coordinates lie on a


straight line or not

43 Write a program to check whether the coordinates lie on which


axis

3
44 Write a program to check the area is greater than perimeter

45 Write the program to print absolute value of enter number

46 According to the Gregorian calendar it was Monday on the


date 01/01/1900. If any year is input through the keyboard,

Sr. No. Program Page No.


47
Write a program to find out what is the day on first January of
this year

48 Write a program to find the position of coordinates in circle

49 Write a program to check whether the year is leap year or not


using logical operator

50 Write a program to find the division of the student in college


using else if

51 Write a program to print consecutive numbers from 0 to n

52 While purchasing certain items a discount of 10% is offered if


the quantity purchase is more than 1000 if quantity and price
per item are input through the keyboard, Write a program to
calculate the total expenses

53 Write a program to calculate a simple interest of three sets of


p,n,r

54 Write a program to display the consecutive digits from 0 - n

55 Write a program to find the factorial of a number

56 Write a program to compute raise to power

57 Write a program to find the sum of digits of a number

58 Any year is input through the keyboard. Write a program to


determine whether the year is a leap year or not. (Hint: Use the
% (modulus) operator)

59 In a company if an employee’s basic salary is less than or equal


to 10,000. Then his DA is 31% of the basic salary, HRA is 12%
of the basic salary, MA is Rs.250. if his salary is greater than
10,000 then DA is 35% of the basic salary, HRA is 15% of basic
and MA is Rs.350. Calculate his gross salary

60 The marks obtained by a student in 5 different subjects are input


through the keyboard. The student gets a division as per the
following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student
4
61 A five-digit number is entered through the keyboard. Write a
program to obtain the reversed number and to determine
whether the original and reversed numbers are equal or not

62 Write a program to find the reverse of an entered number

63 Write a program to check whether number is palindrome or


not using while loop

64 Write a program to print ascii values along with characters

65 Write a program to find the average of N numbers using do


while Loop

66 Write a program of execution for non-number of times using


old loop

67 Write a program to find factorial using do while loop

68 Write a program to calculate simple interest for three sets of


p,n,r

69 Write a program to print first eight multiple of N

70 Write a program to print the sum of a series 5+10+15+.......n

71 Write a program to calculate the average of N given numbers

72 Write a program to print fibonacci series

73 Write a program to print the table of N number

74 Write a program to print average of different list

75 Program to add first N terms of the following series

Sr. No. Program Page No.

1/1!+2/2!+3/3! +---n/n!

76 Write a program to print a star pattern

77 Write a program to find whether number is prime or not

78 Write a program to print all the prime numbers between 2 and


N

79 Write a program to calculate total price after discount

80 Write a program to check whether number is prime number or


not using break

81 Write a program to demonstrate continuous statement

82 Write a program to print reverse star pattern

83 Write a program to print a star pattern

5
84 Write a program that takes a number representing a day of the
week , prints the corresponding day name. If invalid input is
provided, display “invalid day”

85 Write a menu driven program which has following options-


factorial of number - prime or not - odd or even

86 Write a program to print factorial using for loop

87 Write a C program that prints the first 10 even number skipping


odd numbers

88 Write a C program that finds the sum of positive integers


entered by the user until a negative number is encountered

89 Write a program to find the grace marks for a student using


switch. The user should enter the class obtained by the
student and the number of subjects he/ she has failed in

90 Write a C program for calling a function in C

91 Write a C program for calling a function in C with complexity

92 Write a program to calculate some of three no’s

93 Write a program to convert lower case to upper case


character

94 Write a program to find factorial using function

95 Program to calculate the larger of two numbers

96 Write a program to demonstrate pointers value

97 Write a function power(a,b) to calculate the value of a raised t0


b

98 Write a program to Write a function to obtain the prime factors


of this number

99 Write a program for function calling from other user defined


function (function with no return type no argument).

100 Write a program using the keyword void. When a called function
should not return any value

101 Write a program to print the address of a number.

102 Write a program in which a variable that contains address of


another variable

103 Write a program in which a pointer that contains another


variable's address

104 Write a program in which a pointer that contains another


pointer' s address.

6
105 Any year is entered through the keyboard. Write a function to
determine whether the year is a leap year or not.

106 A positive integer is entered through the keyboard. Write a


function to obtain the prime factors of this number. For example,
prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of
35 are 5 and7.

107 Write a program using the recursive version of the function to


calculate the factorial value.

108 Write a function that receives 5 integers and returns the sum,
average and standard deviation of these numbers. Call this
function from main() and print the results in main().

109 Write a function that receives marks received by a student in 3


subjects and returns the average and percentage of these
marks. Call this function from main( ) and print the results in
main( )

Sr. No. Program Page No.

110 Write a function which receives a float and int from main(),
find the product of two

111 Write a program to calculate the average and the percentage


using function

112 Write a program to demonstrate call by value

113 Write a program to calculate average , sum and standard


deviation

114 Write a program to demonstrate call by reference

115 Write a program to demonstrate call by reference for multiple


result at one time

116 Write a program to find factorial using recursion

117 Write a program to generate the first N terms of Fibonacci


sequence by recursion

118 Write a program that compute ’a’ raised to power ‘b’ by using
recursion

119 Write a program to calculate sum of five digits number using


recursion

120 Write a program to find prime factors of a number using


recursion

7
121 Write a program to have tower of Hanoi

122 Write a program for static storage class

123 Write a program that takes 10 no’s and reverse it

124 Write a program to calculate marks of five different students


using array

125 Write a program to find the largest number and its position in a
list of N numbers

126 Write a program to interchange the even position elements


with odd position elements

127 Write a program to search a number from N numbers

128 Write a program to sort elements using bubble sort technique

129 Write a program to pass an array in function using call by


value

130 Write a program to pass an array in function, using call by


reference

131 Write a program to pass an entire array to a function

132 Write a program to access array elements

Sr. Program Page No.


No.
133 Write a program to enter data of 2D array and print the data
in matrix form

134 Write a program to find the sum of two matrices

135 Write a program to find the transpose of a square matrix

136 Write a program to calculate product of two matrices

137 Write a program to insert an element in an array

138 Write a program to delete an element from an array


Write a program to read and write your name using scanf() and
139
printf() in C.
Write a program to read and write strings in C using scanf()
140
and printf().
Write a program to read and write your name using gets() and
141
puts() in C.
Write a program to read and write strings in C using gets() and
142
puts().
Write a program to read your name using the getche() function.
143
Write a program to print your name using character I/O
144
functions (i.e., printf("%c")).

8
Write a program to read a string and count the number of
145
vowels and words present in the string.

Write a program to demonstrate the use of strcat() function.


146
Write a program to demonstrate the use of strcpy() function.
147
Write a program to demonstrate the use of strcmp() function.
148
Write a program to demonstrate the use of strlen() function
149
Write a program to demonstrate the use of strrev() function
150
Write a program to check whether the entered string is a
151
palindrome or not.
Write a program to demonstrate the use of strupr() function.
152
Write a program to demonstrate the use of strlwr() function.
153
Write a program to sort the characters of a string
154
alphabetically.
Passing structure to function in C (By value).
155
Passing structure to function in C (By address).
156
To declare a structure variable as global in C.
157
Union in C.
158
Write a program to write a text file
159
Write a program to Read from a text file
160
Write a program to to add a character in text file
161

9
Q. Write a program to print Hello World
Ans.
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Output:

Q. Write a program to find SUM of three numbers


Ans.
#include <stdio.h>
int main() {
int num1, num2, num3, sum;

10
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
sum = num1 + num2 + num3;
printf("The sum of the three numbers is: %d\n", sum);
return 0;
}
Output:

Q.Write a program to find cube of a number


Ans.
#include <stdio.h>
int main() {
int num, cube;
printf("Enter a number: ");
scanf("%d", &num);
cube = num * num * num;
printf("The cube of %d is: %d\n", num, cube);
return 0;
}

Output:

11
Q. Program to calculate SUM, difference, and quotient:
Ans.
#include <stdio.h>

int main() {
int num1, num2, sum, difference;
float quotient;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

sum = num1 + num2;


difference = num1 - num2;
quotient = (float)num1 / num2;

printf("Sum: %d\n", sum);


printf("Difference: %d\n", difference);
printf("Quotient: %.2f\n", quotient);

return 0;
}
Output:

12
Q. Program to calculate simple interest
Ans.
#include <stdio.h>
int main() {
float principal, rate, time, simple_interest;
printf("Enter principal, rate of interest, and time: ");
scanf("%f %f %f", &principal, &rate, &time);
simple_interest = (principal * rate * time) / 100;
printf("Simple Interest: %.2f\n", simple_interest);
return 0;
}
Output:

Q.Program to find average marks by four different students


Ans.
#include <stdio.h>
int main() {
float student1, student2, student3, student4, average;
13
printf("Enter marks of four students: ");
scanf("%f %f %f %f", &student1, &student2, &student3, &student4);
average = (student1 + student2 + student3 + student4) / 4;
printf("Average marks: %.2f\n", average);
return 0;
}
Output:

Q. Program to find aggregate marks and percentage in five


subjects
Ans.
#include <stdio.h>
int main() {
float subject1, subject2, subject3, subject4, subject5, aggregate,
percentage;

printf("Enter marks of five subjects: ");


scanf("%f %f %f %f %f", &subject1, &subject2, &subject3,
&subject4, &subject5);
aggregate = subject1 + subject2 + subject3 + subject4 + subject5;
percentage = (aggregate / 500) * 100;
printf("Aggregate Marks: %.2f\n", aggregate);
printf("Percentage: %.2f%%\n", percentage);

return 0;

14
}

Output:

Q. Program to convert temperature from Fahrenheit to Celsius.


Ans.
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
Output:

Q. Program to find area and perimeter of a rectangle and area


and circumference of a circle:

15
Ans.
#include <stdio.h>

int main() {
float length, width, radius, area_rectangle, perimeter, area_circle,
circumference;

printf("Enter length and width of the rectangle: ");


scanf("%f %f", &length, &width);
printf("Enter radius of the circle: ");
scanf("%f", &radius);

area_rectangle = length * width;


perimeter = 2 * (length + width);
area_circle = 3.14159 * radius * radius;
circumference = 2 * 3.14159 * radius;

printf("Area of rectangle: %.2f\n", area_rectangle);


printf("Perimeter of rectangle: %.2f\n", perimeter);
printf("Area of circle: %.2f\n", area_circle);
printf("Circumference of circle: %.2f\n", circumference);

return 0;
}
Output:

16
Q. Program to print interchange of two numbers:
Ans.
#include <stdio.h>

int main() {
int num1, num2, temp;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

temp = num1;
num1 = num2;
num2 = temp;

printf("After interchange: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
Output:

17
Q. Program to find the sum of the first and fourth digit of a
four-digit number:
Ans.
#include <stdio.h>

int main() {
int num, first_digit, fourth_digit, sum;

printf("Enter a four-digit number: ");


scanf("%d", &num);

first_digit = num / 1000;


fourth_digit = num % 10;
sum = first_digit + fourth_digit;

printf("Sum of first and fourth digit: %d\n", sum);

return 0;
}
Output:

18
Q. Program to find the sum of all digits of a five-digit number:
Ans. #include <stdio.h>

int main() {
int num, sum = 0, digit;

printf("Enter a five-digit number: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}

printf("Sum of all digits: %d\n", sum);

return 0;
}
Output:

19
Q. Program to find the reverse of a five-digit number:
Ans. #include <stdio.h>

int main() {
int num, reverse = 0, digit;

printf("Enter a five-digit number: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}

printf("Reversed number: %d\n", reverse);

return 0;
}
Output:

Q. Program to demonstrate automatic operators:


Ans.
#include <stdio.h>

20
int main() {
int a = 10, b = 20, c;

c = a + b;
printf("Addition: %d\n", c);

c = a - b;
printf("Subtraction: %d\n", c);

c = a * b;
printf("Multiplication: %d\n", c);

c = b / a;
printf("Division: %d\n", c);

c = b % a;
printf("Modulus: %d\n", c);

return 0;
}
Output:

21
Q. Program to convert days into months:
Ans.
#include <stdio.h>

int main() {
int days, months;

printf("Enter number of days: ");


scanf("%d", &days);

months = days / 30;

printf("%d days is approximately %d months\n", days, months);

return 0;
}
Output:

Q. Interchange of Two Numbers Using a Third Variable


Ans.
#include <stdio.h>

int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
22
b = temp;
printf("After Interchange: a = %d, b = %d\n", a, b);
return 0;
}
Output:

Q. Program to interchange two numbers without using a third


variable:
Ans.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}

Output:

23
Q. Program to calculate compound interest:
Ans.
#include <stdio.h>
int main() {
float principal, rate, time, ci;
printf("Enter principal, rate, and time: ");
scanf("%f %f %f", &principal, &rate, &time);
ci = principal;
for(int i = 0; i < time; i++) {
ci += ci * rate / 100;
}
printf("Compound Interest: %.2f\n", ci - principal);
return 0;
}
Output:

Q. Program to demonstrate relational operators:


Ans.
#include <stdio.h>
24
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("%d == %d is %d\n", a, b, a == b);
printf("%d != %d is %d\n", a, b, a != b);
printf("%d > %d is %d\n", a, b, a > b);
printf("%d < %d is %d\n", a, b, a < b);
printf("%d >= %d is %d\n", a, b, a >= b);
printf("%d <= %d is %d\n", a, b, a <= b);
return 0;
}
Output:

Q. Program to find the greater of two numbers using


conditional operators:
Ans.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int max = (a > b) ? a : b;
printf("Greater number is: %d\n", max);
25
return 0;
}

Output:

Q. Program to check whether a number is positive or not:


Ans.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num >= 0)
printf("The number is positive.\n");
else
printf("The number is negative.\n");
return 0;
}
Output:

Q. Program to calculate gross salary based on HRA and DA:


26
Ans.
#include <stdio.h>
int main() {
float bs, hra, da, gross;
printf("Enter basic salary: ");
scanf("%f", &bs);
if (bs < 1500) {
hra = 0.1 * bs;
da = 0.9 * bs;
} else {
hra = 500;
da = 0.98 * bs;
}
gross = bs + hra + da;
printf("Gross Salary = %.2f\n", gross);
return 0;
}

Output:

Q. Program to check whether the number is negative:


Ans.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
printf("The number is negative.\n");
else
27
printf("The number is not negative.\n");
return 0;
}

Output:

Q. Program to calculate tax at the rate of 20%:


Ans.
#include <stdio.h>
int main() {
float income, tax;
printf("Enter income: ");
scanf("%f", &income);
tax = 0.2 * income;
printf("Tax = %.2f\n", tax);
return 0;
}

Output:

Q. Program to find bigger out of two numbers:


28
Ans.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is bigger\n", a);
else
printf("%d is bigger\n", b);
return 0;
}

Output:

Q. Program to find whether the entered number is even or odd:


Ans.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("The number is even.\n");
else
printf("The number is odd.\n");
return 0;
}
Output:
29
Q. Program to find the biggest of three numbers using nested if-
else
Ans.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
printf("%d is the biggest\n", a);
else
printf("%d is the biggest\n", c);
} else {
if (b > c)
printf("%d is the biggest\n", b);
else
printf("%d is the biggest\n", c);
}
return 0;
}
Output:

Q. Program to find the biggest of three numbers using logical


30
operators:
Ans.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
printf("%d is the biggest\n", a);
else if (b > c)
printf("%d is the biggest\n", b);
else
printf("%d is the biggest\n", c);
return 0;
}
Output:

Q. Program to find the division of a student in college:


Ans.
#include <stdio.h>
int main() {
int marks;
printf("Enter the marks: ");
scanf("%d", &marks);
if (marks >= 60)
printf("First Division\n");
else if (marks >= 50)
printf("Second Division\n");
else if (marks >= 40)
printf("Third Division\n");
else
printf("Fail\n");
31
return 0;
}
Output:

Q. Program to find the day of the week using else-if:


Ans.
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7): ");
scanf("%d", &day);
if (day == 1)
printf("Monday\n");
else if (day == 2)
printf("Tuesday\n");
else if (day == 3)
printf("Wednesday\n");
else if (day == 4)
printf("Thursday\n");
else if (day == 5)
printf("Friday\n");
else if (day == 6)
printf("Saturday\n");
else if (day == 7)
printf("Sunday\n");
else
printf("Invalid input\n");
return 0;
}
Output:

32
Q.Program to give insurance of a driver without logical
operator:
Ans.
#include <stdio.h>
int main() {
int age, experience;
printf("Enter age and driving experience (years): ");
scanf("%d %d", &age, &experience);
if (age >= 25) {
if (experience >= 5)
printf("Driver is eligible for insurance\n");
else
printf("Driver is not eligible for insurance\n");
} else
printf("Driver is not eligible for insurance\n");
return 0;
}
Output:

Q. Program to give insurance of a driver with logical operators:


Ans.
#include <stdio.h>
int main() {
int age, experience;
33
printf("Enter age and driving experience (years): ");
scanf("%d %d", &age, &experience);
if (age >= 25 && experience >= 5)
printf("Driver is eligible for insurance\n");
else
printf("Driver is not eligible for insurance\n");
return 0;
}
Output:

Q. Program to find salary according to year of services and


qualification:
Ans.
#include <stdio.h>
int main() {
int experience, qualification;
float salary;
printf("Enter years of service and qualification (1 for Graduate, 2 for
Postgraduate): ");
scanf("%d %d", &experience, &qualification);
if (experience >= 10 && qualification == 2)
salary = 60000;
else if (experience >= 10 && qualification == 1)
salary = 50000;
else if (experience < 10 && qualification == 2)
salary = 40000;
else
salary = 30000;
printf("Salary = %.2f\n", salary);
return 0;
}
34
Output:

Q. Program to compute income tax for a given salary:


Ans.
#include <stdio.h>
int main() {
float salary, tax;
printf("Enter salary: ");
scanf("%f", &salary);
if (salary <= 250000)
tax = 0;
else if (salary <= 500000)
tax = 0.05 * (salary - 250000);
else if (salary <= 1000000)
tax = 12500 + 0.2 * (salary - 500000);
else
tax = 112500 + 0.3 * (salary - 1000000);
printf("Income tax = %.2f\n", tax);
return 0;
}
Output:

Q. Program to calculate profit or loss:


Ans.
#include <stdio.h>
int main() {
float cost_price, selling_price;
printf("Enter cost price and selling price: ");
35
scanf("%f %f", &cost_price, &selling_price);
if (selling_price > cost_price)
printf("Profit = %.2f\n", selling_price - cost_price);
else if (cost_price > selling_price)
printf("Loss = %.2f\n", cost_price - selling_price);
else
printf("No profit, no loss\n");
return 0;
}
Output:

Q. Program to check whether the year is a leap year or not:


Ans.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
Output:

36
Q. Program to calculate gross salary with different values:
Ans.
#include <stdio.h>
int main() {
float bs, hra, da, gross;
printf("Enter basic salary: ");
scanf("%f", &bs);
hra = 0.1 * bs;
da = 0.5 * bs;
gross = bs + hra + da;
printf("Gross salary = %.2f\n", gross);
return 0;
}
Output:

Q. Program to check whether a number is a palindrome:


Ans.
#include <stdio.h>
int main() {
int num, reversed = 0, original, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed)
printf("%d is a palindrome\n", original);
37
else
printf("%d is not a palindrome\n", original);
return 0;
}
Output:

Q. Program to determine the youngest of Ram, Sham, and Ajay:


Ans.
#include <stdio.h>
int main() {
int ram, sham, ajay;
printf("Enter the ages of Ram, Sham, and Ajay: ");
scanf("%d %d %d", &ram, &sham, &ajay);
if (ram < sham && ram < ajay)
printf("Ram is the youngest\n");
else if (sham < ram && sham < ajay)
printf("Sham is the youngest\n");
else
printf("Ajay is the youngest\n");
return 0;
}
Output:

Q. Program to check whether a triangle is valid or not:


38
Ans.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides of a triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a)
printf("The triangle is valid\n");
else
printf("The triangle is not valid\n");
return 0;
}
Output:

Q. Program to check whether the coordinates lie on a straight


line:
Ans.
#include <stdio.h>
int main() {
int x1, y1, x2, y2, x3, y3;
printf("Enter the coordinates of three points (x1, y1, x2, y2, x3, y3):
");
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
if ((y2 - y1) * (x3 - x2) == (y3 - y2) * (x2 - x1))
printf("The points lie on a straight line\n");
else
printf("The points do not lie on a straight line\n");
return 0;
}
Output:

39
Q. Program to check whether the coordinates lie on which axis:
Ans.
#include <stdio.h>
int main() {
int x, y;
printf("Enter the coordinates (x, y): ");
scanf("%d %d", &x, &y);
if (x == 0 && y == 0)
printf("The point is at the origin\n");
else if (x == 0)
printf("The point lies on the Y-axis\n");
else if (y == 0)
printf("The point lies on the X-axis\n");
else
printf("The point lies in a quadrant\n");
return 0;
}
Output:

Q. Program to check whether the area is greater than the


perimeter:
Ans.
#include <stdio.h>
int main() {
int length, breadth, area, perimeter;
printf("Enter length and breadth: ");
40
scanf("%d %d", &length, &breadth);
area = length * breadth;
perimeter = 2 * (length + breadth);
if (area > perimeter)
printf("Area is greater than perimeter\n");
else
printf("Area is not greater than perimeter\n");
return 0;
}
Output:

Q.Program to print absolute value of entered number:


Ans.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
num = -num;
printf("Absolute value = %d\n", num);
return 0;
}
Output:

Q. Program to find the day on 1st January of a given year:


41
Ans.
#include <stdio.h>
int main() {
int year, day;
printf("Enter the year: ");
scanf("%d", &year);
day = (year - 1900) + ((year - 1900) / 4) - ((year - 1900) / 100) +
((year - 1600) / 400);
day = day % 7;
if (day == 0)
printf("Monday\n");
else if (day == 1)
printf("Tuesday\n");
else if (day == 2)
printf("Wednesday\n");
else if (day == 3)
printf("Thursday\n");
else if (day == 4)
printf("Friday\n");
else if (day == 5)
printf("Saturday\n");
else if (day == 6)
printf("Sunday\n");
return 0;
}
Output:

Q. Program to find the position of coordinates in a circle:


Ans.
#include <stdio.h>
#include <math.h>
int main() {
42
int x, y, r;
printf("Enter the coordinates (x, y) and radius r of the circle: ");
scanf("%d %d %d", &x, &y, &r);
int distance = sqrt(x * x + y * y);
if (distance < r)
printf("The point lies inside the circle\n");
else if (distance == r)
printf("The point lies on the circle\n");
else
printf("The point lies outside the circle\n");
return 0;
}
Output:

Q. Program to check whether the year is a leap year using


logical operators:
Ans.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
Output:

43
Q. Program to find the division of the student in college using
else if:
Ans.
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 60)
printf("First Division\n");
else if (marks >= 45)
printf("Second Division\n");
else if (marks >= 33)
printf("Third Division\n");
else
printf("Fail\n");
return 0;
}
Output:

Q. Program to find the division of the student in college using


else if:
Ans.
#include <stdio.h>
44
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 60)
printf("First Division\n");
else if (marks >= 45)
printf("Second Division\n");
else if (marks >= 33)
printf("Third Division\n");
else
printf("Fail\n");
return 0;
}
Output:

Q. Program to print consecutive numbers from 0 to n:


Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 0; i <= n; i++) {
printf("%d ", i);
}
return 0;
}
Output:

45
Q. Program to calculate total expenses with discount:
Ans.
#include <stdio.h>
int main() {
int quantity;
float price, total;
printf("Enter quantity and price per item: ");
scanf("%d %f", &quantity, &price);
total = quantity * price;
if (quantity > 1000)
total = total * 0.9;
printf("Total expenses = %.2f\n", total);
return 0;
}
Output:

Q. Program to calculate simple interest for three sets of p, n, r:


Ans.
#include <stdio.h>
int main() {
float p, n, r, si;
for (int i = 1; i <= 3; i++) {
printf("Enter principal, time (in years) and rate of interest: ");
scanf("%f %f %f", &p, &n, &r);
si = (p * n * r) / 100;
printf("Simple Interest for set %d = %.2f\n", i, si);
46
}
return 0;
}
Output:

Q. Program to display the consecutive digits from 0 to n:


Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 0; i <= n; i++) {
printf("%d ", i);
}
return 0;
}
Output:

Q. Program to find the factorial of a number:


47
Ans.
#include <stdio.h>
int main() {
int n, factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("Factorial = %d\n", factorial);
return 0;
}
Output:

Q. Program to compute raise to power:


Ans.
#include <stdio.h>
int main() {
int base, exponent, result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exponent);
for (int i = 1; i <= exponent; i++) {
result *= base;
}
printf("Result = %d\n", result);
return 0;
}
Output:

48
Q. Any year is input through the keyboard. Write a program to
determine whether the year is a leap year or not. (Hint: Use the
% (modulus) operator)
Ans.
#include <stdio.h>
#include <conio.h>

int main() {
int year;
scanf("%d", &year);

if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("Leap Year\n");
} else {
printf("Not a Leap Year\n");
}
} else {
printf("Leap Year\n");
}
} else {
printf("Not a Leap Year\n");
}

getch();
return 0;
}
Output:
49
Q. In a company if an employee’s basic salary is less than or
equal to 10,000. Then his DA is 31% of the basic salary, HRA
is 12% of the basic salary, MA is Rs.250. if his salary is greater
than 10,000 then DA is 35% of the basic salary, HRA is 15% of
basic and MA is Rs.350. Calculate his gross salary.
Ans.
#include <stdio.h>
#include <conio.h>

int main() {
float basic_salary, DA, HRA, MA, gross_salary;
scanf("%f", &basic_salary);

if (basic_salary <= 10000) {


DA = 0.31 * basic_salary;
HRA = 0.12 * basic_salary;
MA = 250;
} else {
DA = 0.35 * basic_salary;
HRA = 0.15 * basic_salary;
MA = 350;
}

gross_salary = basic_salary + DA + HRA + MA;


printf("Gross Salary: %.2f\n", gross_salary);

getch();
return 0;
}
50
Output:

Q. The marks obtained by a student in 5 different subjects are


input through the keyboard. The student gets a division as per
the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the
student.
Ans.
#include <stdio.h>
#include <conio.h>

int main() {
float marks[5], total = 0, percentage;
for (int i = 0; i < 5; i++) {
scanf("%f", &marks[i]);
total += marks[i];
}

percentage = (total / 500) * 100;

if (percentage >= 60) {


printf("First Division\n");
} else if (percentage >= 50) {
printf("Second Division\n");
} else if (percentage >= 40) {
51
printf("Third Division\n");
} else {
printf("Fail\n");
}

getch();
return 0;
}
Output:

Q. A five-digit number is entered through the keyboard. Write a


program to obtain the reversed number and to determine
whether the original and reversed numbers are equal or not.
Ans.
#include <stdio.h>
#include <conio.h>

int main() {
int num, reversed = 0, original;
scanf("%d", &num);
original = num;

while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}

printf("Reversed Number: %d\n", reversed);


if (original == reversed) {
printf("Original and Reversed numbers are equal.\n");
52
} else {
printf("Original and Reversed numbers are not equal.\n");
}

getch();
return 0;
}
Output:

Q. Program to find the sum of digits of a number:


Ans.
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
53
Output:

Q. Program to find the reverse of an entered number:


Ans.
#include <stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed number = %d\n", reverse);
return 0;
}
Output:

Q. Program to check whether a number is palindrome or not


using while loop:
Ans.
#include <stdio.h>
int main() {
int num, reverse = 0, original, remainder;
printf("Enter a number: ");
54
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
if (original == reverse)
printf("Palindrome\n");
else
printf("Not a palindrome\n");
return 0;
}
Output:

Q. Program to print ASCII values along with characters:


Ans.
#include <stdio.h>
int main() {
char ch;
for (ch = 0; ch <= 127; ch++) {
printf("Character = %c, ASCII = %d\n", ch, ch);
}
return 0;
}
Output:

55
Q. Program to find the average of N numbers using do-while
loop:
Ans.
#include <stdio.h>
int main() {
int n, i = 1;
float num, sum = 0, avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
do {
printf("Enter number %d: ", i);
scanf("%f", &num);
sum += num;
i++;
} while (i <= n);
avg = sum / n;
printf("Average = %.2f\n", avg);
return 0;
}
56
Output:

Q. Program to find factorial using do-while loop:


Ans.
#include <stdio.h>
int main() {
int n, factorial = 1, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
do {
factorial *= i;
i++;
} while (i <= n);
printf("Factorial = %d\n", factorial);
return 0;
}
Output:

Q. Program to calculate simple interest for three sets of p, n, r:


57
Ans.
#include <stdio.h>
int main() {
float p, n, r, si;
for (int i = 1; i <= 3; i++) {
printf("Enter principal, time (in years) and rate of interest
for set %d: ", i);
scanf("%f %f %f", &p, &n, &r);
si = (p * n * r) / 100;
printf("Simple Interest = %.2f\n", si);
}
return 0;
}
Output:

Q. Program to print first eight multiples of N:


Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= 8; i++) {
58
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Output:

Q. Program to print the sum of a series 5 + 10 + 15 + ... + n:


Ans.
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 5; i <= n; i += 5) {
sum += i;
}
printf("Sum of the series = %d\n", sum);
return 0;
}
Output:

59
Q. Program to calculate the average of N given numbers:
Ans.
#include <stdio.h>
int main() {
int n, i;
float num, sum = 0, avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%f", &num);
sum += num;
}
avg = sum / n;
printf("Average = %.2f\n", avg);
return 0;
}
Output:

Q. Program to print Fibonacci series:


Ans.
#include <stdio.h>
60
int main() {
int n, a = 0, b = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
return 0;
}
Output:

Q.Program to print the table of N number:


Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Output:

61
Q. Program to print average of different lists:
Ans.
#include <stdio.h>
int main() {
int n, num, sum = 0;
float avg;
printf("Enter the number of lists: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("Enter numbers in list %d (Enter -1 to end):\n", i);
int count = 0;
while (1) {
scanf("%d", &num);
if (num == -1)
break;
sum += num;
count++;
}
avg = (float)sum / count;
printf("Average of list %d = %.2f\n", i, avg);
sum = 0;
}
return 0;
}
62
Output:

Q. Program to add first N terms of the series: 1/1! + 2/2! + 3/3!


+ ... + n/n!:
Ans.
#include <stdio.h>
int main() {
int n, i, j;
float sum = 0, fact;
printf("Enter the value of n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fact = 1;
for (j = 1; j <= i; j++) {
fact *= j;
}
sum += i / fact;
}
printf("Sum of the series = %.2f\n", sum);
return 0;
}
Output:

63
Q. Program to print a star pattern:
Ans.
#include<stdio.h>
int main()
{
int n,i,j,k;
printf("Enter the number of lines: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<=(n-i);j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
printf("*");
printf("\n");
}
return 0;
}

Output:

64
Q. Program to find whether a number is prime or not:
Ans.
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("Not prime\n");
return 0;
}
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
Output:

Q.Program to print all the prime numbers between 2 and N:


Ans.
#include <stdio.h>
65
int main() {
int n, i, j, flag;
printf("Enter the value of N: ");
scanf("%d", &n);
for (i = 2; i <= n; i++) {
flag = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("%d ", i);
}
}
return 0;
}
Output:

Q. Program to calculate total price after discount:


Ans.
#include <stdio.h>
int main() {
float price, discount, total_price;
printf("Enter the original price: ");
scanf("%f", &price);
printf("Enter the discount percentage: ");
scanf("%f", &discount);
total_price = price - (price * discount / 100);
printf("Total price after discount = %.2f\n", total_price);
66
return 0;
}
Output:

Q. Program to check whether a number is prime using break:


Ans.
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("Not prime\n");
return 0;
}
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
Output:
67
Q. Program to demonstrate the continue statement:
Ans.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5)
continue;
printf("%d ", i);
}
return 0;
}
Output:

Q. Program to print reverse star pattern:


Ans.
#include <stdio.h>

int main() {
int n, i, j, k;
printf("Enter the number of lines: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < i; j++)
printf(" ");
68
for (k = 0; k < (2 * (n - i) - 1); k++)
printf("*");
printf("\n");
}

return 0;
}
Output:

Q. Program to print a star pattern:


Ans.
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
69
Q. Program to print the corresponding day name for a given
number:
Ans.
#include <stdio.h>
int main() {
int day;
printf("Enter a number representing the day of the week (1-
7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid day\n"); break;
}
return 0;
}
Output:

70
Q. Menu-driven program for factorial, prime check, and
odd/even:
Ans.
#include <stdio.h>
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

int isPrime(int n) {
if (n <= 1)
return 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return 0;
}
return 1;
}

int main() {
int choice, num;
printf("Menu:\n1. Factorial\n2. Prime Check\n3. Odd or
Even\nEnter choice: ");
scanf("%d", &choice);
printf("Enter a number: ");
71
scanf("%d", &num);
switch (choice) {
case 1:
printf("Factorial of %d = %d\n", num, factorial(num));
break;
case 2:
if (isPrime(num))
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
break;
case 3:
if (num % 2 == 0)
printf("%d is even\n", num);
else
printf("%d is odd\n", num);
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Output:

72
Q.Program to print factorial using for loop:
Ans.
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial = %d\n", factorial);
return 0;
}
Output:

Q. Program to print the first 10 even numbers, skipping odd


numbers:
Ans.
#include <stdio.h>
int main() {
int count = 0;
for (int i = 1; count < 10; i++) {
if (i % 2 == 0) {
printf("%d ", i);
count++;
}
}
return 0;
}
Output:
73
Q. Program to find the sum of positive integers until a negative
number is entered:
Ans.
#include <stdio.h>
int main() {
int num, sum = 0;
while (1) {
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
break;
sum += num;
}
printf("Sum = %d\n", sum);
return 0;
}

Output:

74
Q. Program to find grace marks for a student using switch:
Ans.
#include <stdio.h>
int main()
{
int division, failed;
printf("Enter class (1, 2, 3) and number of failed subjects: ");
scanf("%d %d", &division, &failed);
switch (division) {
case 1:
if (failed <= 3)
printf("Grace marks = 5 per subject\n");
else
printf("No grace marks\n");
break;
case 2:
if (failed <= 2)
printf("Grace marks = 4 per subject\n");
else
printf("No grace marks\n");
break;
case 3:
if (failed == 1)
75
printf("Grace marks = 5\n");
else
printf("No grace marks\n");
break;
default:
printf("Invalid class\n");
}
return 0;
}
Output:

Q.Program for calling a function in C:


Ans.
#include <stdio.h>
void display() {
printf("This is a function call in C\n");
}
int main() {
display();
return 0;
}
Output:

Q. Program for calling a function in C with complexity:


76
Ans.
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d", &a, &b);
printf("Sum = %d\n", sum(a, b));
return 0;
}
Output:

Q.Program to calculate the sum of three numbers:


Ans.
#include <stdio.h>
int main() {
int a, b, c, sum;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
sum = a + b + c;
printf("Sum = %d\n", sum);
return 0;
}
Output:

77
Q.Program to convert lowercase to uppercase character:
Ans.
#include <stdio.h>
int main() {
char ch;
printf("Enter a lowercase character: ");
scanf(" %c", &ch);
if (ch >= 'a' && ch <= 'z') {
ch = ch - 32;
printf("Uppercase = %c\n", ch);
} else {
printf("Not a lowercase character\n");
}
return 0;
}
Output:

Q. Program to find factorial using a function:


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

Q. Program to calculate the larger of two numbers:


Ans.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("%d is larger\n", a);
else
printf("%d is larger\n", b);
return 0;
}
Output:
79
Q.Program to demonstrate pointer values:
Ans.
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", &a);
printf("Value of pointer p: %p\n", p);
printf("Value pointed by p: %d\n", *p);
return 0;
}
Output:

Q. Program to calculate power of a number using a function:


Ans.
#include <stdio.h>
int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
80
}
return result;
}
int main() {
int base, exp;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
printf("%d^%d = %d\n", base, exp, power(base, exp));
return 0;
}
Output:

Q. Write a program for function calling from other user defined


function (function with no return type)
Ans.
#include <stdio.h>

void functionA() {
printf("This is Function A\n");
}

void functionB() {
printf("This is Function B\n");
functionA();
}

int main() {
functionB();
return 0;
}
Output:
81
Q. Write a program for sending and receiving values between
functions.
Ans.
#include <stdio.h>

void sendValues(int a, int b) {


printf("Sending values: a = %d, b = %d\n", a, b);
}

int receiveValues() {
return 42;
}

int main() {
int x = receiveValues();
int y = 10;
sendValues(x, y);
return 0;
}
Output:

82
Q. Program to calculate the factorial of n
Ans.
#include <stdio.h>

int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Output:

Q. Program for function declaration and prototypes


Ans.
#include <stdio.h>

void displayMessage();

int main() {
displayMessage();
return 0;
}

void displayMessage() {
printf("Hello, this is a message from a function!\n");
83
}
Output:

Q. Program to calculate the square of any number (float)


Ans.
#include <stdio.h>

float square(float num) {


return num * num;
}

int main() {
float number;
printf("Enter a number: ");
scanf("%f", &number);
printf("Square of %.2f is %.2f\n", number, square(number));
return 0;
}
Output:

Q. Program using the keyword void


Ans.
#include <stdio.h>
84
void displayMessage() {
printf("This function does not return a value.\n");
}

int main() {
displayMessage();
return 0;
}
Output:

Q. Program to print the address of a number


Ans.
#include <stdio.h>

int main() {
int num = 10;
printf("Address of num: %p\n", (void*)&num);
return 0;
}
Output:

Q. Program with a variable that contains the address of another


85
variable
Ans.
#include <stdio.h>

int main() {
int a = 5;
int *ptr = &a;
printf("Address of a: %p\n", (void*)&a);
printf("Value of ptr: %p\n", (void*)ptr);
return 0;
}
Output:

Q. Program with a pointer that contains another variable's


address
Ans.
#include <stdio.h>

int main() {
int num = 20;
int *ptr = &num;
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", (void*)&num);
printf("Pointer ptr points to address: %p\n", (void*)ptr);
return 0;
}
Output:

86
Q. Program with a pointer that contains another pointer's
address
Ans.
#include <stdio.h>

int main() {
int num = 30;
int *ptr1 = &num;
int **ptr2 = &ptr1;
printf("Value of num: %d\n", num);
printf("Value pointed by ptr1: %d\n", *ptr1);
printf("Value pointed by ptr2 (ptr1 points to num): %d\n", **ptr2);
return 0;
}
Output:

Q. Program to determine whether a year is a leap year


Ans.
#include <stdio.h>

void checkLeapYear(int year) {


87
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
}

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
checkLeapYear(year);
return 0;
}
Output:

Q. Program to obtain the prime factors of a positive integer


Ans.
#include <stdio.h>

void primeFactors(int n) {
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
printf("%d ", i);
n /= i;
}
}
printf("\n");
}

88
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
printf("Prime factors of %d are: ", number);
primeFactors(number);
return 0;
}
Output:

Q. Program to obtain the prime factors of a number:


Ans.
#include <stdio.h>
void primeFactors(int n) {
while (n % 2 == 0) {
printf("2 ");
n /= 2;
}
for (int i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
printf("%d ", i);
n /= i;
}
}

89
if (n > 2)
printf("%d ", n);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Prime factors: ");
primeFactors(num);
return 0;
}
Output:

Q. Function to find the product of a float and an int:


Ans.
#include <stdio.h>
void product(float a, int b) {
printf("Product = %.2f\n", a * b);
}
int main() {
float x;
int y;
printf("Enter a float and an integer: ");
scanf("%f %d", &x, &y);
product(x, y);
return 0;
}
Output:

90
Q. Program to calculate average and percentage using a
function:
Ans.
#include <stdio.h>
void calculate(float marks[], int n, float *avg, float *percentage) {
float sum = 0;
for (int i = 0; i < n; i++) {
sum += marks[i];
}
*avg = sum / n;
*percentage = (sum / (n * 100)) * 100;
}
int main() {
int n;
printf("Enter number of subjects: ");
scanf("%d", &n);
float marks[n], avg, percentage;
printf("Enter marks: ");
for (int i = 0; i < n; i++) {
scanf("%f", &marks[i]);
}
calculate(marks, n, &avg, &percentage);
printf("Average = %.2f, Percentage = %.2f%%\n", avg, percentage);
return 0;
}
Output:

91
Q. Program to demonstrate call by value:
Ans.
#include <stdio.h>
void add(int a, int b) {
printf("Sum = %d\n", a + b);
}
int main() {
int x = 5, y = 10;
add(x, y);
return 0;
}
Output:

Q. Program to calculate average, sum, and standard deviation:


Ans.
#include <stdio.h>
void calculate(int arr[], int n, float *avg, float *stddev, int *sum)
{
*sum = 0;
for (int i = 0; i < n; i++) {
92
*sum += arr[i];
}
*avg = (float)(*sum) / n;
float variance = 0;
for (int i = 0; i < n; i++) {
variance += (arr[i] - *avg) * (arr[i] - *avg);
}
variance /= n;
*stddev = variance * 0.5; // Approximate standard deviation
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
float avg, stddev;
int sum;
calculate(arr, n, &avg, &stddev, &sum);
printf("Sum = %d, Average = %.2f, Standard Deviation =
%.2f\n", sum, avg, stddev);
return 0;
}
Output:

93
Q. Program to demonstrate call by reference:
Ans.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Output:

Q. Program to demonstrate call by reference for multiple results


at one time:
Ans.
#include <stdio.h>
94
void calculate(int a, int b, int *sum, int *diff) {
*sum = a + b;
*diff = a - b;
}
int main() {
int x = 15, y = 5, sum, diff;
calculate(x, y, &sum, &diff);
printf("Sum = %d, Difference = %d\n", sum, diff);
return 0;
}
Output:

Q. Program to find factorial using recursion:


Ans.
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
Output:

95
Q. Program to generate the first N terms of Fibonacci sequence
by recursion:
Ans.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int N;
printf("Enter the number of terms: ");
scanf("%d", &N);
printf("Fibonacci series: ");
for (int i = 0; i < N; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Output:

Q. Program to compute a raised to power b using recursion:


Ans.
#include <stdio.h>
96
int power(int a, int b) {
if (b == 0)
return 1;
return a * power(a, b - 1);
}
int main() {
int a, b;
printf("Enter base and exponent: ");
scanf("%d %d", &a, &b);
printf("%d^%d = %d\n", a, b, power(a, b));
return 0;
}
Output:

Q. Program to calculate sum of five digits number using


recursion:
Ans.
#include <stdio.h>
int sumDigits(int n) {
if (n == 0)
return 0;
return n % 10 + sumDigits(n / 10);
}
int main() {
int num;
printf("Enter a five-digit number: ");
scanf("%d", &num);
printf("Sum of digits = %d\n", sumDigits(num));
97
return 0;
}
Output:

Q. Program to find prime factors of a number using recursion:


Ans.
#include <stdio.h>
void primeFactors(int n, int i) {
if (n < 2)
return;
if (n % i == 0) {
printf("%d ", i);
primeFactors(n / i, i);
} else {
primeFactors(n, i + 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Prime factors: ");
primeFactors(num, 2);
return 0;
}
Output:

98
Q. Program to implement Tower of Hanoi:
Ans.
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char
aux_rod) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from %c to %c\n", n, from_rod,
to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
Output:

99
Q. Program for static storage class:
Ans.
#include <stdio.h>
void staticFunction() {
static int count = 0;
count++;
printf("Count = %d\n", count);
}
int main() {
for (int i = 0; i < 5; i++) {
staticFunction();
}
return 0;
}
Output:

100
Q. Program to take 10 numbers and reverse them:
Ans.
#include <stdio.h>
int main() {
int arr[10];
printf("Enter 10 numbers: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
printf("Reversed numbers: ");
for (int i = 9; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Output:

Q. Program to calculate marks of five different students using


an array:
Ans.
#include <stdio.h>
101
int main() {
float marks[5];
printf("Enter marks of 5 students: ");
for (int i = 0; i < 5; i++) {
scanf("%f", &marks[i]);
}
printf("Marks entered: ");
for (int i = 0; i < 5; i++) {
printf("%.2f ", marks[i]);
}
return 0;
}
Output:

Q. Program to find the largest number and its position in a list


of N numbers:
Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
102
int max = arr[0], pos = 0;
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
pos = i;
}
}
printf("Largest number is %d at position %d\n", max, pos +
1);
return 0;
}
Output:

Q. Program to interchange the even position elements with odd


position elements:
Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
103
scanf("%d", &arr[i]);
}
for (int i = 0; i < n - 1; i += 2) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
printf("Array after interchanging:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:

Q. Program to search a number from N numbers:


Ans.
#include <stdio.h>
int main() {
int n, search, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
104
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &search);
for (int i = 0; i < n; i++) {
if (arr[i] == search) {
printf("Number found at position %d\n", i + 1);
found = 1;
break;
}
}
if (!found) {
printf("Number not found.\n");
}
return 0;
}
Output:

Q. Program to sort elements using bubble sort technique:


Ans.
#include <stdio.h>
105
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:

106
Q. Program to pass an array in function using call by value:
Ans.
#include <stdio.h>
void printArray(int arr[], int n) {
printf("Array elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printArray(arr, n);
return 0;
}
Output:
107
Q. Program to pass an array in function using call by reference:
Ans.
#include <stdio.h>
void printArray(int arr[], int n) {
printf("Array elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printArray(arr, n);
return 0;
}
Output:

108
Q. Program to pass an entire array to a function:
Ans.
#include <stdio.h>
void printArray(int arr[], int n) {
printf("Array elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printArray(arr, n);
return 0;
}
Output:

109
Q. Program to access array elements:
Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Accessing array elements:\n");
for (int i = 0; i < n; i++) {
printf("Element at index %d: %d\n", i, arr[i]);
}
return 0;
}
Output:

110
Q. Program to enter data of a 2D array and print the data in
matrix form:
Ans.
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("Matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
111
printf("\n");
}
return 0;
}
Output:

Q. Program to find the sum of two matrices:


Ans.
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
112
int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Sum of matrices is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:

113
Q. Program to find the transpose of a square matrix:
Ans.
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n], transpose[n][n];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
transpose[j][i] = matrix[i][j];
}
}
printf("Transpose of the matrix is:\n");
114
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Output:

Q. Program to calculate the product of two matrices:


Ans.
#include <stdio.h>
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter rows and columns of first matrix: ");
scanf("%d%d", &rows1, &cols1);
printf("Enter rows and columns of second matrix: ");
scanf("%d%d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
int matrix1[rows1][cols1], matrix2[rows2][cols2], product[rows1]
[cols2];
printf("Enter elements of first matrix:\n");
115
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
product[i][j] = 0;
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("Product of matrices is:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
Output:

116
Q. Program to insert an element in an array:
Ans.
#include <stdio.h>
int main() {
int n, pos, element;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n + 1];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position to insert the element: ");
scanf("%d", &pos);
printf("Enter the element to insert: ");
scanf("%d", &element);
for (int i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
117
arr[pos - 1] = element;
printf("Array after insertion:\n");
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:

Q. Program to delete an element from an array:


Ans.
#include <stdio.h>
int main() {
int n, pos;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position of the element to delete: ");
scanf("%d", &pos);
for (int i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
118
}
printf("Array after deletion:\n");
for (int i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:

Q. Write a program to read and write your name using scanf()


and printf() in C.
Ans.
#include<stdio.h>
#include<conio.h>
int main()
{
char name[20];
printf("\n Enter your name please");
scanf("%s",name);
printf("\n Hello %s",name);

getch();
}
Output:

119
Q. Write a program to read and write strings in C using scanf()
and printf().
Ans.
#include <stdio.h>
int main()
{
char str[100];

printf("Enter a string: ");


scanf("%s", str); // Reads a single word (up to the first space)
printf("You entered: %s\n", str);
return 0;
}

Output:

Q. Write a program to read and write your name using gets()


and puts() in C.
Ans.
#include<stdio.h>
#include<conio.h>
int main()
120
{
char name[20];
gets(name);
puts(name);
getch();
}

Output:

Q. Write a program to read and write strings in C using gets()


and puts().
Ans.
#include <stdio.h>
#include <string.h>
int main()
{
char nickname[20];
puts("Enter your Nick name:");
gets(nickname);
puts(nickname);
return 0;
}
Output:

121
Q. Write a program to read and write your name using getche()
function.
Ans.
#include<stdio.h>
#include<conio.h>
int main()
{
char name[20];
int i,n=0;
printf("\n Enter your name and terminate with enter key \n");
while((name[n]=getche())!='\r')
n++;
name[n]='\0';
printf("\n You have entered \n");
for(i=0;i<n;i++)
putchar(name[i]);
getch();
}

Output:

Q. Write a program to print your name using character I/O


122
functions (i.e., printf("%c")).
Ans.
#include<stdio.h>
#include<conio.h>
int main()
{
char name[20];
int i=0;
printf("\n Enter your name ");
gets(name);
printf("\n Your name is...\n");
while(name[i]!='\0')
{
printf("%c",name[i]);
i++;
}
getch();
}

Output:

Q. Write a program to read a string and count the number of


vowels and words present in the string.
Ans.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[80],ch;
int vow,wr,i;
123
printf("\n Enter the line of text\n");
gets(str);
puts(str);
i=0;
vow=wr=0;
while((ch=str[i])!='\0')
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vow++;
else if(ch==' ')
wr++;
i++;
}
printf("\n The total number of vowels=%d",vow);
printf("\n The total number of words=%d",++wr);
getch();
}
Output:

Q. Write a program to demonstrate the use of strcat() function.


Ans.
#include <stdio.h>
#include <string.h>

int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";

strcat(str1, str2);
124
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:

Q. Write a program to demonstrate the use of strcpy() function.


Ans.
#include <stdio.h>
#include <string.h>

int main() {
char source[] = "Hello, World!";
char destination[50];

strcpy(destination, source);

printf("Source string: %s\n", source);


printf("Destination string: %s\n", destination);
return 0;
}

Output:

125
Q. Write a program to demonstrate the use of strcmp() function.
Ans.
#include <stdio.h>
#include <string.h>

int main() {
char str1[50], str2[50];

printf("Enter first string: ");


fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);

str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;

int result = strcmp(str1, str2);

if (result == 0) {
printf("The strings are equal.\n");
} else if (result < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}

return 0;
}

126
Output:

Q. Write a program to demonstrate the use of strlen() function.


#include <stdio.h>
#include <string.h>

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

str[strcspn(str, "\n")] = 0;

printf("Length of the string: %lu\n", strlen(str));


return 0;
}
Output:

Q. Write a program to demonstrate the use of strrev() function.


Ans.
#include <stdio.h>
127
#include <string.h>

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}

Output:

Q. Write a program to check whether the entered string is a


palindrome or not.
Ans.
#include <stdio.h>
#include <string.h>

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;
128
strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}
Output:

Q. Write a program to demonstrate the use of strupr() function.


Ans.
#include <stdio.h>
#include <string.h>

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

strupr(str);
printf("Uppercase string: %s\n", str);
return 0;
}

Output:

129
Q. Write a program to demonstrate the use of strlwr() function.
Ans.
#include <stdio.h>
#include <string.h>

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

strlwr(str);
printf("Lowercase string: %s\n", str);
return 0;
}
Output:

Q. Write a program to sort the characters of a string


alphabetically.
130
Ans.
#include <stdio.h>
#include <string.h>

int main() {
char str[100];
char temp;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

int length = strlen(str);

for (int i = 0; i < length - 1; i++) {


for (int j = i + 1; j < length; j++) {
if (str[i] > str[j]) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

printf("Sorted string: %s\n", str);


return 0;
}
Output:

131
Q. Passing structure to function in c
By value:
Ans.
#include <stdio.h>
#include <string.h>

struct student {
int id;
char name[20];
float percentage;
};

void func(struct student record);

int main() {
struct student record;

record.id = 1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

func(record);
return 0;
}

void func(struct student record) {


printf("Id is: %d\n", record.id);
printf("Name is: %s\n", record.name);
printf("Percentage is: %.2f\n", record.percentage);
}
Output:

132
Q. Passing structure to function in c
By address
Ans.
#include <stdio.h>
#include <string.h>

struct student {
int id;
char name[20];
float percentage;
};

void func(struct student *record);

int main() {
struct student record;

record.id = 1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

func(&record);
return 0;
}

void func(struct student *record) {


printf("Id is: %d\n", record->id);
printf("Name is: %s\n", record->name);
printf("Percentage is: %.2f\n", record->percentage);
}
133
Output:

Q. To declare a structure variable as


Global in c:
Ans.
#include <stdio.h>
#include <string.h>

struct student {
int id;
char name[20];
float percentage;
};

struct student record; // Global declaration of structure

void structure_demo();

int main() {
record.id = 1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

structure_demo();
return 0;
}

void structure_demo() {
printf("Id is: %d\n", record.id);
134
printf("Name is: %s\n", record.name);
printf("Percentage is: %.2f\n", record.percentage);
}
Output:

Q. Union
Ans.
#include <stdio.h>

union item {
int a;
float b;
char ch;
};

int main() {
union item it;
it.a = 12;
it.b = 20.2;
it.ch = 'z';
printf("%d\n", it.a);
printf("%f\n", it.b);
printf("%c\n", it.ch);
return 0;
}
Output:

135
******

Q. Write a program to write a text file

Ans.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num;
FILE *fptr;
fptr=fopen("program.txt","w");
if(fptr==NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}

Output:

136
**********

Q. Write a program to Read from a text file


Ans.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num;
FILE*fptr;
if((fptr=fopen("program.txt","r"))==NULL)
{
printf("Error! opening file");
//program exits if file pointer returns NULL
exit(1);
}
fscanf(fptr,"%d",&num);
printf("Value of n=%d",num);
fclose(fptr);
return 0;
}

Output:

137
Q. Write a program to to add a character in text file

Ans.
#include<stdio.h>
int main()
{
char ch;
FILE*fp;
if(fp=fopen("test.c","r"))
{
ch=getc(fp);
while(ch!=EOF)
{
putc(ch,stdout);
ch=getc(fp);
}
fclose;
return 0;
}
return 1;
}
Output:

138

You might also like