Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
Experiment No. 2 /* 2.1 Display string "Hello" on the screen */ #include<stdio.h> void main() { printf("Hello"); } /* 2.2 Program to accept the marks of three subjects and print total marks and average marks */ #include<stdio.h> #include
void main() { int m1,m2,m3,total,avg; clrscr(); printf("Enter marks of three subjects : "); scanf("%d%d%d",&m1,&m2,&m3); total = m1 + m2 + m3; avg = total / 3; printf("\nTotal = %d Average = %d",total,avg); getch(); } /* 2.3 Display entered number with right justification */ #include<stdio.h> #include void main() { int num; clrscr(); printf("Enter the number : "); scanf("%d",&num); printf("%16d",num); getch(); } /* 2.4 Program to display hexadecimal, decimal and octal format of entered number */ #include<stdio.h> #include CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-1-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
void main() { int num; clrscr(); printf("Enter the number : "); scanf("%d",&num); printf("\nDecimal format : %d",num); printf("\nHexaDecimal format : %x",num); printf("\nOctal format : %o",num); getch(); } /* 2.5 Program to calculate a simple interest using formula si = p * n * r where p = principal amount n = no. of years r = rate of interest*/ #include<stdio.h> #include void main() { float p,n,r,si; clrscr(); printf("Enter principal amount : "); scanf("%f",&p); printf("Enter number of years : "); scanf("%f",&n); printf("Enter rate of interest : "); scanf("%f",&r); si = (p*n*r)/100; printf("\nSimple Interest : %f",si); getch(); }
Experiment No. 3 /* 3.1 Program to determine largest of three integer quantities */ #include<stdio.h> #include void main() { int year; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-2-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
clrscr(); printf("Enter the year : "); scanf("%d",&year); year%4==0?printf("\nLeap year"):printf("\nNot Leap year"); getch(); }
/* 3.2 Determine the largest of three integer qtys. */ #include<stdio.h> #include void main() { int a,b,c; clrscr(); printf("Enter three numbers : "); scanf("%d%d%d",&a,&b,&c); (a>b)?(a>c)?printf("\n%d is greater",a):printf("\n%d is greater",c):(b>c)?printf("\n%d is greater",b):printf("\n%d is greater",c); getch(); }
/* 3.1 If cost price and selling price of an item is input through the keyboard write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he ocurred */ #include<stdio.h> #include void main() { float cp,sp,diff; clrscr(); printf("Enter cost price of item Rs. "); scanf("%f",&cp); printf("\nEnter selling price of item Rs. "); scanf("%f",&sp); if(sp>cp) { CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-3-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
diff = sp - cp; printf("\nIncurred profit Rs.%0.2f",diff); } else { diff = cp - sp; printf("\nIncurred loss of Rs.%0.2f",diff); } getch(); }
/* 3.4 A five digit number is entered through keyboard Reverse that number and check whether that number is equal to original number or not ? */ #include<stdio.h> #include void main() { int num,n,a,b,c,d,e; clrscr(); printf("Enter a five digit number : "); scanf("%d",&num); n = num; e = num / 10000; num = num % 10000; d = num / 1000; num = num % 1000; c = num / 100; num = num % 100; b = num / 10; num = num % 10; a = num; num = a*10000 + b*1000 + c*100 + d*10 + e; printf("\nOriginal number is : %d",n); printf("\nReversed number is : %d",num); if(num == n) printf("\nBoth are equal..."); else printf("\nBoth are not equal..."); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-4-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
getch(); }
/* 3.5 Perform the addition and display the numbers */ #include<stdio.h> #include void main() { int x,y,z; clrscr(); printf("Enter two numbers : "); scanf("%d%d",&x,&y); z = x + y; printf("\nAddition is : %d",z); getch(); }
Experiment No. 4 /* 4.1 Find real roots of quadratic equation axý+bx+c */ #include<stdio.h> #include void main() { int a,b,c; clrscr(); printf("Enter values of a,b,c : "); scanf("%d%d%d",&a,&b,&c); z = x + y; printf("\nAddition is : %d",z); getch(); }
/* 4.2 Find whether entered number is prime or not */ #include<stdio.h> #include void main() { int i=2,num; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-5-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
clrscr(); printf("Enter the number : "); scanf("%d",&num); prime: if(num%i==0) goto down; i++; if(i
/* 4.3 Enter marks of five subjects. Find the division of students */ #include<stdio.h> #include void main() { int m1,m2,m3,m4,m5,total,avg; clrscr(); printf("Enter the marks of five subjects : "); scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5); total = m1+m2+m3+m4+m5; avg = total/5; printf("\nYou obtained grade : "); if(avg>=75) printf("A+"); else if(avg>=60) printf("A"); else if(avg>=60) printf("A"); else if(avg>=50) printf("B"); else if(avg>=40) CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-6-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
printf("C"); else printf("D"); getch(); }
/* 4.4 Find the gross salary of the employee. If the salary is less than 2000, then HRA = 11% of BS and DA is 80% of BS. If the salary is equal to or above 2000,then HRA = Rs. 600 and DA = 95% of BS. Basic Salary (BS)of the employee is input */ #include<stdio.h> #include void main() { float bs,gs,hra,da ; clrscr(); printf("Enter the basic salary : "); scanf("%f",&bs); if(bs<2000) { hra = (11.00/100.00)*bs; da = (80.00/100.00)*bs; } else { hra = 600.00; da = (95.00/100.00)*bs; } gs = bs + hra + da; printf("\nGross Salary is : %.2f",gs); getch(); }
/* 4.5 Find whether the number is even or odd */ #include<stdio.h> #include void main() { int num ; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-7-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
clrscr(); printf("Enter the number : "); scanf("%d",&num); if(num%2==0) printf("\nNumber %d is even",num); else printf("\nNumber %d is odd",num); getch(); }
Experiment No. 5 /* 5.1 Menu driven program for 1. Find whether the number is odd/even 2. Display sum of input five numbers 3. Exit */ #include<stdio.h> #include #include<process.h> void main() { int num,choice,n1,n2,n3,n4,n5; clrscr(); printf("\n\t\t\t Menu"); printf("\n\t\t1. Find odd/even number"); printf("\n\t\t2. Display sum of five numbers"); printf("\n\t\t3. Exit"); printf("\n\t\tEnter the choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\n\t\tEnter the number : "); scanf("%d",&num); if(num%2==0) printf("\n\t\tNumber %d is even",num); else printf("\n\t\tNumber %d is odd",num); break; case 2: printf("\n\t\tEnter five numbers : "); scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5); printf("\n\t\tTotal : %d",n1+n2+n3+n4+n5); break; case 3: CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-8-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
exit(0); default: printf("\n\t\tWrong choice...!"); }
/* 5.2 Menu driven program for 1. Find square of the number 2. Print number in reverse order 3. Exit */ #include<stdio.h> #include #include<process.h> void main() { int num,choice,leap,a,b,c,d,e; clrscr(); printf("\n\t\t\t Menu"); printf("\n\t\t1. Find square of the number"); printf("\n\t\t2. Print 5 digit no. in reverse"); printf("\n\t\t3. Exit"); printf("\n\t\tEnter the choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\n\t\tEnter the number : "); scanf("%d",&num); printf("\n\t\t%dý = %d",num,num*num); break; case 2: printf("\n\t\tEnter a five digit number : "); scanf("%d",&num); e = num / 10000; num = num % 10000; d = num / 1000; num = num % 1000; c = num / 100; num = num % 100; b = num / 10; num = num % 10; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
-9-
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
a = num; num = a*10000 + b*1000 + c*100 + d*10 + e; printf("\n\t\tReversed number is : %d",num); break; case 3: exit(0); default: printf("\n\t\tWrong choice...!"); } getch(); }
/* 5.3 Menu driven program for 1. Find whether the number is -ve/+ve 2. Find whether leap year or not 3. Exit */ #include<stdio.h> #include #include<process.h> void main() { int num,choice,leap; clrscr(); printf("\n\t\t\t Menu"); printf("\n\t\t1. Find -ve/+ve number"); printf("\n\t\t2. Find leap or not"); printf("\n\t\t3. Exit"); printf("\n\t\tEnter the choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\n\t\tEnter the number : "); scanf("%d",&num); if(num<0) printf("\n\t\tNumber %d is positive",num); else printf("\n\t\tNumber %d is negative",num); break; case 2: printf("\n\t\tEnter the year : "); scanf("%d",&leap); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 10 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
if(leap%4==0) printf("\n\t\tYear %d is leap",leap); else printf("\n\t\tYear %d is not leap",leap); break; case 3: exit(0); default: printf("\n\t\tWrong choice...!"); } getch(); }
Experiment No. 6 /* 6.1 Calculate the sum of N elements */ #include<stdio.h> #include void main() { int N,num,sum=0,i=0; clrscr(); printf("\nHow many numbers ? "); scanf("%d",&N); while(i!=N) { printf("\nEnter number : "); scanf("%d",&num); sum = sum + num; i++; } printf("\nSum : %d",sum); getch(); }
/* 6.2 Print sum of digits of given number */ #include<stdio.h> #include void main() { int num,sum=0; clrscr(); printf("\nEnter the number : "); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 11 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
scanf("%d",&num); while(num>0) { sum = sum + (num%10); num = num / 10; } printf("\nSum of digits : %d",sum); getch(); }
/* 6.3 Calculate factorial of given number */ #include<stdio.h> #include void main() { int num,fact=1,i; clrscr(); printf("\nEnter the number : "); scanf("%d",&num); for(i=num;i>0;i--) fact = fact * i; printf("\nFactorial : %d",fact); getch(); }
/* 6.4 Find sum of series using while 1+3+5+7+...
*/
#include<stdio.h> #include void main() { int num,sum=0,i=1; clrscr(); printf("\nHow many numbers ? "); scanf("%d",&num); while(i
- 12 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
}
/* 6.5 Sum of series 0+1+3+6+10+15+21 with do-while */ #include<stdio.h> #include void main() { int num,sum=0,i=0,x=0; clrscr(); printf("\nHow many numbers ? "); scanf("%d",&num); printf("\n"); do { sum = sum + i; printf("%d + ",sum); x = x + sum; i++; } while (num>i); printf(" : %d",x); getch(); }
Experiment No. 7 /* 7.1 Search a particular number in array */ #include<stdio.h> #include void main() { int a[10],i,num; clrscr(); printf("\nEnter 10 numbers : "); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("\nEnter the number to search : "); scanf("%d",&num); for(i=0;i<10;i++) { if(a[i]==num) break; } if(i==10) CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 13 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
printf("\nNumber not present..."); else printf("\nNumber is present..."); getch(); }
/* 7.2 Sort the array in ascending order */ #include<stdio.h> #include void main() { int a[10],i,j,temp; clrscr(); printf("\nEnter 10 numbers : "); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) for(j=i;j<10;j++) if(a[i]>a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } printf("\nSorted Array : \n"); for(i=0;i<10;i++) printf("\n%d",a[i]); getch(); }
/* 7.3 Find smallest number in array */ #include<stdio.h> #include void main() { int a[10],i,min; clrscr(); printf("\nEnter 10 numbers : "); for(i=0;i<10;i++) scanf("%d",&a[i]); min = a[0]; for(i=1;i<10;i++) if(a[i]<min) CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 14 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
min = a[i]; printf("\nSmallest number : %d",min); getch(); }
/* 7.4 Find union of array */ #include<stdio.h> #include void main() { int a[5],b[5],c[10],k=5,i,j; clrscr(); printf("\nEnter first array : "); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("\nEnter second array : "); for(i=0;i<5;i++) scanf("%d",&b[i]); for(i=0;i<5;i++) c[i] = a[i]; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(c[j]==b[i]) break; } if(j==5) { c[k] = b[i]; k++; } } printf("\nResultant array : "); for(i=0;i
/* 7.5 Average of marks of 10 students */ CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 15 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
#include<stdio.h> #include void main() { int marks[10],i,total=0; float avg; clrscr(); printf("\nEnter marks of 10 students : "); for(i=0;i<10;i++) { scanf("%d",&marks[i]); total = total + marks[i]; } avg = (float)total / 10.00f; printf("\nAverage : %.2f",avg); getch(); }
Experiment No. 8 /* 8.1 Search a particular element in 2D array */ #include<stdio.h> #include void main() { int a[3][3],num,i,j,flag=0; clrscr(); printf("\nEnter matrix : "); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter number to search : "); scanf("%d",&num); for(i=0;i<3;i++) for(j=0;j<3;j++) if(a[i][j]==num) flag = 1; if(flag==1) printf("\nNumber is present..."); else printf("\nNumber is absent..."); getch(); }
CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 16 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
/* 8.1 Read 2D matrix and print it */ #include<stdio.h> #include void main() { int a[3][3],i,j; clrscr(); printf("\nEnter matrix : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nMatrix is :\n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(" %d ",a[i][j]); printf("\n"); } getch(); }
/* 8.3 Read two matrices and print their addition */ #include<stdio.h> #include void main() { int a[3][3],b[3][3],i,j; clrscr(); printf("\nEnter first matrix : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter second matrix : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); printf("\nAddition : \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(" %d ",a[i][j] + b[i][j]); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 17 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
printf("\n"); } getch(); }
/* 8.4 Find max of each row and column of matrix */ #include<stdio.h> #include void main() { int a[3][3],i,j,max; clrscr(); printf("\nEnter the matrix : \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) { max = a[i][0]; for(j=1;j<3;j++) if(a[i][j]>max) max = a[i][j]; printf("\nMax of row%d: %d",i+1,max); } printf("\n"); for(j=0;j<3;j++) { max = a[0][j]; for(i=1;i<3;i++) if(a[i][j]>max) max = a[j][i]; printf("\nMax of column%d: %d",j+1,max); } getch(); }
/* 8.5 Find transpose of 4*4 matrix */ #include<stdio.h> #include void main() { int a[4][4],i,j,max; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 18 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
clrscr(); printf("\nEnter the 4 X 4 matrix : \n"); for(i=0;i<4;i++) for(j=0;j<4;j++) scanf("%d",&a[i][j]); printf("\nTranspose is : \n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) printf("%3d",a[j][i]); printf("\n"); } getch(); }
Experiment No. 9 /* 9.1 Arrange name of 5 students in alphabetical order */ #include<stdio.h> #include #include<string.h> void main() { char name[5][10],temp[10]; int i,j; clrscr(); printf("\nEnter five names : "); for(i=0;i<5;i++) scanf("%s",name[i]); for(i=0;i<5;i++) for(j=i;j<5;j++) if(strcmpi(name[i],name[j])>0) { strcpy(temp,name[i]); strcpy(name[i],name[j]); strcpy(name[j],temp); } printf("\nNames are : "); for(i=0;i<5;i++) printf("\n%s",name[i]); getch(); }
CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 19 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
/* 9.2 Check the string is palindrome or not */ #include<stdio.h> #include #include<string.h> void main() { char str[10]; int i,j; clrscr(); printf("\nEnter the string : "); scanf("%s",str); for(i=0,j=strlen(str)-1;i<strlen(str)/2;i++,j--) if(str[i]!=str[j]) break; if(i==j) printf("\nPalindrome..."); else printf("\nNot Palindrome..."); getch(); }
/* 9.3 Check for correct password and username */ #include<stdio.h> #include #include<string.h> void main() { char user[10],pass[10]; int i=0,j; clrscr(); do { printf("\nEnter user name : "); scanf("%s",user); printf("\nEnter password : "); scanf("%s",pass); if(strcmp("Admin",user)==0) { if(strcmp("Admin",pass)==0) { printf("\nCorrect password"); break; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 20 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
} else printf("\nWrong password...try again.\n"); } else printf("\nIncorrect username...try again.\n"); i++; }while(i<3); getch(); }
/* 9.4 Arrange the string in alphabetical order */ #include<stdio.h> #include #include<string.h> void main() { char str[10],temp; int i,j; clrscr(); printf("\nEnter the string : "); scanf("%s",str); for(i=0;i<strlen(str);i++) for(j=0;j<strlen(str);j++) if(str[i]<str[j]) { temp = str[i]; str[i] = str[j]; str[j] = temp; } printf("Reversed String is : %s",str); getch(); }
/* 9.5 Count and display no. of vowels in string */ #include<stdio.h> #include #include<string.h> void main() { char str[10]; int i,vowels = 0; clrscr(); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 21 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
printf("\nEnter the string : "); scanf("%s",str); strlwr(str); for(i=0;i<strlen(str);i++) if(str[i]=='a' || str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u') vowels++; printf("No. of vowels are : %d",vowels); getch(); }
Experiment No. 10 /* 10.1 Function to exchange values of variables */ #include<stdio.h> #include int x,y; void exchange() { int temp; temp = x; x = y; y = temp; } void main() { clrscr(); printf("Enter two values : "); scanf("%d%d",&x,&y); exchange(); printf("\nSwapped values : %d %d",x,y); getch(); }
/* 10.2 Function to print Fibonacci series */ #include<stdio.h> #include void fibo(int n) { int n1,n2,i,fib; n1 = 0; n2 = 1; printf(" %d %d",n1,n2); for(i=0;i
- 22 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
fib = n2; n2 = n1 + n2; printf(" %d",n2); n1 = fib; } } void main() { int num; clrscr(); printf("Enter the number : "); scanf("%d",&num); printf("\n"); fibo(num); getch(); }
/* 10.3 Calculate sum of series 1+3+5+7 */ #include<stdio.h> #include int sum(int n) { int i,s=0; for(i=1;i<=n;i+=2) s = s + i; return s; } void main() { int num,n; clrscr(); printf("Enter the number : "); scanf("%d",&num); n = sum(num); printf("\nSum of series : %d",n); getch(); }
/* 10.4 Find product of two variables */ #include<stdio.h> #include int mul(int n,int m) { CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 23 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
int product; product = n * m; return product; } void main() { int x,y,n; clrscr(); printf("Enter two numbers : "); scanf("%d%d",&x,&y); n = mul(x,y); printf("\nMultiplication : %d",n); getch(); }
/* 10.5 Find prime numbers using function */ #include<stdio.h> #include void prime(int k) { int i,n; for(i=1;i
CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 24 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
Experiment No. 11 /* 11.1 Declare a structure 'Book' having data members title, author and price. Accept this data for one variable and disply the accepted data */ #include<stdio.h> #include struct book { char author[10]; char title[10]; float price; }a; void main() { clrscr(); printf("Enter title : "); gets(a.title); printf("Enter author : "); gets(a.author); printf("Enter price : "); scanf("%f",&a.price); printf("\nData is :\n"); printf("\nTitle: %s",a.title); printf("\nAuthor: %s",a.author); printf("\nPrice : %.2f",a.price); getch(); }
/* 11.2 Declare a structure 'struct_time' having data members hour, minutes, seconds. Accept this data and display the time in format 16:30:32*/ #include<stdio.h> #include struct struct_time { int hour; int minutes; int seconds; }a; void main() { CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 25 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
clrscr(); printf("Enter Hours : "); scanf("%d",&a.hour); printf("Enter Minutes : "); scanf("%d",&a.minutes); printf("Enter Seconds : "); scanf("%d",&a.seconds); printf("\nTime is : %d:%d:%d",a.hour,a.minutes, a.seconds); getch(); } /* 11.3 Declare a structure describing 'fruit_market' having data members item_name, item_price and fruit_stallname. Write a program to display stallnames in order of item_price for given item_name. */ #include<stdio.h> #include struct fruit_market { char item_name[10]; char fruit_stallname[10]; int item_price; }a[3]; void main() { int i; clrscr(); for(i=0;i<3;i++) { printf("Enter item name : "); scanf("%s",a[i].item_name); printf("Enter stall name : "); scanf("%s",a[i].fruit_stallname); printf("Enter price : "); scanf("%d",&a[i].item_price); } for(i=0;i<3;i++) { printf("\nPrice : %d",a[i].item_price); printf("\nItem name : %s",a[i].item_name); printf("\nStall name : %s",a[i].fruit_stallname); } getch(); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 26 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
}
/* 11.4 Declare a structure describing 'cricket' having data members name, team_name, batting avg. Read information of about 20 players and print team wise list containing names of players and their batting average. */ #include<stdio.h> #include #include<string.h> struct cricket { char name[10]; char tname[10]; int avg; }a[5]; void main() { int i; clrscr(); for(i=0;i<5;i++) { printf("Enter name : "); scanf("%s",a[i].name); printf("Enter team name : "); scanf("%s",a[i].tname); printf("Enter average : "); scanf("%d",&a[i].avg); } printf("\n\n\tTeam : IND"); for(i=0;i<5;i++) if(strcmpi(a[i].tname,"IND")==0) { printf("\nName : %s",a[i].name); printf("\nAvg : %d",a[i].avg); } printf("\n\n\tTeam : PAK"); for(i=0;i<5;i++) if(strcmpi(a[i].tname,"PAK")==0) { printf("\nName : %s",a[i].name); printf("\nAvg : %d",a[i].avg); } CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 27 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
printf("\n\n\tTeam : AUS"); for(i=0;i<5;i++) if(strcmpi(a[i].tname,"AUS")==0) { printf("\nName : %s",a[i].name); printf("\nAvg : %d",a[i].avg); } getch(); }
/* 11.5 Declare a structure describing 'circle' having data members as radius, perimeter and area. Accpet radius*/ #include<stdio.h> #include struct circle { int radius; float peri; float area; }a; void main() { clrscr(); printf("Enter radius : "); scanf("%d",&a.radius); a.peri = 2.0 * 3.14 * (float) a.radius; printf("\nPerimeter : %.2f",a.peri); a.area = 3.14 * (float)(a.radius * a.radius); printf("\n\nArea: %.2f",a.area); getch(); }
CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 28 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
Experiment No. 12 /* 12.2 Compare two strings and concatenate if they are not equal using pointers */ #include<stdio.h> #include void main() { int *s1,*s2; clrscr(); printf("\nEnter first string : "); scanf("%s",s1); printf("\nEnter second string : "); scanf("%s",s2); if(strcmp(s1,s2)!=0) { strcat(s1,s2); printf("\nConcatenated string : %s",s1); } getch(); }
/* Sort integer array using pointers */ #include<stdio.h> #include void main() { int a[5]; int *p,i,j,temp; clrscr(); printf("\nEnter five elements in array : "); for(i=0;i<5;i++) scanf("%d",&a[i]); p = a; for(i=0;i<5;i++) for(j=i;j<5;j++) if(*(p+i) > *(p+j)) { temp = *(p+i); *(p+i) = *(p+j); CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 29 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
*(p+j) = temp; } printf("\nSorted Array : "); for(i=0;i<5;i++,p++) printf(" %d",*p); getch(); }
/* 12.4 Exchange values of 2 variables using pointers */ #include<stdio.h> #include void main() { int a,b; int *p, *q, *r; clrscr(); printf("\nEnter first variable : "); scanf("%d",&a); printf("\nEnter second variable : "); scanf("%d",&b); p = &a; q = &b; *r = *p; *p = *q; *q = *r; printf("\nExchanged Values are : %d %d",a,b); getch(); }
/* 12.5 Reverse the string using pointers */ #include<stdio.h> #include void main() { int i, count=0; char *str; CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 30 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
clrscr(); printf("\nEnter string : "); scanf("%s",str); while(*str!='\0') { str++; count++; } printf("\nReversed String : "); for(i=count;i>=0;i--) { printf("%c",*str); str--; } getch(); }
CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 31 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
Experiment No. 13 /* 13.1 Declare an array of pointer. Initialize it with addresses of other array and display array */ #include<stdio.h> #include void main() { int a[10],*p[10],i; printf("\nEnter 10 Elements : "); for(i=0;i<10;i++) canf("%d",&a[i]); for(i=0;i<10;i++) [i] = &a[i]; printf("\nArray Elements are : \n"); for(i=0;i<10;i++) { printf("\n\tAddress : %u = Element: %d",p[i],*p[i]); } }
/* 13.2 Perform addition of matrix using pointers */ #include<stdio.h> #include void main() { int mat1[3][3] = {4,5,6, 1,2,5, 7,1,2 }; int mat2[3][3] = {4,5,6, 1,2,5, 7,1,2 }; int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(" %d",*(*(mat1+i)+j) + *(*(mat2+i)+j)); printf("\n"); } getch(); } CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 32 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
Experiment No. 14 /* 14.1 Use command-line arguments to print hello and your name on the screen, if you specify your name on the command-line. */ #include<stdio.h> #include main(int argc, char *argv[]) { printf("Hello %s",argv[1]); }
/* 14.2 Use command-line arguments to print all the arguments in reverse order */ #include<stdio.h> #include main(int argc, char *argv[]) { printf("Your arguments are : "); while(argc) { argc--; printf("\n %s",argv[argc]); } }
/* 14.3 Use command-line arguments to print addition of all arguments passed to it. */ #include<stdio.h> #include<stdlib.h> main(int argc, char *argv[]) { int add = 0; while(argc>0) { argc--; add = add + atoi(argv[argc]); } printf("\nAddition is : %d",add); } CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 33 -
Programs solution of ‘Programming in C (9017)’ manual of FYIF (2007-2008)
- Programmed by, Tushar B Kute, Lecturer in Information Technology, (Subject Teacher – Programming in ‘C’) K. K. Wagh Polytechnic, Nashik – 03. [email protected]
CPR (FYIF) by Mr. Kute T. B. (Lecturer in IT, KK Wagh Polytechnic, Nashik 2007-2008)
- 34 -