Вы находитесь на странице: 1из 54

C-DAC DAC CET VB ASSIGNMENTS SOLVED BY

LALIT NAPHADE
1)
#include<stdio.h>
#include<conio.h>
void main()
{
int fib,n,i=0;
clrscr();
printf("enter the number of terms:");
scanf("%d",&n);
while(i<n)
{
printf("%d ",fibo(i));
i++;
}
getch();
}
int fibo(int n)
{
int a;
if(n==0 || n==1)
{
return(n);
}
a=fibo(n-1)+fibo(n-2);
return(a);
}
2)
#include<stdio.h>
#include<conio.h>
void read();
void list();
struct cricket
{
char *pl_name;
char *te_name;
float bat_avg;
}player[10];
void main()
{
char ch,another='y';
char name[80];
int i;
float avg;
clrscr();
for(i=0;i<10;i++)
{
fflush(stdin);
printf("\nenter the info. about player%d:\n",i+1);
printf("\n\tPlayer Name: ");
gets(name);
player[i].pl_name=(char*)malloc(sizeof(name));
strcpy(player[i].pl_name,name);
printf("\n\tTeam Name: ");
gets(name);
player[i].te_name=(char*)malloc(sizeof(name));
strcpy(player[i].te_name,name);
printf("\n\tBatting Average: ");
scanf("%f",&avg);
player[i].bat_avg=avg;
}
while(another=='Y'||another=='y')
{
fflush(stdin);
printf("\nenter your choice\n\t\t(R)ead the info. about all players\n\t\t(T)eam-wise list\t");
scanf("%c",&ch);
if(ch=='r'||ch=='R')
{
read();
}
else if(ch=='t'||ch=='T')
{
list();
}
else
printf("\nWrong Choice");
fflush(stdin);
printf("\nWant to continue(y/n): ");
scanf("%c",&another);
}
}
void read()
{
int i;
clrscr();
for(i=0;i<10;i++)
{
printf("\nDetail of Player%d\n ",i+1);
printf("\n\tPlayer Name:%s ",player[i].pl_name);
printf("\n\tTeam Name:%s ",player[i].te_name);
printf("\n\tBatting Average:%f ",player[i].bat_avg);
}
}
void list()
{
int j,i;
clrscr();
for(j=0;j<10;j++)
{
for(i=0;i<j;i++)
{

if(!strcmp(player[j].te_name,player[i].te_name))
{
break;
}
}
if(i!=j)
continue;

printf("\nTeam: %s\n",player[j].te_name);
for(i=j;i<10;i++)
{
if(!strcmp(player[j].te_name,player[i].te_name))
{
printf("\n\tPlayer Name: %s",player[i].pl_name);
printf("\n\tBatting Average: %f",player[i].bat_avg);
}
}
}
}

3)
#include<stdio.h>
#include<conio.h>
void main()
{
char *str,ch;
clrscr();
printf("enter the string:");
gets(str);
printf("enter the character:");
scanf("%c",&ch);
del(str,ch);
printf("the corrected string is: %s",str);
getch();
}

del(char *str,char ch)


{
int i,j,len;
char *str1;
str1=(char*)malloc(sizeof(str));
len=strlen(str);
i=0;
j=0;
//for(i=0,j=0;i<len;i++)
while(*(str+i)!=NULL)
{
if(*(str+i)==ch)
{
i++;
continue;
}
else
{
str1[j]=*(str+i);
i++;
j++;
}
}
str1[j]='\0';
strcpy(str,str1);
}

4)
#include<stdio.h>
#include<conio.h>
int bin_search(int *,int,int);
void main()
{
int arr[8],item,loc,i;
clrscr();
printf("enter the sorted array:\n");
for(i=0;i<8;i++)
scanf("%d",&arr[i]);
printf("enter the item to be searched: ");
scanf("%d",&item);
loc=bin_search(arr,item,8);
printf("the item %d is at location %d",item,loc);
getch();
}

int bin_search(int *arr,int item,int n)


{
int beg,end,mid,loc;
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end && arr[mid]!=item)
{
if(item<arr[mid])
end=mid-1;
if(item>arr[mid])
beg=mid+1;
mid=(beg+end)/2;
}
if(arr[mid]==item)
loc=mid;
else
loc=NULL;
return(loc+1);
}
5)
#include<stdio.h>
#include<conio.h>
void main()
{
char *str,*str1;
int n,m,i,j,count;
clrscr();

printf("enter the string: ");


gets(str);
printf("\nenter the position from where to extract: ");
scanf("%d",&n);
printf("\nenter the number of characters to extract: ");
scanf("%d",&m);
for(i=n-1,count=0,j=0;i<strlen(str);i++)
{
if(count>=m)
break;
else
str1[j]=str[i];
j++;
count++;
}
str1[j]='\0';
printf("the extracted string is: %s",str1);
getch();
}

6)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("enter the no. of rows of pascal's triangle:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=0;j<n-i;j++)
{
printf(" ");
}
for(j=0;j<2*i-1;j++)
{
printf("%d",i);
}
for(j=0;j<n-i;j++)
{
printf(" ");
}
printf("\n");
}
getch();
}

7)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float sum=0;
clrscr();
printf("enter the number of terms in series: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(1/(float)i);
}
printf("The sum of the series of %d terms is= %f",n,sum);
getch();
}

8)
#include<stdio.h>
#include<conio.h>
void main()
{
char *str;
int i,len;
clrscr();
printf("enter the string: ");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
str[i]=~str[i];
}
printf("\nEncrypted String: ");
puts(str);
for(i=0;i<len;i++)
{
str[i]=~str[i];
}
printf("\nDecrypted String: ");
puts(str);
getch();
}

