#Include #Include Struct Char Int Float Int Struct Int For
#Include #Include Struct Char Int Float Int Struct Int For
#include <stdio.h>
#include<string.h>
struct census
{
char city[50];
int population;
float li;
};
int main()
{
struct census c[10],temp;
int i,j;
for(i=0;i<10;i++)
{
printf("\nenter city name, population and literacy level=");
scanf("%s %d %f",c[i].city,&c[i].population,&c[i].li);
}
printf("\nEnterd Data:\n");
for(i=0;i<10;i++)
{
printf("%s\t %d\t %f\n",c[i].city,c[i].population,c[i].li);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(strcmp(c[i].city,c[j].city)<0)
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\nsorted by name of the city\n");
for(i=0;i<10;i++)
{
printf("%s\t %d\t %f\n",c[i].city,c[i].population,c[i].li);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(c[i].population < c[j].population)
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\nsorted by population of the city\n");
for(i=0;i<10;i++)
{
printf("%s\t %d\t %f\n",c[i].city,c[i].population,c[i].li);
}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(c[i].li < c[j].li)
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\nsorted by literacy level of the city\n");
for(i=0;i<10;i++)
{
printf("%s\t %d\t %f\n",c[i].city,c[i].population,c[i].li);
}
return 0;
}
2. Define a structure called cricket that will describe the following information:
(Player name, Team name, batting average)
Using cricket, declare an array player with 20 elements and write a program to read the
information about all the 20 players and print a team-wise list containing names of players
with their batting average.
#include<stdio.h>
#include<string.h>
struct cricket
{
char pname[20];
char tname[20];
float bavg;
};
int main()
{
struct cricket s[20],t;
int i,j,n=20;
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(strcmp(s[j-1].tname,s[j].tname)>0)
{
t=s[j-1];
s[j-1]=s[j];
s[j]=t;
}
}
}
3. Define a structure that can describe a hotel. It should have members that include the name,
address, grade, average room charge, and number of rooms. Write functions to perform the
following operations.
#include<stdio.h>
#include<string.h>
struct hotel
{
char name[15],add[10],grade;
int avg_charge,rooms;
};
void grade(char,int,struct hotel h1[]);
void charge(int n,int a,struct hotel h1[n]);
int main()
{
struct hotel h[8];
strcpy(h[0].name,"ashadeep");
strcpy(h[0].add,"surat");
h[0].grade='A',h[0].avg_charge=4000,h[0].rooms=20;
strcpy(h[1].name,"gateway");
strcpy(h[1].add,"baroda");
h[1].grade='B',h[1].avg_charge=6300,h[1].rooms=30;
strcpy(h[2].name,"taj");
strcpy(h[2].add,"mumbai");
h[2].grade='A',h[2].avg_charge=45000,h[2].rooms=60;
strcpy(h[3].name,"mountain");
strcpy(h[3].add,"surat");
h[3].grade='C',h[3].avg_charge=3000,h[3].rooms=10;
strcpy(h[4].name,"tgb");
strcpy(h[4].add,"goa");
h[4].grade='A',h[4].avg_charge=87000,h[4].rooms=90;
strcpy(h[5].name,"deep");
strcpy(h[5].add,"surat");
h[5].grade='B',h[5].avg_charge=65000,h[5].rooms=75;
strcpy(h[6].name,"amrut");
strcpy(h[6].add,"banglore");
h[6].grade='C',h[6].avg_charge=8000,h[6].rooms=40;
strcpy(h[7].name,"royalqueen");
strcpy(h[7].add,"surat");
h[7].grade='A',h[7].avg_charge=90000,h[7].rooms=50;
printf("enter grade");
char gd;
scanf("%c",&gd);
grade(gd,8,h);
int rp;
printf("enter charge");
scanf("%d",&rp);
charge(8,rp,h);
return 0;
}
void grade(char ch,int n,struct hotel h1[n])
{
struct hotel temp;
printf("\ngrade %c hotels\n",ch);
int i,j,k;
//k=n;
for(i=0;i<k;i++)
{
for(j=i+1;i<k;j++)
{
if(h1[i].avg_charge>h1[j].avg_charge)
{
temp=h1[i];
h1[i]=h1[j];
h1[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(h1[i].grade==ch)
{
printf("\n%s %s %d
%d\n",h1[i].name,h1[i].add,h1[i].avg_charge,h1[i].rooms);
}
}
return;
}
void charge(int n,int a,struct hotel h1[n])
{
printf("list of hotels having charge less than or equal to %d",a);
int i;
for(i=0;i<n;i++)
{
if(h1[i].avg_charge<=a)
{
printf("\n %s %s %c %d
%d",h1[i].name,h1[i].add,h1[i].grade,h1[i].avg_charge,h1[i].rooms);
}
}
return;
}
4. Create a structure called library to hold accession number, title of the book, author name,
price of the book, and flag indicating whether book is issued or not. Write a menu driven
program that depicts the working of a library. The menu options should be:
#include<stdio.h>
struct lib
{
int accession,flag;
float price;
char name[20],authname[20];
};
int main()
{
struct lib l[20];
int ch,i=0,acc,j=0,x;
char author[20];
while(x=1)
{
printf("What do u want to do?\n");
printf("1.Add book information\n");
printf("2.Display book information\n");
printf("3.List all books of given author\n");
printf("4.List the title of specified book\n");
printf("5.List the count of books in the library\n");
printf("6.List the books in the order of accession no\n");
printf("7.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the name of the book\n");
scanf("%s",l[j].name);
printf("Enter price\n");
scanf("%f",&l[j].price);
printf("press 0 if book is issued and 1if available\n");
scanf("%d",&l[j].flag);
printf("Record added successfully\n");
l[j].accession=j;
j++;
break;
}
case 2:
{
for(i=0;i<j;i++)
printf("%s",l[i].name);
printf(" ");
printf("%s",l[i].authname);
printf("%f ",l[i].price);
if(l[i].flag==0)
printf("Book is available\n");
else
printf("Book is not available\n");
break;
}
case 3:
{
printf("Enter the name of author\n");
for(i=0;i<j;i++)
{
if(l[i].authname==author)
{
printf("%s",l[i].name);
printf("\n");
}
}
break;
}
case 4:
{
printf("Enter the accession no. of the book\n");
scanf("%d",&acc);
for(i=0;i<j;i++)
if(l[i].accession==acc)
{
printf("%s",l[i].name);
printf("\n");
}
break;
}
case 5:
{
printf("%d",j);
break;
}
case 6:
{
for(i=0;i<j;i++)
{
printf("%s",l[i].name);
printf("\n");
}
break;
}
case 7:
{
x=2;
break;
}
}
}
return 0;
}
5. Write a function that can be called to find the largest element of an mXn matrix.
#include<stdio.h>
void print (int m, int n, int arr[m][n]);
void read(int m, int n, int arr[m][n]);
void Largest(int m, int n, int arr[m][n]);
int main()
{
int m,n;
printf("enter the value of m*n matrix value=");
scanf("%d %d",&m,&n);
int a[m][n];
printf("Enter array\n");
read (m,n, a);
print (m,n, a);
Largest(m,n,a);
return 0;
}
void print(int m, int n, int arr[m][n])
{
int i, j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void read(int m, int n, int arr[m][n])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("enter element [%d] [%d] ",i,j);
scanf("%d",&arr[i][j]);
}
}
}
void Largest(int m, int n, int arr[m][n])
{
int i,j;
int lg = arr[0][0];
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
if(arr[i][j]>lg)
{
lg = arr[i][j];
}
}
}
printf("Largest = %d\n", lg);
return;
}
6. Write a function called leap() that receives the year as a parameter and returns 0 if it is a
leap year, otherwise it returns 1.
#include<stdio.h>
int leap(int); // function prototype
int main()
{
int year;
return 0;
}
//function definition
int leap(int year)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
return 1;
else
return 0;
}
else
{
if(year % 4 == 0)
return 1;
else
return 0;
}
}
7. Write UDF that takes your birth date as input and display your age in terms year, month and
days.(Use Structure mentioned in Pro. 6)
#include <stdio.h>
void cal_age(int cur_date, int cur_month,int cur_year, int B_date,int B_month, int
B_year);
int main()
{
int cur_date,cur_month,cur_year,B_date,B_month,B_year ;
printf("enter current date in dd/mm/yyyy formate= ");
scanf("%d %d %d",&cur_date,&cur_month,&cur_year);
printf("enter birth date in dd/mm/yyyy formate= ");
scanf("%d %d %d",&B_date,&B_month,&B_year);
cal_age(cur_date, cur_month, cur_year,B_date, B_month, B_year);
return 0;
}
void cal_age(int cur_date, int cur_month,int cur_year, int B_date,int B_month, int
B_year)
{
int m[] = {31, 28, 31, 30, 31, 30, 31,31, 30, 31, 30, 31};
int cal_date,cal_month,cal_year;
if (B_date>cur_date)
{
cur_date=cur_date+m[B_month-1];
cur_month=cur_month-1;
}
if (B_month>cur_month)
{
cur_year=cur_year-1;
cur_month=cur_month+12;
}
cal_date=cur_date-B_date;
cal_month=cur_month-B_month;
cal_year=cur_year-B_year;
printf("Present Age\nYears: %d Months: %d Days: %d\n", cal_year,
cal_month,cal_date);
return;
}
8. Write an UDF that compares two given dates. If the dates are equal then it must return 0
else return 1. (Use Structure mentioned in Pro. 6)
#include <stdio.h>
int compare_date(int cur_date, int cur_month,int cur_year, int E_date,int E_month,
int E_year);
int main()
{
int cur_date,cur_month,cur_year,E_date,E_month,E_year ;
printf("enter first date in dd/mm/yyyy formate= ");
scanf("%d %d %d",&cur_date,&cur_month,&cur_year);
printf("enter second date in dd/mm/yyyy formate= ");
scanf("%d %d %d",&E_date,&E_month,&E_year);
if(compare_date(cur_date, cur_month, cur_year,E_date, E_month, E_year))
{
printf("date are equal");
}
else
{
printf("not equal");
}
return 0;
}
int compare_date(int cur_date, int cur_month,int cur_year, int E_date,int E_month,
int E_year)
{
if((cur_date==E_date)&&(cur_month==E_month)&&(cur_year==E_year))
return 1;
else
return 0;
}
9. Define a structure data type called time containing three members hour, minute, second.
Develop a UDF for following task:
a. create a function Input() to take input of the time from the user.
b. Create a function Display() to display the time entered by user.
c. Create a function Validate() that accepts one argument of time type
and return 0 if it is a valid, otherwise it returns 1.
#include<stdio.h>
void read(int m);
int check(int m);
struct time_struct
{
int hour,minute,second;
}time;
int main()
{
int a;
read(a);
if(check(a))
{
printf("invalid");
}
return 0;
}
void read(int m)
{
printf("\nEnter the time in hour, minute and second format: ");
scanf("%d%d%d",&time.hour,&time.minute,&time.second);
return;
}
int check(int m)
{
if(time.minute>=60 || time.second>=60 || time.hour>=24)
return 1;
else
{
printf("\nEntered Time is :");
if(time.hour<=9 && time.minute<=9 && time.second<=9)
printf("\n0%d:0%d:0%d",time.hour,time.minute,time.second);
else if(time.hour<=9 && time.minute>9 && time.second>9)
printf("\n0%d:%d:%d",time.hour,time.minute,time.second);
else if(time.hour>9 && time.minute<=9 && time.second>9)
printf("\n%d:0%d:%d",time.hour,time.minute,time.second);
else if(time.hour>9 && time.minute>9 && time.second<=9)
printf("\n%d:%d:0%d",time.hour,time.minute,time.second);
else if(time.hour<=9 && time.minute<=9 && time.second>9)
printf("\n0%d:0%d:%d",time.hour,time.minute,time.second);
else if(time.hour<=9 && time.minute>9 && time.second<=9)
printf("\n0%d:%d:0%d",time.hour,time.minute,time.second);
else if(time.hour>9 && time.minute<=9 && time.second<=9)
printf("\n%d:0%d:0%d",time.hour,time.minute,time.second);
else
printf("\n%d:%d:%d",time.hour,time.minute,time.second);
return 0;
}
}
#include<stdio.h>
#define MAX 100
int Max(int []);
int size;
int main()
{
int arr[MAX], max, i;
printf("\n\nEnter the size of the array: ");
scanf("%d", &size);
printf("\n\nEnter %d elements\n\n", size);
max = Max(arr);
printf("\n\nLargest element of the array is %d\n\n", max);
return 0;
}
i++;
Max(a);
}
return max;
}
11. Write a function to multiply two numbers by using recursion.
#include<stdio.h>
int Multiplication(int , int);
int main()
{
int a, b;
printf("Enter the two Number:");
scanf("%d%d",&a,&b);
printf("Multiplication of Two Number:%d",Multiplication(a,b));
return 0;
}
int Multiplication(int a, int b)
{
if(b!=0)
return (a+Multiplication(a, b-1));
else
return 0;
}
#include <stdio.h>
int power(int n1, int n2);
int main() {
int base, a, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &a);
result = power(base, a);
printf("%d^%d = %d", base, a, result);
return 0;
}
#include<stdio.h>
void fibonacciSeries(int range)
{
int a=0, b=1, c;
while (a<=range)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}
int main()
{
int range;
fibonacciSeries(range);
return 0;
}
14. Write a modular program that will perform the following tasks:
#include <stdio.h>
void input(int[], int);
void print(int[], int);
void asort(int[], int);
void merge(int a[],int n,int b[],int m);
int main()
{
printf("\n 1st array");
int m[10];
input(m, 10);
print(m, 10);
printf("\n 2nd array");
int n[10];
input(n, 10);
print(n, 10);
printf("\n sorting of 1st array");
asort(m, 10);
printf("\n Result after sorting \n");
print(m, 10);
printf("\n sorting of 2nd array");
asort(n, 10);
printf("\n Result after sorting \n");
print(n, 10);
printf("\n Result after merging \n");
merge(m,10,n,10);
asort(m,20);
printf("\n Result after sorting \n");
print(m, 20);
return 0;
}
void input(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("\n enter vaue fot element %d ", i);
scanf("%d", &a[i]);
}
return;
}
void print(int b[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("\t %d ", b[i]);
}
return;
}
void asort(int a[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return ;
}
//declaration of the function marge
void merge(int a[],int n,int b[],int m)
{
int i=0,j=0;
while(i<n)
{
i++;
}
int o=n+m;
while(j<o)
{
a[i]=b[j];
i++;
j++;
}
for (i = 0; i < o; i++)
{
printf(" %d ", a[i]);
}
return;
}
#include <stdio.h>
int add(int);
int main()
{
int n, result;
printf("Enetr number:");
scanf("%d", &n);
result = add(n);
printf("SUM=%d", result);
return 0;
}
int add(int n) {
if (n == 0) // Base case
return 0;
#include<stdio.h>
int factorial(int);
int main()
{
int f;
printf("Enter number:");
scanf("%d",&f);
printf("%d",factorial(f));
return 0;
}
int factorial(int a)
{
if(a==1)
return 1;
else
return a=a*factorial(a-1);
}
#include<stdio.h>
int Multiplication(int , int);
int main()
{
int a, b;
printf("Enter the two Number:");
scanf("%d%d",&a,&b);
printf("Multiplication of Two Number:%d",Multiplication(a,b));
return 0;
}
int Multiplication(int a, int b)
{
if(b!=0)
return (a+Multiplication(a, b-1));
else
return 0;
}
18. Write a function to count total number of elements that hold prime numbers in two
dimensional array.
#include<stdio.h>
void print (int m, int n, int arr[m][n]);
void read(int m, int n, int arr[m][n]);
void count(int m, int n, int arr[m][n]);
int main()
{
int a[3][3];
printf("Enter array\n");
read (3,3, a);
print (3,3, a);
printf("\n prime number\n");
count(3,3,a);
return 0;
}
void print(int m, int n, int arr[m][n])
{
int i, j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void read(int m, int n, int arr[m][n])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("enter element [%d] [%d] ",i,j);
scanf("%d",&arr[i][j]);
}
}
}
void count(int m, int n, int arr[m][n])
{
int i,j,k;
int flag=0,count=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(k=2;k<n;k++)
{
flag=0;
if(arr[i][j]%k==0)
{
flag++;
}
}
if(flag==1)
{
count++;
}
}
}
printf("\n prime=%d",count);
return;
}
19. Write a function to calculate a sum of all elements in two dimensional array.
#include<stdio.h>
void print (int m, int n, int arr[m][n]);
void read(int m, int n, int arr[m][n]);
void sum(int m, int n, int arr[m][n]);
int main()
{
int a[3][3];
printf("Enter array\n");
read (3,3, a);
print (3,3, a);
printf("\n sum of all elements\n");
sum(3,3,a);
return 0;
}
void print(int m, int n, int arr[m][n])
{
int i, j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void read(int m, int n, int arr[m][n])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("enter element [%d] [%d] ",i,j);
scanf("%d",&arr[i][j]);
}
}
}
void sum(int m, int n, int arr[m][n])
{
int i,j;
int sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=sum+arr[i][j];
}
}
printf("\n sum=%d",sum);
return;
}
#include<stdio.h>
void print (int m, int n, int arr[m][n]);
void read(int m, int n, int arr[m][n]);
void asort(int m, int n, int arr[m][n]);
void dsort(int m, int n, int arr[m][n]);
int main()
{
int a[3][3];
printf("Enter array\n");
read (3,3, a);
print (3,3, a);
asort(3,3,a);
printf("\nAfter sorting in ascending order\n");
print(3,3,a);
dsort(3,3,a);
printf("\nAfter sorting in discending order\n");
print(3,3,a);
return 0;
}
void print(int m, int n, int arr[m][n])
{
int i, j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void read(int m, int n, int arr[m][n])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0; j<n; j++)
{
printf("enter element [%d] [%d] ",i,j);
scanf("%d",&arr[i][j]);
}
}
}
void asort(int m, int n, int arr[m][n])
{
int k=0,array[m*n],i,j,temp;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
array[k]=arr[i][j];
k++;
}
}
for(i=0;i<m*n;i++)
{
for(j=i+1;j<m*n;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
arr[i][j]=array[k];
k++;
}
}
return;
}
void dsort(int m, int n, int arr[m][n])
{
int k=0,array[m*n],i,j,temp;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
array[k]=arr[i][j];
k++;
}
}
for(i=0;i<m*n;i++)
{
for(j=i+1;j<m*n;j++)
{
if(array[i]<array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
arr[i][j]=array[k];
k++;
}
}
return;
}