Exercise 1) Write a program to calculate average temperature of five days.
Create temp ( )
function.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,d,e,ave;
float temp(float,float,float,float,float);
/clrscr();
printf("\nEnter The Five Days Temperature:\n");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
ave=temp(a,b,c,d,e);
printf("\n A Average of Temperature:%f",ave);
getch();
}
float temp(float a,float b,float c,float d,float e)
{
return((a+b+c+d+e)/5);
}
Output
Enter The Five Days Temperature:
23.7
32.5
34.1
31.5
36.4
A Average of Temperature:31.639999
Exercise 2) Write a program that uses recursive function fibo( ) that generate Fibonacci series
Containing N elements.
Solution :
#include <stdio.h>
// Fibonacci() funtion definition
int fibo(int num)
{
// first base condition check
if (num == 0)
{
return 0; // retuning 0, if condition meets
}
// second base condition check
else if (num == 1)
{
return 1; // returning 1, if condition meets
}
// else calling the fibonacci() function recursively till we get to the base conditions
else
{
return fibo(num - 1) + fibo(num - 2);
// recursively calling the fibonacc() function and then adding them
}
}
int main()
{
int num; // variable to store how many elements to be displayed in the series
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num); // taking user input
int i;
for (i = 0; i < num; i++)
{
printf("%d, ", fibo(i));
// calling fibonacci() function for each iteration and printing the returned value
}
return 0;
}
Output :
Enter the number of elements to be in the series : 13
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
Exercise 3) Write a program that uses recursive function fact ( ) that find the factorial of a given
number N.
Solution:
#include<stdio.h>
long int fact (int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int fact(int n)
{
if (n>=1)
return n*fact(n-1);
else
return 1;
}
Output:
Enter a positive integer: 8
Factorial of 8 = 40320
Exercise 4) Write a program to find the given number is prime or not. The function should
Accept the number as an argument and return if the no is prime or not.
Solution:
// C program to demonstrate whether a number is prime or not using function
#include <stdio.h>
#include<conio.h>
// Defining the function
int primenumber(int number)
{
int i;
// Condition for checking the given number is prime or not
for (i = 2; i <= number / 2; i++)
{
if (number % i != 0)
continue;
else
return 1;
}
return 0;
}
// Driver code
int main()
{
int num , res = 0;
printf("Enter any no to check for prime \n");
scanf("%d",&num);
// Calling the function
res = primenumber(num);
if (res == 0)
printf("%d is a prime number", num);
else
printf("%d is not a prime number", num);
}
Output
Enter any no to check for prime
33
33 is not a prime number
Exercise 5) Write a function which accepts a character array as argument from
user. The function should print ASCII equivalent of all characters in the string.
Solution:
#include <stdio.h>
void ascii_print(char str[])
{
int length=strlen(str);
int count=0;
while (count < length)
{
printf(" %c = %d\n", str[count], str[count] );
++ count ;
}
}
void main()
{
char string[20];
int n, count = 0;
printf("Enter the no of characters present in an array \n ");
scanf("%d", &n);
printf(" Enter the string of %d characters \n" , n);
scanf("%s", string);
ascii_print(string);
getch();
}
Output:
Enter the no of characters present in an array
7
Enter the string of 7 characters
college
c = 99
o = 111
l = 108
l = 108
e = 101
g = 103
e = 101
Exercise 6) Write a program to define a structure with tag State with fields state
name, number of districts and total population. Read and display the data.
Solution:
#include<stdio.h>
#include<conio.h>
struct state
{
char name[20];
int no_districts;
long population;
};
void main()
{
struct state s;
//clrscr();
printf("Enter name of state : ");
scanf("%s",s.name);
printf("\nEnter no of districts : ");
scanf("%d",&s.no_districts);
printf("\nEnter no of population : ");
scanf("%ld",&s.population);
printf("Entered data is \n");
printf("State name is - ");
puts(s.name);
printf("\nNo districts %d\n",s.no_districts);
printf("No of population is – %ld",s.population);
getch();
Output
Enter name of state : maharashtra
Enter no of districts : 34
Enter no of population : 1273772
Entered data is
State name is - maharashtra
No districts 34
No of population is – 1273772
Exercise 7) Write a program to create a list of books details. Details of book
include title, author, publisher, publishing year, number of pages and price.
Solution:
#include<stdio.h>
#include<conio.h>
struct Book
{
char title[20],author[20],publisher[20];
int pub_year,pages;
float price;
};
void main()
{
struct Book b1;
printf("Enter book title - ");
//scanf("%s",b1.title);
gets(b1.title);
printf("\nEnter author name -");
scanf("%s",b1.author);
printf("\nEnter publisher name -");
scanf("%s",b1.publisher);
printf("\nEnter publishing year - ");
scanf("%d",&b1.pub_year);
printf("\nEnter no of pages - ");
scanf("%d",&b1.pages);
printf("\nEnter price of book -");
scanf("%f",&b1.price);
printf("\n--------Entered data for book is ------\n ");
printf("\n Book title - %s",b1.title);
printf("\n Author name -%s",b1.author);
printf("\n Publisher year -%s",b1.publisher);
printf("\n Publishing year - %d",b1.pub_year);
printf("\n No of pages - %d",b1.pages);
printf("\n Price of book - %f",b1.price);
getch();
}
Output:
Enter book title - letus c
Enter author name -venugopal
Enter publisher name -microsoft
Enter publishing year - 2022
Enter no of pages - 233
Enter price of book -425
--------Entered data for book is ------
Book title - letus c
Author name -venugopal
Publisher year -microsoft
Publishing year - 2022
No of pages - 233
Price of book - 425.000000
Exercise 8) Define a structure called Item with members : Item_code,Item_name,
Price. Create an array of five Items. Create a function which accepts the Item
array and modifies each element with an increase of 10% in the price.
#include<stdio.h>
#include<conio.h>
struct Item
{
int code, price;
char name[20];
};
void update_price(struct Item a[5])
{
for(int i=0;i<5;i++)
{
a[i].price=a[i].price+((a[i].price*10)/100);
void main()
{
struct Item item_array[5];
for(int i=0;i<5;i++)
{
printf("\n--------Enter details of %d item------------\n",i+1);
printf("Enter Item code - ");
scanf("%d",&item_array[i].code);
printf("\nEnter Item name -");
scanf("%s",item_array[i].name);
printf("\n Enter item price -");
scanf("%d",&item_array[i].price);
}
update_price(item_array);
printf("\nItems with updated price is -");
for(int j=0;j<5;j++)
{
printf(" \n---------Data for %d item -------\n",j+1);
printf("\nItem Code is - %d",item_array[j].code);
printf("\nItem name is - %s",item_array[j].name);
printf("\nItem price is - %d",item_array[j].price);
}
getch();
}
Output
--------Enter details of 1 item------------
Enter Item code - 1
Enter Item name -abc
Enter item price -10
--------Enter details of 2 item------------
Enter Item code - 2
Enter Item name -def
Enter item price -20
--------Enter details of 3 item------------
Enter Item code - 3
Enter Item name -ghi
Enter item price -30
--------Enter details of 4 item------------
Enter Item code - 4
Enter Item name -jkl
Enter item price -40
--------Enter details of 5 item------------
Enter Item code - 5
Enter Item name -mno
Enter item price -50
Items with updated price is -
---------Data for 1 item -------
Item Code is - 1
Item name is - abc
Item price is - 11
---------Data for 2 item -------
Item Code is - 2
Item name is - def
Item price is - 22
---------Data for 3 item -------
Item Code is - 3
Item name is - ghi
Item price is - 33
---------Data for 4 item -------
Item Code is - 4
Item name is - jkl
Item price is - 44
---------Data for 5 item -------
Item Code is - 5
Item name is - mno
Item price is – 55
Exercise 9) Define a structure to represent a Date. Use your structure to accept two different
dates in the format mm dd of the same year. Write a C program to display the month name of
both dates.
Solution:
#include<stdio.h>
#include<conio.h>
struct Date
{
int dd,mm,year;
};
void month_name(struct Date d[2])
{
int i;
for(i=0;i<2;i++)
{
printf("\n Name of the %d month is \n",i+1);
switch(d[i].mm)
{
case 1: printf("January");
break;
case 2: printf("February");
break;
case 3: printf("March");
break;
case 4: printf("April");
break;
case 5: printf("May");
break;
case 6: printf("June");
break;
case 7: printf("July");
break;
case 8: printf("August");
break;
case 9: printf("September");
break;
case 10: printf("October");
break;
case 11: printf("November");
break;
case 12: printf("December");
break;
}
}
}
void main()
{
int i;
struct Date d[2];
//clrscr();
for(i=0;i<2;i++)
{
printf("\nEnter Date %d",i+1);
printf("\nEnter Day :-");
scanf("%d",&d[i].dd);
printf("\nEnter Month :-");
scanf("%d",&d[i].mm);
printf("\nEnter year :- ");
scanf("%d",&d[i].year);
}
printf("\n Months of given Date are :- ");
month_name(d);
getch();
}
OUTPUT:
Enter Date 1
Enter Day :-12
Enter Month :-2
Enter year :- 1983
Enter Date 2
Enter Day :-28
Enter Month :-8
Enter year :- 1987
Months of given Date are :-
Name of the 1 month is
February
Name of the 2 month is
August
Exercise 10) Define a structure that can describe a Hotel. It should have members that include
name, address, grade, room charges, no of rooms. Write a function to print out all hotel details
with room charges less than given value.
Solution:
#include<stdio.h>
#include<conio.h>
struct Hotel
{
char name[20],address[20], room_type[20];
int charges,no_rooms;
};
void search ( struct Hotel h[4] )
{
int price,i;
printf("\nEnter room charges for searching :- ");
scanf("%d",&price);
printf("\nHotel with price less than %d are :-\n",price);
for(i=0;i<4;i++)
{
if(h[i].charges<=price)
{
printf("\n %s \t %s \t %s \n",h[i].name,h[i].address,h[i].room_type);
printf("\n %d \t %d ",h[i].charges,h[i].no_rooms);
}
}
}
void main()
{
struct Hotel h[4];
int i;
for(i=0;i<4;i++)
{
printf("\n------------------Enter Details of %d hotel-----------------",i+1);
printf("\n Enter hotel %d name :- ",i+1);
scanf("%s",&h[i].name);
printf("\n Enter hotel %d address :- ",i+1);
scanf("%s",&h[i].address);
printf("\n Enter hotel %d Room type :- ",i+1);
scanf("%s",&h[i].room_type);
printf("\n Enter hotel %d Room charges :- ",i+1);
scanf("%d",&h[i].charges);
printf("\n Enter hotel %d No. of rooms :- ",i+1);
scanf("%d",&h[i].no_rooms);
}
for(i=0;i<4;i++)
{
printf("\n-------------Hotel %d details--------------- ",i+1 );
printf("\n Hotel Name : %s \n",h[i].name);
printf("\n Hotel address : %s \n",h[i].address);
printf("\n Hotel Room type : %s \n",h[i].room_type);
printf("\n Hotel charges : %d \n", h[i].charges);
printf("\n Hotel No. of rooms : %d \n",h[i].no_rooms);
}
search(h);
getch();
}
OUTPUT:
------------------Enter Details of 1 hotel-----------------
Enter hotel 1 name :- Taj
Enter hotel 1 address :- mumbai
Enter hotel 1 Room type :- ac
Enter hotel 1 Room charges :- 2000
Enter hotel 1 No. of rooms :- 1
------------------Enter Details of 2 hotel-----------------
Enter hotel 2 name :- ITC
Enter hotel 2 address :- Pune
Enter hotel 2 Room type :- AC
Enter hotel 2 Room charges :- 3000
Enter hotel 2 No. of rooms :- 2
------------------Enter Details of 3 hotel-----------------
Enter hotel 3 name :- sunset
Enter hotel 3 address :- delhi
Enter hotel 3 Room type :- ac
Enter hotel 3 Room charges :- 4200
Enter hotel 3 No. of rooms :- 2
------------------Enter Details of 4 hotel-----------------
Enter hotel 4 name :- VITS
Enter hotel 4 address :- sambhajinagar
Enter hotel 4 Room type :- ac
Enter hotel 4 Room charges :- 2100
Enter hotel 4 No. of rooms :- 1
-------------Hotel 1 details---------------
Hotel Name : Taj
Hotel address : mumbai
Hotel Room type : ac
Hotel charges : 2000
Hotel No. of rooms : 1
-------------Hotel 2 details---------------
Hotel Name : ITC
Hotel address : Pune
Hotel Room type : AC
Hotel charges : 3000
Hotel No. of rooms : 2
-------------Hotel 3 details---------------
Hotel Name : sunset
Hotel address : delhi
Hotel Room type : ac
Hotel charges : 4200
Hotel No. of rooms : 2
-------------Hotel 4 details---------------
Hotel Name : VITS
Hotel address : sambhajinagar
Hotel Room type : ac
Hotel charges : 2100
Hotel No. of rooms : 1
Enter room charges for searching 2200
Hotel with price less than 2200 are :-
Taj Mumbai ac
2000 1
VITS Sambhajinagar ac
2100 1
Exercise 11) Write a program to display contents of file on screen. Program should ask for file
name. Display file content in capital case.
Solution:
#include <stdio.h>
#include <stdlib.h> // For exit()
void main()
{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", toupper(c));
c = fgetc(fptr);
}
fclose(fptr);
getch();
}
Enter the filename to open
file.txt
THIS IS SENTENCE IN LOWER CASE
AFTER READING BY C PROGRAM SHOWN IN UPPER CASE
Exercise 12) Write a program to find the size of a file.
Solution:
// C program to find the size of file
#include <stdio.h>
int findSize(char file_name[])
{
// opening the file in read mode
FILE* fp = fopen(file_name, "r");
// checking if the file exist or not
if (fp == NULL) {
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
// calculating the size of the file
int result = ftell(fp);
// closing the file
fclose(fp);
return result;
}
// Driver code
int main()
{
char file_name[] = { "file.txt" };
int result = findSize(file_name);
if (result != -1)
printf("Size of the file is %d bytes \n", result);
return 0;
}
OUTPUT :
Size of the file is 81 bytes
Exercise 13) Write a program to combine the content of two files into third file.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Open two files to be merged
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
// Open file to store the result
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
puts("Could not open files");
exit(0);
}
// Copy contents of first file to file3.txt
while ((c = fgetc(fp1)) != EOF)
{
fputc(c, fp3);
}
// Copy contents of second file to file3.txt
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
OUTPUT
Merged file1.txt and file2.txt into file3.txt