9)
#include<stdio.h>
#include<conio.h>
void main()
{
char *str,item;
int i,j;
clrscr();
printf("enter the string: ");
gets(str);
for(j=1;j<strlen(str);j++)
{
item=str[j];
i=j-1;
if(i>=0)
{
while(str[i]>=item)
{
str[i+1]=str[i];
i--;
}
}
str[i+1]=item;
}
printf("String in alphabetical order is \n\t");
puts(str);
getch();
}
10.
#include<stdio.h>
#include<conio.h>
#define JAN 31
#define MAR 31
#define APR 30
#define MAY 31
#define JUN 30
#define JUL 31
#define AUG 31
#define SEP 30
#define OCT 31
#define NOV 30
#define DEC 31
void main()
{
int date,month,year,diff,temp,FEB,i,days;
clrscr();
printf("enter the date (dd/mm/yyyy) :\t");
scanf("%d/%d/%d",&date,&month,&year);
if(month<0 || month>12 || date<0 || date>31)
{
printf("\nInvalid Date");
return;
}
if(year>=2001)
{
diff=year-2001;
}
else
{
diff=2001-year;
}
if(year>=2001)
{
for(i=1,days=0;i<=diff;i++)
{
temp=year-i;
if(temp%400==0 || (temp%4==0 && temp%100!=0))
{
days=days+366;
}
else
{
days=days+365;
}
}
}
else
{
for(i=1,days=0;i<diff;i++)
{
temp=year+i;
if(temp%400==0 || (temp%4==0 && temp%100!=0))
{
days=days+366;
}
else
{
days=days+365;
}
}
}

if(year%400==0 || (year%4==0 && year%100!=0))


{
FEB=29;
}
else
{
FEB=28;
}
if(year>=2001)
{
switch(month)
{
case 1:
{
temp=0;
break;
}
case 2:
{
temp=JAN;
break;
}
case 3:
{
temp=JAN+FEB;
break;
}
case 4:
{
temp=JAN+FEB+MAR;
break;
}
case 5:
{
temp=JAN+FEB+MAR+APR;
break;
}
case 6:
{
temp=JAN+FEB+MAR+APR+MAY;
break;
}
case 7:
{
temp=JAN+FEB+MAR+APR+MAY+JUN;
break;
}
case 8:
{
temp=JAN+FEB+MAR+APR+MAY+JUN+JUL;
break;
}
case 9:
{
temp=JAN+FEB+MAR+APR+MAY+JUN+JUL+AUG;
break;
}
case 10:
{

temp=JAN+FEB+MAR+APR+MAY+JUN+JUL+AUG+SEP;
break;
}
case 11:
{

temp=JAN+FEB+MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT;
break;
}
case 12:
{

temp=JAN+FEB+MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV;
break;
}
}
}
else
{
switch(month)
{
case 1:
{

temp=FEB+MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC;
date=JAN-date;
break;
}
case 2:
{

temp=MAR+APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC;
date=FEB-date;
break;

}
case 3:
{

temp=APR+MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC;
date=MAR-date;
break;
}
case 4:
{
temp=MAY+JUN+JUL+AUG+SEP+OCT+NOV+DEC;
date=APR-date;
break;
}
case 5:
{
temp=JUN+JUL+AUG+SEP+OCT+NOV+DEC;
date=MAY-date;
break;
}
case 6:
{
temp=JUL+AUG+SEP+OCT+NOV+DEC;
date=JUN-date;
break;
}
case 7:
{
temp=AUG+SEP+OCT+NOV+DEC;
date=JUL-date;
break;
}
case 8:
{
temp=SEP+OCT+NOV+DEC;
date=AUG-date;
break;
}
case 9:
{
temp=OCT+NOV+DEC;
date=SEP-date;
break;
}
case 10:
{
temp=NOV+DEC;
date=OCT-date;
break;
}
case 11:
{
temp=DEC;
date=NOV-date;
break;
}
case 12:
{
temp=0;
date=DEC-date;
break;
}
}
}
date=days+temp+date;
date=date%7;
if(year>=2001)
{
switch(date)
{
case 1:
{
printf("\n\tMONDAY");
break;
}
case 2:
{
printf("\n\tTUESDAY");
break;
}
case 3:
{
printf("\n\tWEDNESDAY");
break;
}
case 4:
{
printf("\n\tTHURSDAY");
break;
}
case 5:
{
printf("\n\tFRIDAY");
break;
}
case 6:
{
printf("\n\tSATURDAY");
break;
}
case 0:
{
printf("\n\tSUNDAY");
break;
}
}
}
else
{
switch(date)
{
case 1:
{
printf("\n\tSATURDAY");
break;
}
case 2:
{
printf("\n\tFRIDAY");
break;
}
case 3:
{
printf("\n\tTHURSDAY");
break;
}
case 4:
{
printf("\n\tWEDNESDAY");
break;
}
case 5:
{
printf("\n\tTUESDAY");
break;
}
case 6:
{
printf("\n\tMONDAY");
break;
}
case 0:
{
printf("\n\tSUNDAY");
break;
}
}
}
getch();
}

11.
#include<stdio.h>
#include<conio.h>
void main()
{
char *str,*str1,*str2;
int i,j;
clrscr();
strcpy(str,"");
strcpy(str1,"");
strcpy(str2,"");
printf("enter the string: ");
gets(str);
str1=(char*)malloc(sizeof(str));
str2=(char*)malloc(sizeof(str));

for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]==' ')
{
str1[j]='\0';
//puts(str1);
j=0;
}
else
{
str1[j]=str[i];
j++;
}
if(str[i]==' ')
{
if(!strcmp(str1,"from"))
strcpy(str1,"to");
strcat(str2,str1);
strcat(str2," ");
}
}

