Sunday, 24 April 2022
7. Create a structure student with rollno (int), name (char array), marks (int), grade (char). Create an array stu[5] of type student. Take the details of students like rollno, name, and marks from the user. Call UDF prepare_result() which will store the values for grade based on marks (if marks >=75 then ‘A’, Between 60 to 75 ‘B’, Between 50 to 60 ‘C’, Between 35 to 50 ‘D’ and Below 35 ‘F’ grade). Print the details of the students with Grade.
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno,marks;
char name[30],grade;
}stu[5];
void prepare_result();
void main()
{
int i;
clrscr();
for(i=0; i<5; i++)
{
printf("\nenter %d student roll no: ",i+1);
scanf("%d",&stu[i].rollno);
printf("enter %d student name: ",i+1);
scanf("%s",&stu[i].name);
printf("enter %d student marks: ",i+1);
scanf("%d",&stu[i].marks);
}
prepare_result();
getch();
}
void prepare_result()
{
int i;
for(i=0; i<5; i++)
{
if(stu[i].marks >= 75)
{
stu[i].grade = 'A';
}
else if(stu[i].marks >= 60)
{
stu[i].grade = 'B';
}
else if(stu[i].marks >= 50)
{
stu[i].grade = 'C';
}
else if(stu[i].marks >= 35)
{
stu[i].grade = 'D';
}
else
{
stu[i].grade = 'F';
}
}
printf("\n\n !! result !! ");
printf("\n===============================");
for(i=0; i<5; i++)
{
printf("\n%d %10s %d %c",stu[i].rollno,stu[i].name,
stu[i].marks,stu[i].grade);
}
printf("\n===============================");
}
/* output:
enter 1 student rolll no: 1
enter 1 student name: vrushal
enter 1 student marks: 39
enter 2 student roll no: 2
enter 2 student name: smit
enter 2 student marks: 80
enter 3 student roll no: 3
enter 3 student name: vaibhav
enter 3 student marks: 49
enter 4 student roll no: 4
enter 4 student name: anuj
enter 4 student marks: 60
enter 5 student roll no: 5
enter 5 student name: arshad
enter 5 student marks: 80
!! result !!
===============================
1 vrushal 39 D
2 smit 80 A
3 vaibhav 49 D
4 anuj 60 B
5 arshad 80 A
===============================
*/
6. Create a structure product with ProductCode (int), Name (char array) and Price data elements. In the main function declare p[5] of product. Do the necessary data entry for all five products. Pass the base address of an array to user defined function inc_price(), which will increase the price of all the products by 10%. Print all the products with all the details again after increasing the price.
Coming soon
5. Write a user defined function to reverse the given string.
#include<stdio.h>
#include<conio.h>
void reverse(char *);
void main()
{
char str[90];
clrscr();
printf("enter string: ");
gets(str);
reverse(str);
getch();
}
void reverse(char *str1)
{
int i,len=0;
for(i=0; str1[i] != '\0'; i++)
{
len++;
}
printf("\n\nreverse string: ");
for(i=len; i != -1; i--)
{
printf("%c",str1[i]);
}
}
/* output:
enter string: khyati foundation
reverse string: noitadnuof itayhk
*/
4. Write a program, which takes a name of the user in the lowercase letters. Call a user defined function upper which will convert all lowercase letters into the uppercase letter. Finally print the string.
#include<stdio.h>
#include<conio.h>
void uppercase(char *);
void main()
{
char str1[90],str2[90];
clrscr();
printf("enter string: ");
gets(str1);
printf("\n\nyour string in lowercase: %s",str1);
uppercase(str1);
getch();
}
void uppercase(char *str2)
{
int i;
for(i=0; str2[i] != '\0'; i++)
{
if(str2[i] != ' ')
{
str2[i] = str2[i] - 32;
}
}
printf("\n\nyour string in uppercase: %s",str2);
}
/* output:
enter string: vrushal pandav
your string in lowercase: vrushal pandav
your string in uppercase: VRUSHAL PANDAV
*/
3. Write a user defined function which will return the length of the string declared locally in the main function.
#include<stdio.h>
#include<conio.h>
int length(char *);
void main()
{
int len;
char str[99];
clrscr();
printf("enter string: ");
gets(str);
len = length(str);
printf("\n\nyour string length is: %d",len);
getch();
}
int length(char *l)
{
int i,k=0;
for(i=0; l[i] != '\0'; i++)
{
k++;
}
return k;
}
/* output:
enter string: vrushal pandav
your string length is: 14
*/
2. Write a user defined function calc(), which will returns the sum, subtraction, multiplication, and division values of two variable locally declared in the main function.
#include<stdio.h>
#include<conio.h>
void calc(int *,int *);
void main()
{
int no1,no2;
clrscr();
printf("enter 1st number: ");
scanf("%d",&no1);
printf("enter 2nd number: ");
scanf("%d",&no2);
calc(&no1, &no2);
getch();
}
void calc(int *a, int *b)
{
printf("\n\naddition : %d",*a + *b);
printf("\nsubtraction: %d",*a - *b);
printf("\nmultiplication: %d",*a * *b);
printf("\ndivision: %d",*a / *b);
}
/* output:
enter 1st number: 10
enter 2nd number: 5
addition : 15
subtraction: 5
multiplication: 50
division: 2
*/
1. Write a user defined function which will swap the values of two variables declared locally in the main program.
#include<stdio.h>
#include<conio.h>
int swap(int * , int *);
void main()
{
int x , y;
clrscr();
printf("enter 1st number: ");
scanf("%d",&x);
printf("enter 2nd number: ");
scanf("%d",&y);
swap(&x,&y);
printf("\n1st number after swap: %d",x);
printf("\n2nd number after swap: %d",y);
getch();
}
int swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
return 0;
}
/* output:
enter 1st number: 12
enter 2nd number: 13
1st number after swap: 13
2nd number after swap: 12
*/
8. Write a program to accept records of different states using array of structures. The structure should contain char state, int population, int literacy rate and int per capita income. Assume suitable data. Display the state whose literacy rate is highest and whose per capita income is highest.
coming soon....
10. Define a structure with tag population with fields Men and Women. Create structure with in structure using state and population structure. Read and display the data.
#include<stdio.h>
#include<conio.h>
struct population
{
long men,women;
}p1;
struct state
{
char name[60];
long total;
struct population p1;
}s1;
void main()
{
clrscr();
printf("enter state name: ");
scanf("%s",s1.name);
printf("enter how many men: ");
scanf("%ld",&s1.p1.men);
printf("enter how many women: ");
scanf("%ld",&s1.p1.women);
s1.total = s1.p1.men + s1.p1.women;
printf("\n\nstate name: %s",s1.name);
printf("\nmen: %ld",s1.p1.men);
printf("\nwomen: %ld",s1.p1.women);
printf("\ntotal population: %ld",s1.total);
getch();
}
/* output:
enter state name: gujrat
enter how many men: 28000
enter how many women: 20000
state name: gujrat
men: 28000
women: 20000
total population: 48000
*/
9. Define a structure employee with members employee name, basic pay, dearness allowance, house rent, net salary. Declare an array of 5 employees. Write a function which calculates the net salary of employees and prints all employee details in descending order of their net salary.
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[20];
long b_pay,da,hr,net_sal;
}e[5];
void main()
{
int i;
clrscr();
for(i=0; i<5; i++)
{
printf("\nenter %d employee name: ",i+1);
scanf("%s",e[i].name);
printf("enter %d employee basic pay: ",i+1);
scanf("%ld",&e[i].b_pay);
printf("enter %d employee dearness allowence: ",i+1);
scanf("%ld",&e[i].da);
printf("enter %d employee house rent: ",i+1);
scanf("%ld",&e[i].hr);
e[i].net_sal = e[i].b_pay + e[i].da + e[i].hr;
}
for(i=0; i<5; i++)
{
printf("\n\n%d employee name: %s",i+1,e[i].name);
printf("\n%d employee basic pay: %ld",i+1,e[i].b_pay);
printf("\n%d employee dearness allowence: %ld",i+1,e[i].da);
printf("\n%d employee house rent: %ld",i+1,e[i].hr);
printf("\n%d employee net salary: %ld",i+1,e[i].net_sal);
}
getch();
}
7. Define a structure by name time with members seconds, minutes and hours of int type. A variable of the structure would thus represent time. If time1 and time2 are two variables of the structure type, write a program to find the difference of two times using a function.
#include<stdio.h>
#include<conio.h>
struct time
{
int sec,min,h;
}t[2];
void main()
{
int i,s,m,hh;
clrscr();
for(i=0; i<2; i++)
{
printf("enter time %d: ",i+1);
printf("\n\nenter hour for time %d: ",i+1);
scanf("%d",&t[i].h);
if(t[i].h > 24)
{
printf("\n!! please enter less than 24 !!");
getch();
exit(0);
}
printf("\nenter minute for time %d: ",i+1);
scanf("%d",&t[i].min);
if(t[i].min > 60)
{
printf("\n!! please enter less than 60 !!");
getch();
exit(0);
}
printf("\nenter second for time %d: ",i+1);
scanf("%d",&t[i].sec);
if(t[i].sec > 60)
{
printf("\n!! please enter less than 60 !!");
getch();
exit(0);
}
printf("\n\n");
}
for(i=0; i<2; i++)
{
printf("\ntime %d= %d : %d : %d",i+1,t[i].h,t[i].min,t[i].sec);
}
if(t[0].h > t[1].h)
{
hh = t[0].h - t[1].h;
}
else
{
hh = t[1].h - t[0].h;
}
if(t[0].min > t[1].min)
{
m = t[0].min - t[1].min;
}
else
{
m = t[1].min - t[0].min;
}
if(t[0].sec > t[1].sec)
{
s = t[0].sec - t[1].sec;
}
else
{
s = t[1].sec - t[0].sec;
}
printf("\n\ndifference between two time= %d : %d : %d",hh,m,s);
getch();
}
/* output:
enter time 1:
enter hour for time 1: 12
enter minute for time 1: 35
enter second for time 1: 24
enter time 2:
enter hour for time 2: 8
enter minute for time 2: 59
enter second for time 2: 46
time 1= 12 : 35 : 24
time 2= 8 : 59 : 46
difference between two time= 4 : 24 : 22
*/
6. Write a program to accept records of different states using array of structures. The structure should contain char state and number of int engineering colleges, int medical colleges, int management colleges and int universities. Calculate total colleges and display the state, which is having highest number of colleges.
#include<stdio.h>
#include<conio.h>
struct states
{
char state[20];
int engc, medc, manc, uni;
}s[20];
void main()
{
int no,i,total[20],temp,k,max;
clrscr();
printf("how many state you want to enter: ");
scanf("%d",&no);
if(no>20)
{
printf("\n\n!! please enter less than 20");
}
else
{
for(i=0; i<no; i++)
{
printf("\nenter %d state : ",i+1);
scanf("%s",s[i].state);
printf("\nin %s state how many engineering college: ",s[i].state);
scanf("%d",&s[i].engc);
printf("in %s state how many medical college: ",s[i].state);
scanf("%d",&s[i].medc);
printf("in %s state how many management college: ",s[i].state);
scanf("%d",&s[i].manc);
printf("in %s state how mnay universities: ",s[i].state);
scanf("%d",&s[i].uni);
total[i] = s[i].engc + s[i].medc + s[i].manc + s[i].uni;
printf("\n");
}
printf("\n\n!! total colleges !!\n");
for(i=0; i<no; i++)
{
printf("\nin %s : %d",s[i].state,total[i]);
printf("\n");
}
max= total[0];
for(i=0; i<no; i++)
{
if(total[i] > max)
{
max = total[i];
k = i;
}
else if(max == total[0])
{
k = 0;
}
}
printf("\n-> in %s state highest number of %d colleges <-",
s[k].state,max);
}
getch();
}
/* output:
enter how many state you want to enter: 3
enter 2 state : gujrat
in gujrat state how many engineering college: 13
in gujrat state how many medical college: 12
in gujrat state how many management college: 13
in gujrat state how mnay universities: 14
enter 2 state : rajasthan
in rajasthan state how many engineering college: 2
in rajasthan state how many medical college: 4
in rajasthan state how many management college: 7
in rajasthan state how mnay universities: 8
enter 3 state : up
in up state how many engineering college: 23
in up state how many medical college: 4
in up state how many management college: 10
in up state how mnay universities: 1
!! total colleges !!
in gujrat : 52
in rajasthan : 21
in up : 38
-> in gujrat state highest number of 52 colleges <-
*/
5. Define a structure that can describe a Hotel. It should have members that include name, address, grade, room charges, grade and no of rooms. Write a function to print out all hotel details with room charges less than a given value.
#include<stdio.h>
#include<conio.h>
struct hotel
{
char name[20],add[50],grade[3];
int rch,rno;
}s[20];
void lesscharges(int);
void main()
{
int no,i;
clrscr();
printf("how many hotel details you want to enter: ");
scanf("%d",&no);
for(i=0; i<no; i++)
{
printf("\nenter hotel name: ");
scanf("%s",s[i].name);
printf("enter hotel address: ");
scanf("%s",s[i].add);
printf("enter hotel grade: ");
scanf("%s",s[i].grade);
printf("enter hotel charges: ");
scanf("%d",&s[i].rch);
printf("enter no of rooms: ");
scanf("%d",&s[i].rno);
}
lesscharges(no);
getch();
}
void lesscharges(int num)
{
int i,min,k;
min = s[0].rch;
for(i=0; i<num; i++)
{
if(s[i].rch < min)
{
min = s[i].rch;
k = i;
}
else if(min == s[0].rch)
{
k = 0;
}
}
printf("\n\n!! room charges lessthan given value !!");
printf("\n\nhotel name: %s",s[k].name);
printf("\nhotel address: %s",s[k].add);
printf("\nhotel grade: %s",s[k].grade);
printf("\nhotel charges: %d",s[k].rch);
}
/* output:
how many hotel details you want to enter: 3
enter hotel name: taj
enter hotel address: ahmedabad
enter hotel grade: A
enter hotel charges: 1500
enter no of rooms: 1
enter hotel name: shyam
enter hotel address: goa
enter hotel grade: B
enter hotel charges: 1300
enter no of rooms: 1
enter hotel name: vihar
enter hotel address: div
enter hotel grade: A
enter hotel charges: 1400
enter no of rooms: 1
!! room charges lessthan given value !!
hotel name: shyam
hotel address: goa
hotel grade: B
hotel charges: 1300
*/
4. Define a structure to represent a date. Use your structures that accept two different dates in the format mm dd of the same year. Write a C program to display the month names of both dates.
#include<stdio.h>
#include<conio.h>
struct date
{
int dd,mm,yy;
}s[2];
void main()
{
int i;
clrscr();
printf("!! enter two date MM and DD format of the same year !!");
for(i=0; i<2; i++)
{
printf("\n\nenter date %d DD: ",i+1);
scanf("%d",&s[i].dd);
if(s[i].dd>=32)
{
printf("\n\n!! enter valid date !!");
getch();
exit(0);
}
printf("enter date %d mm: ",i+1);
scanf("%d",&s[i].mm);
if(s[i].mm>=13)
{
printf("\n\n!! enter valid month !!");
getch();
exit(0);
}
printf("enter date %d yy: ",i+1);
scanf("%d",&s[i].yy);
if(i==1)
{
if(s[0].yy != s[1].yy)
{
printf("\n\n!! please enter same year of both dates !!");
getch();
exit(0);
}
}
}
for(i=0; i<2; i++)
{
switch(s[i].mm)
{
case 1: printf("\n%d date %d-%d-%d month: january\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 2: printf("\n%d date %d-%d-%d month: february\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 3: printf("\n%d date %d-%d-%d month: march\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 4: printf("\n%d date %d-%d-%d month: april\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 5: printf("\n%d date %d-%d-%d month: may\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 6: printf("\n%d date %d-%d-%d month: june\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 7: printf("\n%d date %d-%d-%d month: july\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 8: printf("\n%d date %d-%d-%d month: august\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 9: printf("\n%d date %d-%d-%d month: september\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 10: printf("\n%d date %d-%d-%d month: octomber\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 11: printf("\n%d date %d-%d-%d month: navember\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
case 12: printf("\n%d date %d-%d-%d month: december\n",
i+1,s[i].dd,s[i].mm,s[i].yy);
break;
}
}
getch();
}
/* output:
!! enter two date MM and DD format of the same year !!
enter date 1 DD: 31
enter date 1 mm: 1
enter date 1 yy: 2004
enter date 2 DD: 13
enter date 2 mm: 10
enter date 2 yy: 2004
1 date 31-1-2004 month: january
2 date 13-10-2004 month: octomber
*/
Subscribe to:
Posts (Atom)
python programs
1. sum of two number
-
● Stack ● Queue ● Searching ● Sorting
-
Semester 1 Semester 2 Semester 3 Semester 4 NOTICE: Click To Text And Open It
-
1. Find the Simple Interest. Inputs are principal amount, period in year and rate of interest. 2. Find the area and perimeter of square a...