Académique Documents
Professionnel Documents
Culture Documents
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int dec,bin,days;
long mm,mg,nn,sec;
float fahr,cel;
char ch;
int ch1,ch2,ch3,i,n,m;
clrscr();
do
{
printf("Program for CONVERSIONS\n");
printf("1.Number format conversion.\n");
printf("2.Temperature conversion.\n");
printf("3.Unit Conversion.\n");
printf("Enter your Choice\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Press 1 for Decimal to Binary.\n");
printf("Press 2 for Binary to Decimal.\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the Decimal no.\n");
scanf("%d",&dec);
bin=0;
i=0;
while(dec>0)
{
n=dec%2;
bin=bin+pow(10,i)*n;
dec=dec/2;
1
i++;
}
printf("It's Binary form is %d\n",bin);
break;
case 2:
printf("Enter the Binary no.\n");
scanf("%d",&bin);
dec=0;
i=0;
while(bin>0)
{
n=bin%10;
dec=dec+pow(2,i)*n;
bin=bin/10;
i++;
}
printf("It's Decimal form is %d\n",dec);
break;
default:
printf("Invalid chice.\n");
}
break;
case 2:
printf("Press 1 for Farhenheit to Celcius.\n");
printf("Press 2 for Celcius to Fahrenheit.\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the Farhenhiet value.\n");
scanf("%f",&fahr);
cel=5*((fahr-32)/9);
printf("It's Celcius value is %f\n",cel);
break;
case 2:
printf("Enter the Celcius value.\n");
scanf("%f",&cel);
fahr=((cel*9)/5)+32;
printf("It's Farhenheit value is %f\n",fahr);
break;
default:
printf("Invalid chice.\n");
}
break;
case 3:
printf("Press 1 Days Conversion.\n");
printf("Press 2 Length Conversion.\n");
printf("Press 3 for Weight Conversion.\n");
printf("Press 4 for Time Conversion.\n");
scanf("%d",&ch2);
switch(ch2)
{
2
case 1:
printf("Enter the value of Days.\n");
scanf("%d",&days);
n=days/365;
days=days%365;
printf("Years = %d\n",n);
n=days/30;
days=days%30;
printf("Months = %d\n",n);
printf("Days = %d\n",days);
break;
case 2:
printf("Enter the value of MilliMeter.\n");
scanf("%ld",&mm);
nn=mm/1000000;
mm=mm%1000000;
printf("KiloMeter = %ld\n",nn);
nn=mm/1000;
mm=mm%1000;
printf("Meters = %ld\n",nn);
nn=mm/10;
mm=mm%10;
printf("CentiMeters = %ld\n",nn);
printf("MilliMeters = %ld\n",mm);
break;
case 3:
printf("Enter the value of MilliGrams.\n");
scanf("%ld",&mg);
nn=mg/1000000;
mg=mg%1000000;
printf("KiloGrams = %ld\n",nn);
nn=mg/1000;
mg=mg%1000;
printf("Grams = %ld\n",nn);
printf("MilliGrams = %ld\n",mg);
break;
case 4:
printf("Enter the value of Seconds.\n");
scanf("%ld",&sec);
nn=sec/3600;
sec=sec%3600;
printf("Hours = %ld\n",nn);
nn=sec/60;
sec=sec%60;
printf("Minutes = %ld\n",nn);
printf("Seconds = %ld\n",sec);
break;
default:
printf("Invalid chice.\n");
}
break;
default:
3
printf("Invalid Choice.");
}
printf("Do you want to continue\n");
printf("Press Y for Yes\n");
printf("Press N for No\n");
scanf("%s",&ch);
}
while(ch=='y'||ch=='Y');
}
4
Dynamic memory allocation in ‘C’:
1. Write a program to built linked list using ‘C’ programming language’s
dynamic memory allocation operators.
Create distinct subroutines for:
I. Creating a new node
II. Inserting the node at: beginning, end , after and before a particular node
III. Deletion of a node : from beginning, end and deletion of a particular node
IV. Search operation: search the fist occurrence, last occurrence and no of
occurrences of a particular node
V. Traversal and display
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
node *next;
}*nptr;
nptr->next = temp;
return temp;
}
while(temp->next!=NULL)
{
5
temp = temp->next;
}
return temp1;
}
if(nptr->next==NULL)
printf("list is empty\n");
temp = (node*)malloc(sizeof(struct node));
temp->info = beg;
temp->next = NULL;
nptr->next = temp;
return temp;
}
else
{
temp1=(node*)malloc(sizeof(struct node));
temp1->info=beg;
temp1->next=nptr->next;
nptr->next=temp1;
return temp1; }
}
if(nptr->next==NULL)
{
printf("list is empty\n");
temp = (node*)malloc(sizeof(struct node));
temp->info = aft;
temp->next = NULL;
6
nptr->next = temp;
return temp;
}
else
{
printf("enter the value after which u want to insert\n");
scanf("%d",&a);
temp = nptr;
while(temp->info!=a)
temp = temp->next;
temp1=(node*)malloc(sizeof(struct node));
temp1->info=aft;
temp1->next=temp->next;
temp->next=temp1;
return temp1;
}
}
if(nptr->next==NULL)
{
printf("list is empty\n");
temp = (node*)malloc(sizeof(struct node));
temp->info = inf;
temp->next = NULL;
nptr->next = temp;
return temp;
}
else
{
printf("enter the value before which u want to insert\n");
scanf("%d",&a);
temp = nptr;
temp1=temp->next;
while(temp1->info!=a)
{
temp1= temp1->next;
temp=temp->next; }
temp2=(node*)malloc(sizeof(struct node));
temp2->info=inf;
temp2->next=temp1;
temp->next=temp2;
7
return temp2;
}
}
if(nptr->next==NULL)
{
printf("can't delete;list is empty\n");
return nptr;
}
else
{
temp = nptr;
temp1= temp->next;
temp2 = temp1->next;
while(temp1->info!=a)
{
temp = temp->next;
temp1=temp1->next;
temp2=temp2->next;
}
temp1->next=NULL;
temp->next=temp2;
return nptr;
}
}
while(1)
{
printf("%d %u -> ",r->info,r->next);
if(r->next==NULL)
break;
r=r->next;
}
}
8
node* pop()
{
struct node *temp,*temp1;
temp = nptr;
if(temp->next == NULL)
{
printf("Stack Underflow\n");
return nptr;
}
else
{
temp1 = temp->next;
temp->next = temp1->next;
temp1->next = NULL;
return temp1;
}
}
temp1=(node*)malloc(sizeof(struct node));
temp1->info=inf;
temp1->next=nptr->next;
nptr->next=temp1;
return temp1;
while(1)
{
printf("%d ",r->info);
if(r->next==NULL)
break;
r=r->next;
}
}
9
struct node *temp1;
temp1=(node*)malloc(sizeof(struct node));
temp1->info=inf;
temp1->next=nptr->next;
nptr->next=temp1;
return temp1;
}
node* queue_deletion()
{
if(temp->next == NULL)
{
printf("Queue Underflow\n");
return nptr;
}
else
{
temp1 = temp->next;
temp->next = temp1->next;
temp1->next = NULL;
return temp1;
}
}
while(1)
{
printf("%d ",r->info);
if(r->next==NULL)
break;
r=r->next;
}
}
int main()
{
clrscr();
struct node *temp;
int a,b;
10
nptr = NULL;
int k = 1;
while(k==1)
{
11
scanf("%d",&a);
delete_node(a);
break;
printf("want to continue?1/0\n");
scanf("%d",&k);
if(k!=1)
break;
}
getch();
return 0;
12
File Handling:
1. Write a program to count no of words, spaces and new-lines in a given input
file.
Now eliminate the multiple blank spaces and new lines also insert line
numbers at the beginning of each new line.
# include <stdio.h>
void count( )
{
FILE *fp ;
char ch ;
int nol = 0, not = 0, nob = 0, noc = 0 ;
clrscr();
fp = fopen ( "Macro.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
noc++ ;
if ( ch == ' ' )
nob++ ;
if ( ch == '\n' )
nol++ ;
if ( ch == '\t' )
not++ ;
}
fclose ( fp ) ;
printf ( "\nNumber of characters = %d", noc ) ;
printf ( "\nNumber of blanks = %d", nob ) ;
printf ( "\nNumber of tabs = %d", not ) ;
printf ( "\nNumber of lines = %d", nol ) ;
}
void trim()
{
int w;
FILE *fp1;
FILE *fp2;
char c;
int fs=1;
int fn=1;
fp1=fopen("text.txt","r");
fp2=fopen("text1.txt","w");
c=fgetc(fp1);
13
}
while((int)c==32)
{
c=fgetc(fp1);
}
while(c!=EOF)
{
if(c!=' ' && c!='\n')
{
fputc(c,fp2);
fs=1;
fn=1;
}
else if(c==' ')
{
if(fs==1)
{
if(fn==1)
{
fputc(c,fp2);
fs=0;
}
}
}
else if(c=='\n' && fn==1)
{
fputc(c,fp2);
fn=0;
fs=0;
}
c=fgetc(fp1);
}
printf("\nTrimmed.");
}
void main()
{
count();
trim();
getch();
}
14
2. Write a program to add Student structure to a file containing the information
like student name, roll no, percentage, etc.
Add multiple records of such type to a file, and then retrieve the records based
on different fields like roll no or percentage; etc.
#include <stdio.h>
#include <conio.h>
void main( )
{
FILE *fp ;
char another = 'Y' ;
struct emp
{
char name[40] ;
int age ;
float bs ;
};
struct emp e ;
fp = fopen ( "Student.txt", "wb" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( another == 'Y' || another == 'y')
{
printf ( "\nEnter name, age and basic salary: " ) ;
scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
fprintf ( fp, "%s %d %f\n", e.name, e.age, e.bs ) ;
printf ( "Add another record (Y/N) " ) ;
fflush ( stdin ) ;
another = getche( ) ;
}
fclose ( fp ) ;
getch();
}
#include <stdio.h>
#include <conio.h>
void main( )
{
FILE *fp ;
char empname[25];
//char another = 'Y' ;
struct emp
{
char name[40] ;
15
int age ;
float bs ;
};
struct emp e ;
fp = fopen ( "Student.txt", "r" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( fread(&e,sizeof(e),1,fp)==1 )
{
if(strcmp(e.name,empname)==0)
{
fseek(fp,sizeof(e),SEEK_CUR);
printf ( "\n info about employee");
printf ( "%d\n%s\n%f\n", e.age,e.name,e.bs ) ;
}
}
fclose ( fp ) ;
getch();
}
16
Macros:
1. Write a programs to depict working of preprocessing statements in ‘C’;
Write sample program to show working of:
a)#define b)#include c) #ifdef - #else - #endif d) #if- #elif directives
e)#undef f)#pragma
#include<stdio.h>
#include<conio.h>
void fun1();
void fun2();
//#undef HLINE
//#undef VLINE
//#undef ARRANGE
//#undef FOUND
//#undef AREA
void main()
{
float r1 = 6.25, r2 = 2.5, ar ;
int i,y;
clrscr();
ar = AREA ( r1 ) ;
printf ( "\nArea of circle = %f", ar ) ;
ar = AREA ( r2 ) ;
printf ( "\nArea of circle = %f", ar ) ;
gotoxy ( 1, 12 ) ;
HLINE
for ( y = 1 ; y < 25 ; y++ )
VLINE ( 39, y ) ;
#if TEST<=5
printf("Condition Satisfy.\n");
#else
printf("Condition False.\n");
#endif
17
#ifdef OKAY
printf("Virus detected.\n");
#else
{
printf("No virus is there.\n");
}
#endif
printf("Indide main.\n");
getch();
}
void fun1()
{
printf("Inside fun1.\n");
}
void fun2()
{
printf("Inside fun2.\n");
}
18