if(i==strlen(str))
{
str1[j]='\0';
j=0;
if(!strcmp(str1,"from"))
strcpy(str1,"to");
strcat(str2,str1);

puts(str2);
}

12.
#include<stdio.h>
#include<conio.h>
main()
{
int **arr;
int n,i,sum,j,temp;
clrscr();
printf("enter the dimension of the square matrix: ");
scanf("%d",&n);
printf("\nenter the elements of the array: ");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&arr[i][j]);
printf("\nThe array is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",arr[i][j]);
printf("\n");
}
for(i=0,sum=0;i<n;i++)
sum=sum+arr[0][i];
for(i=0;i<n;i++)
{
temp=0;
for(j=0;j<n;j++)
temp=temp+arr[i][j];
if(temp!=sum)
{
printf("\nthe matrix is not a magic square");
return(0);
}
}
for(i=0;i<n;i++)
{
temp=0;
for(j=0;j<n;j++)
temp=temp+arr[j][i];
if(temp!=sum)
{
printf("\nthe matrix is not a magic square");
return(0);
}
}
for(i=0,temp=0;i<n;i++)
temp=temp+arr[i][i];
if(temp!=sum)
{
printf("\nthe matrix is not a magic square");
return(0);
}
printf("\nthe matrix is a square matrix");
return(0);
}

13.
#include<stdio.h>
#include<conio.h>
void main()
{
int **a,**b,m,n,i,j,m1,n1;
clrscr();
printf("enter the number of rows and colsin the matrix: ");
scanf("%d%d",&m,&n);
a=(int*)malloc(sizeof(int)*m*n);
b=(int*)malloc(sizeof(int)*m*n);
printf("enter the elements:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
m1=n;
n1=m;
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
b[i][j]=a[j][i];
printf("Original matrix:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
printf("\nTransposed matrix:\n");
for(i=0;i<m1;i++)
{
printf("\n");
for(j=0;j<n1;j++)
printf("%d\t",b[i][j]);
}
getch();
}
14.
#include<stdio.h>
#include<conio.h>
struct emp{
char *name;
char *emp_no;
char *desg;
float basic_pay;
char *ph;
char *dept;
struct emp *next;
};
char temp[100];
struct emp *start;
int main()
{
int ch;
start=NULL;
while(1)
{
//clrscr();
printf("enter your choice:\n\t1.Add employee detail after a given name");
printf("\n\t2.Append data\n\t3.Print details of employee");
printf("\n\t4.Edit details of a particular employee\n\t5.Find telephone no. of an
employee\n\t6.Exit\t");
fflush(stdin);
scanf("%d",&ch);
//fflush(stdin);
if(ch==1)
add();
else if(ch==2)
append();
else if(ch==3)
print();
else if(ch==4)
edit();
else if(ch==5)
search();
else if(ch==6)
return(0);
else
printf("\nWrong Choice");
}
}
add()
{
float pay;
//char *name;
struct emp *node,*ptr;
printf("\nenter the employee name after which you want to add data: ");
fflush(stdin);
gets(temp);

for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
if(!strcmp(ptr->name,temp))
{
break;
}
}
if(ptr==NULL)
{
printf("Name not found");
getch();
return(0);
}
else
{
node=(struct emp*)malloc(sizeof(struct emp));
printf("enter the data of the employee:\n\tName: ");
fflush(stdin);
gets(temp);

node->name=(char*)malloc(sizeof(temp));
strcpy(node->name,temp);
printf("\n\tEmployee No.: ");
fflush(stdin);
gets(temp);

node->emp_no=(char*)malloc(sizeof(temp));
strcpy(node->emp_no,temp);
printf("\n\tDesignation: ");
fflush(stdin);
gets(temp);

node->desg=(char*)malloc(sizeof(temp));
strcpy(node->desg,temp);
printf("\n\tBasic Pay: ");
scanf("%f",&pay);
fflush(stdin);
node->basic_pay=pay;
printf("\n\tTelephone Number: ");
fflush(stdin);
gets(temp);

node->ph=(char*)malloc(sizeof(temp));
strcpy(node->ph,temp);
printf("\n\tDepartment: ");
fflush(stdin);
gets(temp);

node->dept=(char*)malloc(sizeof(temp));
strcpy(node->dept,temp);
/*if(ptr==start)
{
node->next=start;
start=node;
}
else
{ */
node->next=ptr->next;
ptr->next=node;
//}
}
return(0);
}

append()
{
float pay;
struct emp *node,*ptr,*prev;
node=(struct emp*)malloc(sizeof(struct emp));
printf("enter the data of the employee:\n\tName: ");
fflush(stdin);
gets(temp);
node->name=(char*)malloc(sizeof(temp));
strcpy(node->name,temp);
printf("\n\tEmployee No.: ");
fflush(stdin);
gets(temp);

node->emp_no=(char*)malloc(sizeof(temp));
strcpy(node->emp_no,temp);
printf("\n\tDesignation: ");
fflush(stdin);
gets(temp);
node->desg=(char*)malloc(sizeof(temp));
strcpy(node->desg,temp);
printf("\n\tBasic Pay: ");
fflush(stdin);
scanf("%f",&pay);

node->basic_pay=pay;
printf("\n\tTelephone Number: ");
fflush(stdin);
gets(temp);

node->ph=(char*)malloc(sizeof(temp));
strcpy(node->ph,temp);
printf("\n\tDepartment: ");
fflush(stdin);
gets(temp);

node->dept=(char*)malloc(sizeof(temp));
strcpy(node->dept,temp);

if(start==NULL)
{
start=node;
node->next=NULL;
}
else
{
for(ptr=start;ptr!=NULL;ptr=ptr->next)
prev=ptr;
prev->next=node;
node->next=NULL;
}
}

print()
{
struct emp *ptr,*point;
int ch;
printf("enter your choice\n\t1.Total list\n\t2.Department wise\t");
scanf("%d",&ch);
if(ch==1)
{
printf("Details of Employees:\n");
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
printf("\tEmployee Name: %s\n",ptr->name);
printf("\tEmployee No: %s\n",ptr->emp_no);
printf("\tDesignation: %s\n",ptr->desg);
printf("\tBasic Pay: Rs. %f\n",ptr->basic_pay);
printf("\tTelephone No.: %s\n",ptr->ph);
printf("\tDepartment: %s\n",ptr->dept);
printf("\n\n");
}
}
else if(ch==2)
{
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
for(point=start;point!=ptr;point=point->next)
{
if(!strcmp(point->dept,ptr->dept))
break;
}
if(ptr!=point)
continue;
printf("\tDepartment: %s\n",ptr->dept);
for(point=ptr;point!=NULL;point=point->next)
{
if(!strcmp(point->dept,ptr->dept))
{
printf("\tEmployee Name: %s\n",point->name);
printf("\tEmployee No: %s\n",point->emp_no);
printf("\tDesignation: %s\n",point->desg);
printf("\tBasic Pay: Rs. %f\n",point->basic_pay);
printf("\tTelephone No.: %s\n",point->ph);
printf("\n");
}
}
printf("\n\n");
}
}
getch();
}

