0% found this document useful (0 votes)
13 views

C Program L

Uploaded by

kgf0237
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C Program L

Uploaded by

kgf0237
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Structure that describes a hotel(name,address,grade,avg room rent)perform

some operations.

#include<stdio.h>

#include<conio.h>

main( )

struct hotel

char name[20];

char city[10];

char grade;

int rc,nr;

};

struct hotel ht[20],t;

int i,n,j,c;

char gr;

clrscr( );

printf(“enter no. of hotels\n”);

scanf(“%d”,&n);

for(i=0;i<n;i++)

{
printf(“enter name of hotel \n”);

scanf(“%s”,&ht[i].name);

printf(“enter name of city \n”);

scanf(“%s”,&ht[i].city);

printf(“enter the grade \n”);

scanf(“%s”.ht[i].grade);

ht[i].grade=getche( );

printf(“enter room charge \n”);

scanf(“%d”,&ht[i].rc);

printf(“enter no of rooms \n”);

scanf(“%d”,&ht[i].nr);

for(i=0;i<n;i++)

for(j=0;j<n-i;j++)

t=ht[j];

ht[j]=ht[j+i];

ht[j+1]=t;

printf(“enter a grade to print the hotels \n”);

gr=getche();
printf(“hotel name city grade roomcharge no of room”);

for(i=0;i<n;i++)

if(gr==ht[i].grade)

printf(“%s %s %c %d %d”,ht[i].name,ht[i].city,ht[i].grade,ht[i].rc,ht[i].nr);

printf(“enter a room charge to print hotels less than given charge \n”);

scanf(“%d”,&c);

printf(“hotel name city grade roomcharge no of rooms”);

for(i=0;i<n;i++)

if(c<=ht[i].rc)

printf(“%s %s %c %d%d”,ht[i].name,ht[i].city,h[i].grade,ht[i].rc,ht[i].nr);

C Program using union to Calculate total runs scored by cricket team

# include < stdio.h >


# include < conio.h >
union player
{
char name[20] ;
int runs ;
};

int main( )
{
int i, s = 0 ;
union player a[11] ;
printf("\n Enter Name of Player Runs Scored \n") ;
printf(" ---------------------------------------------\n") ;
for(i = 0; i <= 10; i++ )
{
printf(" Enter %d Player Name : ", i + 1) ;
scanf("%s",a[i].name) ;
printf(" Enter %d Player Runs : ", i + 1) ;
scanf("%d",&a[i].runs);
}
for(i = 0; i<=10; i++ )
s = s + a[i].runs ;

printf("\n ---------------------------------------------\n") ;
printf("\n Runs Scored by Player \n") ;
printf(" Name \t\tRuns\n") ;
for(i = 0; i <= 10; i++ )
{
printf(" %s %d", a[i].name, a[i].runs) ;
}
printf(" Total Runs Scored by Team: %d", s) ;
return 0 ;
}

Write a c program to evaluate pointer Expression

#include <stdio.h>

int main()
{

int a = 20, b = 10;

int add, sub, div, mul, mod;

ptr_a = &a;
ptr_b = &b;
add = *ptr_a + *ptr_b;
sub = *ptr_a - *ptr_b;
mul = *ptr_a * *ptr_b;
div = *ptr_a / *ptr_b;
mod = *ptr_a % *ptr_b;
printf("Addition = %d\n", add);
printf("Subtraction = %d\n", sub);
printf("Multiplication = %d\n", mul);
printf("Division = %d\n", div);
printf("Modulo = %d\n", mod);
return 0;
}

C program that will receive a filename and a line of text as command line
arguments

#include<stdio.h>
#include<conio.h>
main(argc, argv)
int argc;

char *argv[ ];
{
FILE *fp;
int i;
char word[15];

fp = fopen(argv[1], “w");
printf(“\nNo. of arguments in Command line = %d\n\n", argc);
for(i = 2; i < argc; i++)
fprintf(fp, “%s", argv[i]);
fclose(fp);

printf(“Contents of %s file\n\n", argv[1]);


fp = fopen(argv[1], “r");
for(i = 2; i < argc; i++)
{
fscanf(fp, “%s", word);
printf(“%s", word);
}
fclose(fp);
printf(“\n\n");

for(i = 0; i < argc; i++)


printf(“%*s\n", i*5,argv[i]);
}

Write a c program Creation ,insertion and deletion in a linked list.

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

struct node
{
int info;
struct node *link;
};

typedef struct node* NODE;

NODE getnode();
NODE insert_front(NODE , int);
NODE delete_front(NODE);
void display(NODE);

void main()
{
NODE first;
int choice, item;

first = NULL;

while(1)
{
printf("Enter\n");
printf("1. Insert Front\n");
printf("2. Delete Front\n");
printf("3. Display the list\n");
printf("4. Exit\n");

scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter item to be inserted\n");
scanf("%d", &item);
first = insert_front(first, item);

break;

case 2:
first = delete_front(first);

break;

case 3:
display(first);

break;

default:
exit(0);
}
}
}

NODE getnode()
{
NODE x;

x = (NODE) malloc(sizeof(struct node));


if(x == NULL)
{
printf("Node creation error\n");
return;
}

return x;
}

NODE insert_front(NODE first , int item)


{
NODE temp;

temp = getnode();
temp->info = item;

temp->link = first;

return temp;
}

NODE delete_front(NODE first)


{
NODE temp;

if(first == NULL)
{
printf("Cannot delete. Empty List\n");
return first;
}

temp = first;
first = first->link;

printf("Deleted node is %d\n", temp->info);


free(temp);

return first;
}

void display(NODE first)


{
NODE temp;

printf("Contents of linked list is:\n");

if(first == NULL)
{
printf("Cannot print. Empty list\n");
return;
}

temp = first;

while(temp != NULL) //as long as there are elemens in the linked list
{
printf("%d\t", temp->info);
temp = temp->link;
}

printf("\n");

You might also like