Uploaded by prasanna.lifeskills

C PROGRAMMING LAB MANUAL

advertisement
C PROGRAMMING LAB MANUAL
1)WELCOME PROGRAM
#include<stdio.h>
void main()
{
printf("Welcome to Praavinya\n\n");
}
2) Student Result Program
#include<stdio.h>
void main()
{
int kan+eng+mat+sci+soc+hin;
printf("Enter 6 subj marks\n");
scanf("%d%d%d%d%d%d",&kan,&eng,&mat,&soc,&sci,&hin);
total=kan+eng+mat+sci+soc+hin;
per=total/6;
printf("Total=%d",total);
printf("Per=%f",per);
}
3)Program on IF-ELSE
#include<stdio.h>
void main()
{
int age;
printf("Enter your Age\n");
scanf("%d",&age);
if(age<18)
{
printf("Not eligible for voting\n");
}
else
printf("Eligible for voting\n");
}
4) Student result with grade program
.
#include<stdio.h>
void main()
{
int kan+eng+mat+sci+soc+hin;
float=per;
printf("Enter 6 subj marks\n");
scanf("%d%d%d%d%d%d",&kan,&eng,&mat,&soc,&sci,&hin);
total=kan+eng+mat+sci+soc+hin;
per=total/6;
printf("Total=%d\n",total);
printf("Per=%f\n",per);
if((kan<35)||(eng<35)||(kan<35)||(mat<35)||(soc<35)||(hin<35))
printf("Fail\n");
else if(per<50)
printf("just pass");
else if(per<60)
printf("Second class");
else if(per<70)
printf("First class");
else
printf("distinction");
}
5)
ATM
#include<stdio.h>
#include"func.c"
void main()
{
int s1=500,s2=100,s3=50,s4=10,amt;
printf("VIJAYA BANK\n");
printf(".............\n");
printf("Enter the amount\n");
scanf("%d",&amt);
s1=calculate(amt,s1);
amt=s1;
s2=calculate(amt,s2);
amt=s2;
s3=calculate(amt,s3);
amt=s3;
s4=calculate(amt,s4);
}
int calculate(int amt,int note)
{
int rem,quo;
rem=amt%note;
quo=amt/note;
if(quo!=0)
printf("\n%d*%d=%d",quo,note,note*quo);
return rem;
}
6)Program on Multiplication Table
#include<stdio.h>
void main()
{
int i,num;
printf("Enter a Number\n");
scanf("%d",&num);
for(i=1;i<11;i++)
{
printf("%d*%d=%d\n",num,i,num*i);
}
}
7)
Program on Multiplication Table2
#include<stdio.h>
void main()
{
int i,num,choice=1;
while(choice==1)
{
system("cls");
printf("Enter a Number\n");
scanf("%d",&num);
for(i=1;i<11;i++)
{
printf("%d*%d=%d\n",num,i,num*i);
}
printf("\n\n1:Continue\t2:Exit\nPlz make ur choice\n");
scanf("%d",&choice);
}
}
8)Nested For Loop
#include<stdio.h>
void main()
{
int i,j,lc,sc;
printf("Enter Line Number\n");
scanf("%d",&lc);
printf("Enter star Number\n");
scanf("%d",&sc);
for(i=1;i<=lc;i++)
{
printf("\n");
for(j=1;j<=sc;j++)
{
printf("*");
}
}
}
9) Nested For Loop 2:
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<5;j++)
{
if(i==0||i==4)
{
printf("S");
}
else{
if(j==0||j==4)
{
printf("S");
}
else
{
printf(" ");
}
}
}
}
}
10)Program to find out biggest of numbers in 3*3 array
#include<stdio.h>
void main()
{
int matrixA[10][10],big=0,i,j;
printf("Program to find the biggest of all numbers you enter in a 2D array of 3*3 size\n\n");
printf("Enter value for matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matrixA[i][j]);
if(big<matrixA[i][j])
{
big=matrixA[i][j];
}
}
}
printf("\n\n The biggest number in the Array is %d",big);
}
11) Program on Matrix Addition
#include<stdio.h>
void main()
{
int matrixA[10][10],matrixB[10][10],matrixC[10][10],m,n,i,j;
printf("Enter the order of the Matrix\n");
scanf("%d%d",&m,&n);
printf("Enter value for matrixA\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrixA[i][j]);
}
}
printf("Enter value for matrixB\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrixB[i][j]);
}
}
printf("The Resultant Matrix is:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
matrixC[i][j]=matrixA[i][j]+matrixB[i][j];
printf("%d\t",matrixC[i][j]);
}
}
}
12) Program on Array using pointers
#include <stdio.h>
void disp(int*);
void main()
{
int i;
int marks[]={55,65,75,56,78,78,90};
for(i=0;i<=6;i++)
disp(&marks[i]);
}
void disp(int *n)
{
printf("%d",*n);
}
13) Program on Strings:
#include<stdio.h>
void main()
{
char name[]="Naan Avan Alla";
int i=0;
while(i<=7)
{
printf("%c",name[i]);
i++;
}
}
14) Program on String Length
#include<stdio.h>
#include<string.h>
void main()
{
char arr[20];
int len=0;
printf("\nEnter a String",len1);
scanf("%s",arr);
/*
while(arr[i]!='\0')
{
len++;
i++;
}
*/
len=strlen(arr);
printf("\nLength of %s is %d",arr,len);
}
15)
Program on String Comparision:
#include<stdio.h>
#include<string.h>
void main()
{
int p;
char str1[20],str2[20],mismatch=0;
int i=0,j=0;
printf("enter the name for str1\n");
scanf("%s", str1);
printf("enter the name for str2\n");
scanf("%s",str2);
/*
while((str1[i]!='\0')&&(str2[i]!='\0'))
{
if(str1[i]!=str2[i])
{
mismatch=1;
break;
}
i++;
}
if(mismatch==1)
{
printf("equal");
else
printf("not equal");
}
*/
p=strcmp(str1,str2);
if(p==0)
printf("equal");
else
printf("not equal");
getch();
}
16)
Program on String Concatenation:
#include<stdio.h>
void main()
{
int i=0,j=0;
char str1[20],str2[20];
printf("Enter the name for str1\n");
scanf("%s",str1);
printf("Enter the name for str2\n");
scanf("%s",str2);
/*while(str1[i]!='\0')
{
i++;
}
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}*/
strcat(str1,str2);
printf("str1=%s\n ",str1);
getch();
}
17)Program on Structures 1
#include <stdio.h>
struct book
{
char name[25];
char author[25];
int callno;
};
void display(struct book);
void main()
{
struct book b1={"let us C","YRK",101};
display(b1);
}
void display (struct book b)
{
printf("\n%s\t%s\t%d",b.name,b.author,b.callno);
}
18)Program on Structures 2:
#include <stdio.h>
#include <string.h>
void main()
{
struct employee
{
char name[10];
int age;
float salary;
};
struct employee e1={"sanjay",30,5500.50};
struct employee e2,e3;
/*piece-meal copying*/
strcpy(e2.name, e1.name);/*e2.name=e1.name is wrong*/
e2.age=e1.age;
e2.salary=e1.salary;
/*copying all elements at one go */
e3=e2;
printf("\n%s%d%f\n",e1.name,e1.age,e1.salary);
printf("\n%s%d%f\n",e2.name,e1.age,e1.salary);
printf("\n%s%d%f\n",e3.name,e1.age,e1.salary);
}
19)Program on Structures 3:
#include <stdio.h>
void main()
{
struct book
{
char name[20];
float price;
int pages;
};
struct book b[3];
int i;
for(i=0;i<3;i++)
{
printf("\n Enter name,price,and pages\n");
scanf("%s%f%d", b[i].name, &b[i].price, &b[i].pages);
}
for(i=0;i<3;i++)
printf("\n%s%f%d", b[i].name,b[i].price,b[i].pages);
}
20)Program on Structures using Pointer
#include <stdio.h>
void main()
{
struct book
{
char name[25];
char author[25];
int callno;
};
struct book b1={"Let us C","YPK",101};
struct book *ptr;
ptr=&b1;
printf("\n%s%s%d\n", b1.name, b1.author,b1.callno);
printf("\n%s%s%d\n", ptr->name, ptr->author,ptr->callno);
}
Programs on Files:
21) Program to write entire struct object into File
#include<stdlib.h>
#include<stdio.h>
void main()
{
FILE *fp;
char another='Y';
struct emp{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen("EMP.DAT","a");
if(fp==NULL)
{
puts("Cannot open file");
exit(0);
}
while(another=='Y')
{
printf("\nEnter name,age & basic salary:");
scanf("%s%d%f",e.name,&e.age,&e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add another record (Y/N)");
fflush(stdin);
another=getche();
}
fclose(fp);
}
22) Program to read entire struct object from File
#include<stdlib.h>
#include<stdio.h>
void main()
{
FILE *fp;
struct emp{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen("EMP.DAT","r");
if(fp==NULL)
{
puts("Cannot open file");
exit(0);
}
printf("\nName\tAge\tSalary\t\n");
while(fread(&e,sizeof(e),1,fp)==1){
printf("\n%s\t%d\t%.2f",e.name,e.age,e.bs);
}
printf("\n\n");
fclose(fp);
}
Download