edit()
{
struct emp *ptr;
float pay;
printf("\nenter the employee number whose data is to be edited: ");
fflush(stdin);
gets(temp);
for(ptr=start;ptr!=NULL;ptr=ptr->next)
{
if(!strcmp(ptr->emp_no,temp))
{
break;
}
}
if(ptr==NULL)
{
printf("\nEmployee not found");
return;
}
else
{
printf("\nenter the new information about the employee:\n");
printf("\n\tEmployee Name.: ");
gets(temp);
fflush(stdin);
ptr->name=(char*)malloc(sizeof(temp));
strcpy(ptr->name,temp);
printf("\n\tEmployee No.: ");
gets(temp);
fflush(stdin);
ptr->emp_no=(char*)malloc(sizeof(temp));
strcpy(ptr->emp_no,temp);
printf("\n\tDesignation: ");
gets(temp);
fflush(stdin);
ptr->desg=(char*)malloc(sizeof(temp));
strcpy(ptr->desg,temp);
printf("\n\tBasic Pay: ");
scanf("%f",&pay);
fflush(stdin);
ptr->basic_pay=pay;
printf("\n\tTelephone Number: ");
gets(temp);
fflush(stdin);
ptr->ph=(char*)malloc(sizeof(temp));
strcpy(ptr->ph,temp);
printf("\n\tDepartment: ");
gets(temp);
fflush(stdin);
ptr->dept=(char*)malloc(sizeof(temp));
strcpy(ptr->dept,temp);
}
}

search()
{
struct emp *ptr;
printf("\nenter the employee name : ");
fflush(stdin);
gets(temp);

for(ptr=start;ptr!=NULL;ptr=ptr->next)
if(!strcmp(ptr->name,temp))
break;
printf("Telephone Number of %s is \n\n\t\t%s",ptr->name,ptr->ph);
getch();

15.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[25],temp,mean,std,var,median,mode;
int i,n=26,total,j,med;
clrscr();
while(n>25)
{
printf("enter the number of items:\n");
scanf("%d",&n);
if(n>25)
{
printf("\nNumber of items can be less than 25");
}
}
printf("enter the %d items:\n",n);
for(i=0,total=0;i<n;i++)
{
scanf("%f",&x[i]);
total++;
}
for(i=0,temp=0;i<n;i++)
{
temp=temp+x[i];
}
mean=temp/n;
for(i=0,temp=0;i<n;i++)
{
temp=temp+(x[i]-mean)*(x[i]-mean);
}
var=temp/n;
std=sqrt(var);
//median
for(i=1;i<n;i++)
{
temp=x[i];
for(j=i-1;j>=0;j--)
{
if(x[j]>temp)
{
x[j+1]=x[j];
}
else
{
break;
}
}
x[j+1]=temp;
}
if(total%2!=0)
{
med=(total+1)/2;
median=x[med-1];
}
else
{
med=total/2;
median=(x[med-1]+x[med])/2;
}

printf("\n\tTotal Items:\t%d",total);
printf("\n\tMean:\t%f",mean);
printf("\n\tStandard Deviation:\t%f",std);
printf("\n\tVariance:\t%f",var);
printf("\n\tMedian:\t%f",median);
getch();
}

16. #include<stdio.h>
#include<conio.h>
void main()
{
char *str1,*str2;
int len,i;
clrscr();
printf("enter the string: ");
gets(str1);
str2=(char*)malloc(sizeof(str1));
len=strlen(str1);
for(i=0;i<len;i++)
str2[len-i-1]=str1[i];
printf("\nReversed String:\n\t%s",str2);
getch();
}

