Name : Piyush Menghani Roll Number : DTU/2K13/B01/0159 Group : B01
DTU/2K13/B01/0155
INDEX S.No. Practicals Date Remarks 1 Program to Calculate Simple Interest 2 Program to find sum and average of 10 numbers 3 Program to find Greatest of 10 numbers 4 Program to check whether entered number is prime or not 5 Program to implement switch - case statement 6 Program for Fibonacci series 7 Program to find the sum of digits of a five digit number 8 Program to reverse a five digit number 9 Program to find area and perimeter of a circle, rectangle, square and triangle using function
10 Program to find factorial using recursion 11 Program to find the number of vowels in the entered string 12 Program to check whether the entered string is palindrome or not
13 Program to convert Uppercase to Lower and Vice-Versa 14 Program for converting number to binary and vice versa 15 Program to implement bitwise operators 16 Program to print given pattern 1 12 123 1234 12345
17 Program for addition of two 3x3 matrix 18 Program for multiplication of two 3x3 matrix 19 Program to search a number from array of numbers 20 Program to generate employee details using structure 21 Program to sort an array using insertion sort/selection sort 22 Program to pass and return a pointer to a function 23 Program to pass an array to function as pointer and calculate
DTU/2K13/B01/0155
the sum of its elements 24 Program to read a file and write into another file after converting all lowercase characters to uppercase
25 Program to find the length of the string without using predefined functions
DTU/2K13/B01/0155
PROGRAM 1 File name: prg1.c /* Q1 Program to Calculate Simple Interest*/ #include<stdio.h> #include<conio.h> int main() { float p,t,r,si; clrscr(); printf("Enter the principal amount:"); scanf("%f",&p); printf("Enter the rate of interest per annum:"); scanf("%f",&r); printf("Enter the time(in years):"); scanf("%f",&t); si=(float)(p*r*t)/100; printf("The simple interest is: %f",si); printf("\nThe final amount is: %f",p+si); getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 2 File Name: prg2.c /* Q2 Program to find sum and average of 10 numbers*/ #include<stdio.h> #include<conio.h> int main() { int i,data; float avg,sum=0; clrscr(); for(i=1;i<=10;i++) { printf("Enter number %d:",i); scanf("%d",&data); sum+=data; } avg=(float)sum/10; printf("The sum of entered numbers is: %f",sum); printf("\nThe average of entered numbers is: %f",avg); getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 3 File Name: prg3.c /*Q3 Program to find Greatest of 10 numbers*/ #include<stdio.h> #include<conio.h> int main() { int max,n,i; clrscr(); printf("Enter number 1 :"); scanf("%d",&max); for(i=0;i<9;i++) { printf("Enter number %d :",i+2); scanf("%d",&n); if(n>max) max=n; } printf("The greatest number is: %d",max); getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 4 File name: prg4.c /*Q4 Program to check whether entered number is prime or not*/ #include<stdio.h> #include<conio.h> int main() { int data,i,found=0; clrscr(); printf("Program to check if a number is prime or not"); printf("\nEnter the number to be checked:"); scanf("%d",&data); if(data==1||data==2) printf("The number is neither prime nor composite"); else { for(i=2;i<data;i++) { if(data%i==0) found++; }
PROGRAM 5 File name: prg5.c /*Q5 Program to implement switch - case statement*/ #include<stdio.h> int main() { char ch; int a,b,f; printf("Menu : \n\ta. Addition \n\tb. Multiply\n"); printf("\nEnter your choice\t"); scanf("%c",&ch); switch (ch) { case 'a' : printf("\nEnter the two numbers to be added\t"); scanf("%d%d",&a,&b); f=a+b; printf("The sum is\t%d",f); break; case 'b' : printf("\nEnter the two numbers to be multiplied\t"); scanf("%d%d",&a,&b); f=a*b; printf("The sum is\t%d",f); break; default : printf("Wrong choice"); break; } return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 6 File name: prg6.c /* Q6. Program to print fibbonaci series*/ #include<stdio.h> int main() { int i,n,f=0,a=0,b=1; printf("Enter the number of terms\t"); scanf("%d",&n); for(i=0;i<n-1;i++) { f=f+a; a=b; b=f; printf("%d",f); } return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 7 File name: prg7.c /*Q7 Program to find the sum of digits of a five digit number*/ #include<stdio.h> #include<conio.h> int main() { long data; int rem,sum=0; clrscr(); printf("Enter a 5 digit number:"); scanf("%ld",&data); while(data>0) { rem=data%10; sum+=rem; data=data/10; } printf("The sum of digits is: %d",sum); getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 8 File name: prg8.c /*Q8 Program to reverse a five digit number*/ #include<stdio.h> #include<conio.h> int main() { long data,rev=0; int rem; clrscr(); printf("enter the number:"); scanf("%ld",&data); while(data>0) { rem=data%10; rev=rev*10+rem; data=data/10; } printf("The reverse of the number is: %ld",rev); getch(); return 0; } OUTPUT:
DTU/2K13/B01/0155
PROGRAM 9 File name: prg9.c /*Q9. Program to find area and perimeter of a circle, rectangle, square and triangle using function*/ #include<stdio.h> #define PI 3.1415 void circle(); void rectangle(); void square(); void triangle();
int main() { int ch; printf("Area and Perimeter Menu :\n1.Circle\n2.Rectangle\n3.Square\n4.Triangle\n\nEnter your Choice"); scanf("%d",&ch); switch(ch) { case 1 : circle(); break;
PROGRAM 10 File name: prg10.c /*Q10 Program to find factorial using recursion*/ #include<stdio.h> #include<conio.h> long fact(int ); int main() { int n; long fac; clrscr(); printf("Enter the number: "); scanf("%d",&n); fac=fact(n); if(fac==0) printf("\nInvalid input"); else printf("The factorial is: %ld ",fac);
PROGRAM 11 File name: prg11.c /* Q11 Program to find the number of vowels in the entered string*/ #include<stdio.h> #include<conio.h> #include<ctype.h> int main() { char s[50];int i,vow=0,cons=0; clrscr(); printf("Enter a string: "); gets(s); printf("\nThe string is as follows:\n"); printf("%s",s); for(i=0;s[i]!='\0';i++) { s[i]=(islower(s[i])?toupper(s[i]):s[i]); if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U') vow++; else cons++; } printf("\nNumber of Vowels: %d",vow); printf("\nNumber of Consonants: %d",cons); getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 12 File name: prg12.c /*Q12 Program to check if the string is a palindrome using pointers*/ #include<stdio.h> #include<conio.h> int main() { char str[30]; clrscr(); char *p,*t; printf("Enter a string: "); gets(str); for(p=str;*p!=NULL;p++); for(t=str,p--;p>=t;) { if(*p==*t) { p--; t++; } else break; } if(t>p) printf("\nString is a palindrome"); else printf("\nString is not a palindrome");
getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 13 File name: prg13.c /*Q13 Program to convert Uppercase to Lower and Vice-Versa*/ #include<stdio.h> #include<conio.h> #include<ctype.h> int main() { char s[50]; int i; clrscr(); printf("Enter a string:\n"); gets(s); printf("\nThe entered string is as follows:\n"); printf("%s",s); for(i=0;s[i]!='\0';i++) { if(islower(s[i])) s[i]=toupper(s[i]); else if(isupper(s[i])) s[i]=tolower(s[i]); } printf("\nNew String:\n"); printf("%s",s); getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 14 File name: prg14.c /*Q14 Program for converting number to binary and vice versa */ #include<stdio.h> void dectobin(void); void bintodec(void); int pow(int ,int ); int main() { int ch; printf("Menu : \n1. Decimal To Binary\n2. Binary to Decimal\nEnter your Choice\t"); scanf("%d",&ch); switch(ch) { case 1: dectobin(); break; case 2: bintodec(); break; default: printf("Wrong Choice"); break; } return 0; }
void dectobin() { int i,num,r[100],count; printf("\nEnter the number\t"); scanf("%d",&num); for(i=0,count=0;num!=0;i++,count++) { r[i]=num%2; num=num/2; } printf("Binary is "); for(i=count-1;i>=0;i--) { printf("%d",r[i]); }
} void bintodec() { int i,num,r[100],count,sum=0; printf("Enter the binary\t"); scanf("%d",&num); for(count=0;num!=0;count++)
DTU/2K13/B01/0155
{ r[count]=num%10; sum=sum+pow(2,count)*r[count]; num=num/10; } printf("The number is %d",sum); } int pow(int number, int index) { int i,f=1; for(i=0;i<index;i++) { f=f*number; } return f; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 15 File name: prg15.c /*Q15Program to implement bitwise operator */ #include<stdio.h> #include<conio.h> int main() { long int num,nnumb,numb,c,nnum; printf("Enter a Number="); scanf("%ld",&num); printf("Enter a Number="); scanf("%ld",&numb); printf("\nMenu:\nPress 1 for Left Shift"); printf("\nPress 2 for Right Shift"); printf("\nPress 3 for OR operator"); printf("\nPress 4 for AND operator"); printf("\nPress 5 for XOR operator"); printf("\nEnter your choice:-"); scanf("%ld",&c); switch(c) { case 1:nnum=num<<1; printf("Left shift of %ld by 1= %ld",num,nnum); nnumb=numb<<1; printf("\nLeft shift of %ld by 1= %ld",numb,nnumb);break; case 2:nnum=num>>1; printf("Right shift of %ld by 1= %ld",num,nnum); nnumb=numb>>1; printf("\nRight shift of %ld by 1= %ld",numb,nnumb);break; case 3:nnum=num|numb; printf("%ld OR %ld= %ld",num,numb,nnum);break; case 4:nnum=num&numb; printf("%ld AND %ld= %ld",num,numb,nnum);break; case 5:nnum=num^numb; printf("%ld XOR %ld= %ld",num,numb,nnum);break; default:printf("\nINVALID INPUT");break; } getch(); } OUTPUT:
DTU/2K13/B01/0155
PROGRAM 16 File name: prg16.c /*Q16 To print the given type of pattern*/ #include<stdio.h> #include<conio.h> int main() { int n,i,j; clrscr(); do { printf("Enter the number of rows <=25: "); scanf("%d",&n); }while(n<1||n>25); for(i=1;i<=n;i++) { for(j=1;j<=i;j++) printf("%d",j); printf("\n"); } getch(); return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 17 File name: prg17.c /* Q17 Program for addition of two 3x3 matrix*/ #include<stdio.h> #include<conio.h> void input(int [][3]); void display(int [][3]); void add(int [][3],int [][3],int [][3]); int main() { int a[3][3],b[3][3],c[3][3]; clrscr(); printf("Enter the matrix 1:\n"); input(a); printf("Enter the matrix 2:\n"); input(b); printf("Displaying the matrix 1:\n"); display(a); printf("Dipslaying the matrix 2:\n"); display(b); add(a,b,c); printf("Displaying the new matrix:\n"); display(c); getch(); return 0; } void input(int a[][3]) { int r,c; for(r=0;r<3;r++) { for(c=0;c<3;c++) { printf("Enter element (%d,%d): ",r+1,c+1); scanf("%d",&a[r][c]); } } } void display(int a[][3]) { int r,c; for(r=0;r<3;r++) { for(c=0;c<3;c++) printf("%d ",a[r][c]); printf("\n"); } } void add(int a[][3],int b[][3],int d[][3]) { int r,c;
PROGRAM 19 File name: prg19.c /*Q19 Program to search a number from array of numbers*/ #include<stdio.h> #include<conio.h> void input(int [],int &); void display(int [],int ); int lsearch(int [],int ,int ); int main() { int a[100],n,data,x; clrscr(); printf("Enter the array:\n"); input(a,n); printf("Displaying the array:\n"); display(a,n); printf("Enter the number you want to search: "); scanf("%d",&data); x=lsearch(a,n,data); if(x==0) printf("Element not found"); else printf("Element found at position: %d",x); getch(); return 0; } void input(int a[],int &n) { do { printf("Enter the number of elements <=100: "); scanf("%d",&n); }while(n<1||n>100); int i; for(i=0;i<n;i++) { printf("Enter element %d: ",i+1); scanf("%d",&a[i]); } } void display(int a[],int n) { int i; for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n"); } int lsearch(int a[],int n,int data) {
DTU/2K13/B01/0155
int i=0,found=0; while((i<n)&&!(found)) { if(a[i]==data) found++; i++; }
if(found) return i; else return 0; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 20 File name: prg20.c /*Q20 Program to generate employee details using structure*/ #include<stdio.h> struct employee { char name[20]; int day; char month[10]; int year; float salary; };
PROGRAM 22 File name :prog22.c /*Program to pass and return a pointer to a function(checking which number is larger)*/ #include<stdio.h> #include<conio.h> int *larger(int *,int *); int main() { int a,b; clrscr(); int *result; printf("Enter number 1: "); scanf("%d",&a); printf("Enter number 2: "); scanf("%d",&b); result=larger(&a,&b); printf("The larger number is: %d",*result); getch(); return 0; } int *larger(int *x,int *y) { if(*x>*y) return x; else return y; }
OUTPUT:
DTU/2K13/B01/0155
PROGRAM 23 File Name: prog23.c /* Program to pass an array to function as pointer and calculate the sum of its elements */ #include<stdio.h> #include<conio.h> void input(int [],int &); void display(int *,int ); int sum(int [],int ); int main() { int a[100]; int n; clrscr(); input(a,n); display(a,n); int result=sum(a,n); printf("The sum of all elements of array is: %d",result); getch(); return 0; } void input(int a[],int &n) { do { printf("Enter the number of elements <=100: "); scanf("%d",&n); }while(n<1||n>100); int i; for(i=0;i<n;i++) { printf("Element %d:",i+1); scanf("%d",&a[i]); } } void display(int a[],int n) { printf("\nDisplaying the array:\n"); int i; for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n"); } int sum(int *x,int n) { int i,add=0; for(i=0;i<n;i++) add+=*x++;
DTU/2K13/B01/0155
return add; }
OUTPUT:
DTU/2K13/B01/0155
DTU/2K13/B01/0155
PROGRAM 24 File Name: prog24.c /* Program to read a file and write into another file after converting all lowercase characters to uppercase */ #include<stdio.h> #include<conio.h> #include<ctype.h> void create(char []); void display(char []); void manipulate(char []); int main() { clrscr(); char name[20]; printf("Enter the name of the file: "); gets(name); printf("Enter the contents of the file:\n"); create(name); printf("Displaying the contents of the file:\n"); display(name); manipulate(name); printf("Displaying the new file:\n"); display("New"); getch(); return 0; } void create(char name[]) { FILE *fp; fp=fopen(name,"w"); char c; while((c=getchar())!=EOF) putc(c,fp); fclose(fp); } void display(char name[]) { FILE *fp; fp=fopen(name,"r"); char c; while((c=getc(fp))!=EOF) printf("%c",c); fclose(fp); } void manipulate(char name[]) { FILE *fp1,*fp2;
PROGRAM 25 File name:prog25.c /* Program to find the length of the string without using predefined functions */ #include<stdio.h> #include<conio.h> int slen(char []); int main() { char str[100]; int n; clrscr(); printf("Enter a string <100 characters long: "); gets(str); n=slen(str); printf("\nThe length of the string is: %d",n);
getch(); return 0; } int slen(char s[]) { int i; for(i=0;s[i]!='\0';i++); return i; }