Final C Project 2
Final C Project 2
Sector 36-A,
Chandigarh
DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS
SESSION 2024-2025
11 Write a program to find the sum of first and fourth digit of four
digit number
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
40 If the age of Ram , Sham and Ajay are input through keyboard,
Write a program to determine the youngest of three
3
44 Write a program to check the area is greater than perimeter
1/1!+2/2!+3/3! +---n/n!
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”
100 Write a program using the keyword void. When a called function
should not return any value
6
105 Any year is entered through the keyboard. Write a function to
determine whether the year is a leap year or not.
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().
110 Write a function which receives a float and int from main(),
find the product of two
118 Write a program that compute ’a’ raised to power ‘b’ by using
recursion
7
121 Write a program to have tower of Hanoi
125 Write a program to find the largest number and its position in a
list of N numbers
8
Write a program to read a string and count the number of
145
vowels and words present in the string.
9
Q. Write a program to print Hello World
Ans.
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Output:
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:
Output:
11
Q. Program to calculate SUM, difference, and quotient:
Ans.
#include <stdio.h>
int main() {
int num1, num2, sum, difference;
float 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:
return 0;
14
}
Output:
15
Ans.
#include <stdio.h>
int main() {
float length, width, radius, area_rectangle, perimeter, area_circle,
circumference;
return 0;
}
Output:
16
Q. Program to print interchange of two numbers:
Ans.
#include <stdio.h>
int main() {
int num1, num2, temp;
temp = num1;
num1 = num2;
num2 = temp;
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;
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;
while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
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;
while (num != 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
return 0;
}
Output:
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;
return 0;
}
Output:
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:
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:
Output:
Output:
Output:
Output:
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:
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:
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:
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:
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:
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);
getch();
return 0;
}
50
Output:
int main() {
float marks[5], total = 0, percentage;
for (int i = 0; i < 5; i++) {
scanf("%f", &marks[i]);
total += marks[i];
}
getch();
return 0;
}
Output:
int main() {
int num, reversed = 0, original;
scanf("%d", &num);
original = num;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
getch();
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:
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:
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:
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:
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:
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:
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:
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:
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>
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:
void displayMessage();
int main() {
displayMessage();
return 0;
}
void displayMessage() {
printf("Hello, this is a message from a function!\n");
83
}
Output:
int main() {
float number;
printf("Enter a number: ");
scanf("%f", &number);
printf("Square of %.2f is %.2f\n", number, square(number));
return 0;
}
Output:
int main() {
displayMessage();
return 0;
}
Output:
int main() {
int num = 10;
printf("Address of num: %p\n", (void*)&num);
return 0;
}
Output:
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:
int main() {
int num = 20;
int *ptr = #
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 = #
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:
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
checkLeapYear(year);
return 0;
}
Output:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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];
Output:
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:
Output:
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
124
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
int main() {
char source[] = "Hello, World!";
char destination[50];
strcpy(destination, source);
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];
str1[strcspn(str1, "\n")] = 0;
str2[strcspn(str2, "\n")] = 0;
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:
int main() {
char str[100];
str[strcspn(str, "\n")] = 0;
int main() {
char str[100];
strrev(str);
printf("Reversed string: %s\n", str);
return 0;
}
Output:
int main() {
char str[100];
int main() {
char str[100];
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];
strlwr(str);
printf("Lowercase string: %s\n", str);
return 0;
}
Output:
int main() {
char str[100];
char temp;
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;
};
int main() {
struct student record;
record.id = 1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
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;
};
int main() {
struct student record;
record.id = 1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
return 0;
}
struct student {
int id;
char name[20];
float percentage;
};
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
******
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
**********
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