17.
#include<stdio.h>
#include<conio.h>
#define MAXSTK 10
void main()
{

int *stack,top,ch,item,n,num1,num2,i;
stack=(int*)malloc(sizeof(int)*MAXSTK);
top=-1;
while(1)
{
clrscr();
printf("\n\t1)Push \n\t2)Pop \n\t3)Print top element \n\t4)Duplicate the top
element\n\t5)Swap top two elements\n\t6)Clear Stack\n\t7)Print\n\t8)Exit\n\t\tenter your
choice\t");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:
printf("\nEnter the item:\t");
scanf("%d",&item);
fflush(stdin);
n=push(stack,&top,item);
if(n==1)
{
printf("\nItem has been inserted successfully");
}
break;
case 2:
n=pop(stack,&top,&item);
if(n==1)
{
printf("\nItem Poped:\t%d",item);
}
break;
case 3:
if(top==-1)
{
printf("\n There is no element in the stack");
}
else
{
printf("\nThe top element of stack is:\t
%d",stack[top]);
}
break;
case 4:
n=pop(stack,&top,&item);
if(n==1)
{
n=push(stack,&top,item);
if(n==1)
{
push(stack,&top,item);
if(n==1)
{
printf("\nTop element have been
duplicated successfully");
}
}
}
break;
case 5:
n=pop(stack,&top,&num1);
if(n==1)
{
n=pop(stack,&top,&num2);
if(n==1)
{
n=push(stack,&top,num1);
if(n==1)
{
n=push(stack,&top,num2);
if(n==1)
{
printf("\nThe top two element
of the stack have been swapped successfully");
}
}
}
}
break;
case 6:
top=-1;
printf("\nStack have been cleared successfully");
break;
case 7:
printf("The stack is: ");
for(i=0;i<=top;i++)
{
printf("%d ",stack[i]);
}
break;
case 8:
return;
}
getch();
clrscr();
}
}
push(int *stack,int *top,int item)
{
if(*top==MAXSTK)
{
printf("\n\n\t\tOverFlow");
return(0);
}
else
{
(*top)++;
stack[*top]=item;
}
return(1);
}

pop(int *stack,int *top,int *item)


{
if(*top==-1)
{
printf("\n\n\t\tUnderFlow");
return(0);
}
else
{
*item=stack[*top];
(*top)--;
}
return(1);
}

18.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int i,num1,num2,mul,temp;
char c='y';
clrscr();
while(c=='y' || c=='Y')
{
num1=rand()%10;
num2=rand()%10;
mul=num1*num2;
do{
clrscr();
printf("\nhow much is %d times %d? ",num1,num2);
scanf("%d",&temp);
fflush(stdin);
if(temp!=mul)
{
printf("\nNo,Want to Try Again(y/n)\t");
scanf("%c",&c);
fflush(stdin);
}
}while(temp!=mul && (c=='y' || c=='Y'));
if(temp==mul)
{
printf("\nVery Good! Want to Try Another Problem(y/n)\t");
scanf("%c",&c);
fflush(stdin);
}
}
}

19.
#include<stdio.h>
#include<conio.h>
void main()
{
int len,j,hex,i,hour,minute,second;
char ch,str[80];
clrscr();

// b)read hexadecimal value


printf("\nenter number in hexadecimal value: ");
scanf("%x",&hex);
printf("Hexadecimal number:%#x",hex);

// c)print 200 with and without sign


printf("\n%d %u",200,200);

// d)print 100 in hexadecimal form


printf("\n%#x",100);

// e)Read characters in array str until 'p' is encountered


printf("\nenter the string: ");
for(i=0;i<80;i++)
{
ch=getche();
if(ch=='p')
break;
str[i]=ch;
}
str[i]='\0';
puts(str);

// f)print 1.234 with preceding zeros in a 9-digit field


printf("\nfloat no: %09g",1.234);

// g)enter time in format (hh:mm:ss) using assignment suppression character


while(1)
{
printf("\nenter the time(hh:mm:ss) ");
fflush(stdin);
scanf("%d%*c%d%*c%d",&hour,&minute,&second);
if(hour>=24 || minute>=60 ||second>=60)
{
printf("\nINVALID Time.\nEnter time(hh:mm:ss) again ");
}
else
{
break;
}
}
printf("\nTime is\t%d:%d:%d",hour,minute,second);

printf("enter the string:");


fflush(stdin);
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='\"')
{
for(j=i;j<len;j++)
{
str[j]=str[j+1];
}
len--;
}
}
puts(str);

// i)enter time in format (hh:mm:ss) not using assignment suppression character


while(1)
{
printf("\nenter the time(hh:mm:ss) ");
scanf("%d:%d:%d",&hour,&minute,&second);
if(hour>=24 || minute>=60 ||second>=60)
{
printf("\nINVALID Time.\nEnter time(hh:mm:ss) again ");
}
else
{
break;
}
}
printf("\nTime is\t%d:%d:%d",hour,minute,second);

getch();
}

20.
#include<stdio.h>
#include<conio.h>
void main()
{
int faren=0,i=0;
float cel;
clrscr();

for(faren=0;faren<=212;faren++)
{
cel=5.0/9.0*(faren-32);
printf("%10d%10.3f\n",faren,cel);
i++;
if(i==40)
{
getch();
i=0;
clrscr();
}
}
getch();
}

21.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,pow,bits;
long result;
clrscr();
printf("enter the value of number:\t");
scanf("%d",&num);
printf("enter the value of power:\t");
scanf("%d",&pow);
result=eval(num,pow);
printf("\n\tresult=\t%d\n\t",result);
printf("\n\tresult in bits\t");
showbits(result);
getch();
}
eval(int num,int pow)
{
long temp,result;
temp=1<<pow;
result=num*temp;
return(result);
}

showbits(long result)
{
int i,bit[16];
for(i=0;i<16;i++)
{
bit[15-i]=result>>i&1;
}
for(i=0;i<16;i++)
printf("%d",bit[i]);
}

22.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person{
char lastname[15];
char firstname[15];
char age[4];
}e={"unassigned","","0"};
void input();
FILE *fp;
long int recsize;
int main()
{
int i,ch,j;
clrscr();
fp=fopen("f:\\nitesh\\cdac\\nameage.dat","rb+");
if(fp==NULL)
{
fp=fopen("f:\\nitesh\\cdac\\nameage.dat","wb+");
if(fp==NULL)
{
printf("\nCannot Open File");
return;
}
else
{
for(i=0;i<100;i++)
fwrite(&e,sizeof(e),1,fp);
}

}
rewind(fp);
recsize=sizeof(e);
while(1)
{
printf("\nenter your choice:\n\t1.Input info. of 10 persons\n\t2.Update
Record\n\t3.Delete Record\n\t4.Exit");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:
{
input();
break;
}
case 2:
{
update();
break;
}
case 3:
{
del();
break;
}
case 4:
{
return(0);

}
default :
{
printf("\nWRONG CHOICE\n");;
}
}
/* rewind(fp);
for(i=0;i<1;i++)
{
fread(&e,recsize,1,fp);
printf("\n %s , %s, %s",e.firstname,e.lastname,e.age);
}*/
}
}

void input()
{
int i,j=0;
for(i=0;i<10;i++)
{
rewind(fp);
while(fread(&e,sizeof(e),1,fp)==1)
{
if(!strcmp(e.lastname,"unassigned") && !strcmp(e.firstname,"")
&& !strcmp(e.age,"0"))
{
printf("\nenter record%d :\n\tFirst Name:\t",i+1);
fflush(stdin);
gets(e.firstname);
printf("\n\tLast Name:\t");
gets(e.lastname);
printf("\n\tAge:\t");
gets(e.age);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fp);
j++;
break;
}
}
if(j==100)
{
printf("\n\tNo more space for records\n\tCannot Write in the
File.");
break;
}
}
}

update()
{
char fname[15],lname[15];
int j;
printf("\nenter the first nameof the person: ");
gets(fname);
printf("\nenter the last name of the person: ");
gets(lname);
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
if(!strcmp(lname,e.lastname) && !strcmp(fname,e.firstname))
{
printf("\nenter new information :\n\tFirst Name:\t");
fflush(stdin);
gets(e.firstname);
printf("\n\tLast Name:\t");
gets(e.lastname);
printf("\n\tAge:\t");
gets(e.age);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fp);
j++;
break;
}
}
if(j==100)
{
printf("\n\tName not found.");
}
}

del()
{
char fname[15],lname[15];
int j;
printf("\nenter the first nameof the person: ");
gets(fname);
printf("\nenter the last name of the person: ");
gets(lname);
rewind(fp);
while((j=fread(&e,recsize,1,fp))==1)
{
if(!strcmp(lname,e.lastname) && !strcmp(fname,e.firstname))
{
strcpy(e.lastname,"unassigned");
strcpy(e.firstname,"");
strcpy(e.age,"0");
fseek(fp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fp);
//j++;
break;
}
}
if(j==0)
{
printf("\n\tName not found.");
}
}

23.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct tool{
char *recordno;
char *name;
int qty;
float cost;
}e={"0","",0,0};
void input();
FILE *fp;
long int recsize;
char temp[100];
int main()
{
int i,ch,j;
clrscr();
fp=fopen("f:\\nitesh\\cdac\\hardware.dat","rb+");
if(fp==NULL)
{
fp=fopen("f:\\nitesh\\cdac\\hardware.dat","wb+");
if(fp==NULL)
{
printf("\nCannot Open File");
return;
}
else
{
for(i=0;i<100;i++)
fwrite(&e,sizeof(e),1,fp);
}

}
rewind(fp);
recsize=sizeof(e);
while(1)
{
printf("\nenter your choice:\n\t1.Input record \n\t2.Update
Record\n\t3.Delete Record\n\t4.List\n\t5.Exit");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:
{
input();
break;
}
case 2:
{
update();
break;
}
case 3:
{
del();
break;
}
case 4:
{
list();
break;
}
case 5:
{
return;
}
default :
{
printf("\nWRONG CHOICE\n");;
}
}
/* rewind(fp);
for(i=0;i<1;i++)
{
fread(&e,recsize,1,fp);
printf("\n %s , %s, %s",e.firstname,e.lastname,e.age);
}*/
}
}
void input()
{
int i,j;
j=0;
while(j==0)
{
rewind(fp);
printf("\nenter record%d :\n\tRecord Number:\t",i+1);
fflush(stdin);
gets(temp);
while(fread(&e,recsize,1,fp)==1)
{
if(!strcmp(e.recordno,temp))
{
printf("\nRecord Number already present");
break;
}
else
{
j=1;
break;
}
}
}
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
if(!strcmp(e.recordno,"0"))
{
e.recordno=(char*)malloc(sizeof(temp));
strcpy(e.recordno,temp);
printf("\n\tTool Name:\t");
gets(temp);
e.name=(char*)malloc(sizeof(temp));
strcpy(e.name,temp);
printf("\n\tQuantity:\t");
fflush(stdin);
scanf("%d",&e.qty);
fflush(stdin);
printf("\n\tCost:\t");
scanf("%f",&e.cost);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fp);
j++;
break;
}
}
if(j==100)
{
printf("\n\tNo more space for records\n\tCannot Write in the File.");
}
}

update()
{
int j;
printf("\nenter the record number of the tool: ");
fflush(stdin);
gets(temp);
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
if(!strcmp(temp,e.recordno))
{
printf("\nenter new information :");
fflush(stdin);
printf("\n\tTool Name:\t");
gets(temp);
e.name=(char*)malloc(sizeof(temp));
strcpy(e.name,temp);
printf("\n\tQuantity:\t");
fflush(stdin);
scanf("%d",&e.qty);
printf("\n\tCost:\t");
fflush(stdin);
scanf("%f",&e.cost);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fp);
j++;
break;
}
}
if(j==100)
{
printf("\n\tName not found.");
}
}

del()
{
int j;
printf("\nenter the record number of the tool: ");
gets(temp);
rewind(fp);
while((j=fread(&e,recsize,1,fp))==1)
{
if(!strcmp(temp,e.recordno))
{
strcpy(e.recordno,"0");
strcpy(e.name,"");
e.qty=0;
e.cost=0;
fseek(fp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,fp);
j++;
break;
}
}
if(j==0)
{
printf("\n\tName not found.");
}
}
list()
{
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
{
if(strcmp(e.recordno,"0")!=0)
{
printf("\n\tRecord Number: %s\n\tTool Name: %s\n\tQuantity:
%d\n\tCost:Rs. %f\n",e.recordno,e.name,e.qty,e.cost);
}
}
}

24.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20],str1[20];
int i;
clrscr();
printf("enter the string:\t");
for(i=0;i<5;i++)
{
str[i]=getche();
}

//gets(str);
/*if(strlen(str)>5)
{
printf("\nWord cannot be greater than five letters");
} */

for(i=0;i<strlen(str);i++)
{
str1[i]=str[i]-30;
}
printf("\nEncoded String: \t");
puts(str1);
getch();
}

25.
#include<stdio.h>
#include<conio.h>
void main()
{
float *f,*x,temp=0,avg=0;
int i,n;
clrscr();
printf("enter the number of data:\t");
scanf("%d",&n);
f=(float*)malloc(sizeof(float)*n);
x=(float*)malloc(sizeof(float)*n);
printf("\nenter the data:\nFor");
for(i=0;i<n;i++)
{
do{
printf("\ni=%d\tf=",i);
scanf("%f",&f[i]);
fflush(stdin);
if(f[i]<0 || f[i]>1)
{
printf("%f ",f[i]);
printf("\nThe value of f can be between 0 and 1 only");
}
}while(f[i]<0 || f[i]>1);
printf("\tx=");
scanf("%f",&x[i]);
temp=temp+f[i];
}
if(temp!=1)
{
printf("\n\tthe sum of all f's is greater than 1\n\n\n\t\tPress any key");
getch();
return;
}
for(i=0;i<n;i++)
{
avg=avg+f[i]*x[i];
}
printf("Weighted Average:\t%f",avg);
getch();
}

26.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,root1,root2;
clrscr();
printf("enter the coefficients(a,b,c) of the quadratic eq.(ax^2+bx+c):\n");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("\nThe equation has no real roots");
}
else
{
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("\nThe roots are:\t%f and %f",root1,root2);
}
getch();
}

27.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
struct family{
char *name;
char *nation;
struct family *left,*right;
};
void add(int);
int inord(int);
struct family* find(char*,int);
struct family *root;
char temp[100];
void main()
{
int n,i,ch,gen;
struct family *node,*ptr;
clrscr();
root=NULL;
printf("\nenter the number of generations: ");
scanf("%d",&n);
while(1)
{
clrscr();
printf("\t1) Add a Member\n\t2) Modify existing details\n\t3) Exit\n\t\t
enter your choice:\t");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:
for(i=0;i<n;i++)
{
printf("\n\t%d) Enter the member in generation
%d",i+1,i+1);
}
printf("\n\t%d) Goto previous screen",i+1);
printf("\n\t%d) EXIT\n\t\t enter your choice:\t",i+2);
scanf("%d",&ch);
fflush(stdin);
if(ch==n+1)
{
break;
}
else if(ch==n+2)
{
return;
}
else
{
add(ch);
list(n);
}
break;
case 2:
if(root==NULL)
{
printf("\nThere no records");
break;
}
else
{
printf("\nenter the name of person whose record to
modify:");
gets(temp);
printf("\nenter the generation to which %s
belong:",temp);
scanf("%d",&gen);
fflush(stdin);
if(gen==1)
{
if(strcmp(root->name,temp)==0)
{
ptr=root;
}
else
{
printf("\nNAME Not Found");
ptr=NULL;
}
}
else
{
ptr=find(temp,gen-1);
}
if(ptr!=NULL)
{
printf("enter new name:");
gets(temp);
ptr->name=(char*)malloc(sizeof(temp));
strcpy(ptr->name,temp);
printf("enter Nationality:");
gets(temp);
ptr->nation=(char*)malloc(sizeof(temp));
strcpy(ptr->nation,temp);
}
list(n);
}
break;
case 3:
return;
}
getch();

}
}

list(int n)
{
int i,top=1,num[20],j=0;
struct family *ptr,*stack[20];
for(i=0;i<n;i++)
{
printf("\ngeneration%d\n",i+1);
top=1;
j=0;
stack[top]=NULL;
num[top]=NULL;
ptr=root;
while(ptr!=NULL)
{
top=top+1;
stack[top]=ptr;
num[top]=j;
ptr=ptr->left;
j++;
}
ptr=stack[top];
j=num[top];
top--;
while(ptr!=NULL)
{
if(j==i)
{
printf("\n\tNAME:\t%s",ptr->name);
printf("\n\tNATIONALITY:\t%s",ptr->nation);
}
if(ptr->right!=NULL)
{
ptr=ptr->right;
j++;
while(ptr!=NULL)
{
top=top+1;
stack[top]=ptr;
num[top]=j;
ptr=ptr->left;
j++;
}
}
ptr=stack[top];
j=num[top];
top--;
}
}
}

void add(int ch)


{
int n,val;
struct family *node,*ptr;
n=ch-1;
if(n==0)
{
if(root==NULL)
{
printf("\nenter your details\n");
printf("\n\tName: ");
fflush(stdin);
gets(temp);
node=(struct family*)malloc(sizeof(struct family));
node->name=(char*)malloc(sizeof(temp));
strcpy(node->name,temp);
printf("\n\tNationality: ");
gets(temp);
node->nation=(char*)malloc(sizeof(temp));
strcpy(node->nation,temp);
root=node;
node->left=NULL;
node->right=NULL;
}
else
{
printf("\nAlready added details of parents");
}
}
else
{
val=inord(n-1);
if(val==0)
{
printf("\nThere is no member in the generation%d.\nFirst enter
members in generation%d",ch-1,ch-1);
}
else
{
printf("\nenter the name of the child whose parent you want to
enter: ");
gets(temp);
ptr=find(temp,n-1);
if(ptr!=NULL)
{
printf("\nenter the details of member in generation
%d:",ch);
printf("\n\tName: ");
gets(temp);
node=(struct family*)malloc(sizeof(struct family));
node->name=(char*)malloc(sizeof(temp));
strcpy(node->name,temp);
printf("\n\tNationality: ");
gets(temp);
node->nation=(char*)malloc(sizeof(temp));
strcpy(node->nation,temp);
if(ptr->left==NULL)
{
ptr->left=node;
}
else
{
ptr->right=node;
}
node->left=NULL;
node->right=NULL;
}
}
}
}

int inord(int n)
{
int top=1,num[20],j=0;
struct family *ptr,*stack[20];
stack[top]=NULL;
num[top]=NULL;
ptr=root;
while(ptr!=NULL)
{
if(j==n)
{
return(1);
}
else
{
top=top+1;
stack[top]=ptr;
num[top]=j;
ptr=ptr->left;
j++;
}
}
ptr=stack[top];
j=num[top];
top--;
while(ptr!=NULL)
{
if(ptr->right!=NULL)
{
ptr=ptr->right;
j++;
while(ptr!=NULL)
{
if(j==n)
{
return(1);
}
else
{
top=top+1;
stack[top]=ptr;
num[top]=j;
ptr=ptr->left;
j++;
}
}
}
ptr=stack[top];
j=num[top];
top--;
}

return(0);
}

struct family* find(char *name,int n)


{
int top=1,num[20],j=0;
struct family *ptr,*stack[20];
stack[top]=NULL;
num[top]=NULL;
ptr=root;
while(ptr!=NULL)
{
top=top+1;
stack[top]=ptr;
num[top]=j;
ptr=ptr->left;
j++;
}
ptr=stack[top];
j=num[top];
top--;
while(ptr!=NULL)
{
if(j==n && (strcmpi(ptr->name,name)==0))
{
if(ptr->left==NULL || ptr->right==NULL)
{
return(ptr);
}
else
{
printf("\nAlready added details of parents");
return(NULL);
}
}

if(ptr->right!=NULL)
{
ptr=ptr->right;
j++;
while(ptr!=NULL)
{

top=top+1;
stack[top]=ptr;
num[top]=j;
ptr=ptr->left;
j++;
}
}
ptr=stack[top];
j=num[top];
top--;
}
printf("\nNAME NOT FOUND");
return(NULL);
}

28.
#include<stdio.h>
void main()
{
clrscr();
printf("\n%d",__LINE__);
printf("\n%s",__FILE__);
printf("\n%s",__DATE__);
printf("\n%s",__TIME__);
getch();

29.
#include"stdio.h"
#define YES 1
#define NO 0
#line 3000
#define TRUE "true"
void main()
{
int i;
clrscr();
printf("\n%s",TRUE);
#ifndef TRUE
#else
#undef TRUE
#define TRUE 1
#endif
printf("\n%d",TRUE);
#ifdef TRUE
#undef TRUE
#define TRUE 1
#endif
printf("\n%d",TRUE);

#if TRUE!=0
#define FALSE 0
#else
#define FALSE 1
#endif
printf("\n%d",FALSE);

#define SQUARE_AREA(x) x*x


i=SQUARE_AREA(5);
printf("\n%d",SQUARE_AREA(5));
printf("\n%d",i);
printf("\n%d %d",YES,NO);
printf("\n%d",__LINE__);
}

30.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float fp[30][16],i[16],k,temp;
int j,n;
clrscr();
for(j=0,k=4;j<12;j++)
{
i[j]=k;
k=k+0.5;
}
for(j=0,k=10;j<3;j++)
{
i[j+12]=k;
k++;
}
i[15]=15;

for(n=0;n<30;n++)
{
for(j=0;j<16;j++)
{
fp[n][j]=pow(1+i[j]/100,n+1);
}
}
printf("i= \t");
for(j=0;j<16;j++)
{
printf("%4.1f",i[j]);
}
printf("\n");
for(n=0;n<30;n++)
{
printf("\nn=%2d",n+1);
for(j=0;j<16;j++)
{
printf("%4.1f",fp[n][j]);
}
}
getch();
}

31.
Did notsolved.

Вам также может понравиться