c lab questions
TRANSCRIPT
1.//Program to find largest of three numbers#include<stdio.h>#include<conio.h>
int main(){
int num1,num2,num3;clrscr();printf("\n Enter Three numbers : ");scanf("%d%d%d",&num1,&num2,&num3);if(num1<=0||num2<=0||num3<=0)
printf("\n Unexpected input....");else if(num1>num2&&num1>num3)
printf("\n Largest is %d",num1);else if(num2>num1&&num2>num3)
printf("\n Largest is %d",num2);else
printf("\n Largest is %d",num3);getch();return 0;
}/*OUTPUT 1========Enter Three numbers : 15 12 5Largest is 15
OUTPUT 2========Enter Three numbers : 25 60 7Largest is 60
OUTPUT 3========Enter Three numbers : 28 9 45Largest is 45*/2.//Program to check whether the given string is palindrome or not#include<stdio.h>#include<conio.h>#include<string.h>
int main(){
char str[25];int length,i,j,flag=1;clrscr();
printf("Enter the string : ");scanf("%s",&str);length=strlen(str);j=length-1;for(i=0;i<length/2;i++,j--){
if(str[i]!=str[j]){
flag=0;break;
}}if(flag==1)
printf("\n Entered string is palindrome..");else
printf("\ Entered string is not palindome..");getch();return 0;
}/*OUTPUT 1========Enter the string : englishEntered string is not palindome..
OUTPUT 2========Enter the string : malayalamEntered string is palindome..
*/3.//Program to find whether a given number is prime
#include<stdio.h>#include<conio.h>#include<process.h>
int main(){
int num,flag=1,i=2;clrscr();start:printf("\n Enter Number to be checked : ");scanf("%d",&num);while(i<=num/2){
if((num%i)==0)
{flag=0;printf("\n Not a prime number..");break;
}i++;
}if(flag==1)
printf("\n Prime number..");getch();return 0;
}
/*
OUTPUT 1========Enter Number to be checked : 4Not a prime number..
OUTPUT 2========Enter Number to be checked : 7Prime number..*/
4.// Program to check whether a given number is armstrong number or not
#include<stdio.h>#include<conio.h>
int main(){
int num,rem=0,temp,sum=0;clrscr();printf("\n Enter number to be checked : ");scanf("%d",&num);temp=num;while(temp>0){
rem=temp%10;sum=sum+(rem*rem*rem);temp=temp/10;
}if(num==sum)
printf("\n Armstrong number..");else
printf("\n Not an armstrong number..");getch();return 0;
}
/*OUTPUT 1========Enter number to be checked : 754Not an armstrong number..
OUTPUT 1========Enter number to be checked : 153Armstrong number..*/
5.//program to print pascal triangle#include<stdio.h>#include<conio.h>
int main(){
int a[20][20],i,j,n,spc=25,k;printf("enter the number of rows:");scanf("%d",&n);for(i=0;i<n;i++) //outer loop for rows{
for(k=spc-2*i;k>=0;k--)printf(" "); //to print the spacesfor(j=0;j<=i;j++) //inner loop for columns{
if(j==0||i==j){
a[i][j]=1;}else{
a[i][j]=a[i-1][j-1]+a[i-1][j];}printf("%4d",a[i][j]);
}printf("\n\n");
}getch();return 0;return 0;
}/*OUTPUT======enter the number of rows:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1*/
6.//Find the sum and average of n integer using linear array
#include<stdio.h>#include<conio.h>
int main(){
int lim,arr[100],sum=0,i=0;float avg=0;clrscr();printf("\nEnter the number of elements : ");scanf("%d",&lim);printf("\nEnter elements : ");while(i<lim){
scanf("%d",&arr[i]);sum+=arr[i];i++;
}avg=sum/lim;printf("\n Sum of %d elements is %d",lim,sum);printf("\n Average of %d elements is %f",lim,avg);getch();return 0;
}
/*OUTPUT
======
Enter the number of elements : 10
Enter elements : 12345678910
Sum of 10 elements is 55Average of 10 elements is 5.000000
*/
7.// Matrix addition, multiplication & transpose#include<stdio.h>#include<conio.h>
int main(){
int ch;void addition(void);void mult();void transpose();clrscr();
printf("1.Matrix addition\n");printf("2.Matrix multiplication\n");printf("3.Transpose of a matrix\n");printf("4.Exit");printf("\nEnter your choice : ");scanf("%d",&ch);switch(ch){
case 1:addition();break;
case 2:mult();
break;case 3:transpose();
break;case 4:exit(1);
break;default:printf("\nInvalid entry...");
break;}getch();return 0;
}
void addition(){
int mat1[20][20],mat2[20][20],resMat[20][20];int col1,row1,col2,row2,i,j,k;printf("Enter the number of rows and columns of matrix 1 : ");scanf("%d%d",&row1,&col1);printf("Enter the number of rows and columns of matrix 2 : ");scanf("%d%d",&row2,&col2);printf("Enter the elements of 1st matrix (row wise): \n");for(i=0;i<row1;i++)
for(j=0;j<col1;j++)scanf("%d",&mat1[i][j]);
printf("Enter the elements of 2nd matrix (row wise): \n");for(i=0;i<row2;i++)
for(j=0;j<col2;j++)scanf("%d",&mat2[i][j]);
//matrix additionprintf("\nResult \n");if(row1==row2 && col1==col2){
for(i=0;i<row1;i++)for(j=0;j<col1;j++)
resMat[i][j]=mat1[i][j]+mat2[i][j];//Display result-addition
for(i=0;i<row1;i++){
for(j=0;j<col1;j++)printf("%d\t",resMat[i][j]);
printf("\n");}
}else
printf("\nAddition cannot be performed\n");
}void mult(){
int mat1[20][20],mat2[20][20],resMat[20][20];int col1,row1,col2,row2,i,j,k;printf("Enter the number of rows and columns of matrix 1 : ");scanf("%d%d",&row1,&col1);printf("Enter the number of rows and columns of matrix 2 : ");scanf("%d%d",&row2,&col2);//read matrix 1printf("Enter the elements of 1st matrix (row wise): \n");for(i=0;i<row1;i++)
for(j=0;j<col1;j++)scanf("%d",&mat1[i][j]);
//read matrix 2printf("Enter the elements of 2nd matrix (row wise): \n");
for(i=0;i<row2;i++)for(j=0;j<col2;j++)
scanf("%d",&mat2[i][j]);//calculationprintf("\nResult \n");if(row1==col2){
for(i=0;i<row1;i++){
for(j=0;j<col2;j++){
resMat[i][j]=0;for(k=0;k<row1;k++)
resMat[i][j]+=mat1[i][k]*mat2[k][j];}
}//Result printing
for(i=0;i<row1;i++){
for(j=0;j<col2;j++)printf("%d\t",resMat[i][j]);
printf("\n");
}}else
printf("Multiplication is not posible..\n");
}void transpose()
{int mat[20][20],i,j,row,col;printf("Enter the number of rows and columns of matrix : ");scanf("%d%d",&row,&col);printf("Enter the elements of 2nd matrix (row wise): \n");
for(i=0;i<row;i++)for(j=0;j<col;j++)
scanf("%d",&mat[i][j]);printf("\nTranspose of Matrix : \n");//calculationfor(j=0;j<col;j++){
for(i=0;i<row;i++)printf("%d\t",mat[i][j]);
printf("\n");}
}/*OUTPUT 1========1.Matrix addition2.Matrix multiplication3.Transpose of a matrix4.ExitEnter your choice : 1Enter the number of rows and columns of matrix 1 : 2 3Enter the number of rows and columns of matrix 2 : 2 3Enter the elements of 1st matrix (row wise):4 5 67 8 9Enter the elements of 2nd matrix (row wise):1 4 67 8 5
Result5 9 1214 16 14
OUTPUT 2========1.Matrix addition2.Matrix multiplication3.Transpose of a matrix4.ExitEnter your choice : 2Enter the number of rows and columns of matrix 1 : 3 3
Enter the number of rows and columns of matrix 2 : 2 3Enter the elements of 1st matrix (row wise):2 3 45 2 13 4 7Enter the elements of 2nd matrix (row wise):2 3 48 7 6
Result28 27 2626 29 3238 37 36
OUTPUT 3========1.Matrix addition2.Matrix multiplication3.Transpose of a matrix4.ExitEnter your choice : 3Enter the number of rows and columns of matrix : 3 3Enter the elements of 2nd matrix (row wise):4 5 79 6 12 8 3
Transpose of Matrix :4 9 25 6 87 1 3*/
8.// Program to find factorial of n by recursion
#include<stdio.h>#include<conio.h>
int main(){
int n,fact;int recFact(int);clrscr();printf("\nEnter number: ");scanf("%d",&n);fact=recFact(n);
printf("\nFactorial of %d is %d ",n,fact);getch();return 0;
}
int recFact(int x){
if(x==1)return 1;
elsereturn x*recFact(x-1);
}
/*
OUTPUT======
Enter number: 5
Factorial of 5 is 120*/
9.//String conactination, comparison and reverse using user defined fun
#include<stdio.h>#include<conio.h>
int main(){
void stringConcat();void stringMatch();void stringRev();int ch;clrscr();printf("1.Concatinate Strings\n");printf("2.Compare Strings\n");printf("3.Reverse a String\n");printf("Enter your choice : ");scanf("%d",&ch);switch(ch){
case 1:stringConcat();break;
case 2:stringMatch();break;
case 3:stringRev();break;
default:printf("\nInvalid entry..");break;
}getch();return 0;
}
void stringConcat(){
int i=0,j=0;char str1[20],str2[20],str3[40];printf("\nEnter string 1 : ");scanf("%s",&str1);printf("\nEnter string 2 : ");scanf("%s",&str2);
while(str1[i]!='\0'){
str3[i]=str1[i];i++;
}
while(str2[j]!='\0'){
str3[i]=str2[j];i++;j++;
}str3[i]='\0';printf("\nConcatinated word is %s",str3);
}void stringMatch(){
int i=0,flag=1;char str1[20],str2[20],str3[40];printf("\nEnter string 1 : ");scanf("%s",&str1);printf("\nEnter string 2 : ");scanf("%s",&str2);while(str1[i]!='\0'|| str2[i]!='\0'){
if(str1[i]!=str2[i]){
flag=0;break;
}i++;
}if(flag==1)
printf("\nStrings are equal..");else
printf("\nStrings are not equal..");}
void stringRev(){
char str[20];int i=0,c=0;printf("\nEnter a string : ");scanf("%s",&str);while(str[i]!='\0'){
c++;i++;
}while(c>=0){
printf("%c",str[c]);c--;
}}
10.// find sum of series//n - n*(2/2!)+n*(3/3!)-(n*4/4!)+.......
#include<stdio.h>//#include<conio.h>int recFact(int);
int main(){
int lim,i=1,sign=-1,x=0;double s=0;clrscr();printf("\nEnter the limit n : ");scanf("%d",&lim);while(i<=lim){
sign=-sign;
x=recFact(i);s=s+(sign*lim*(double)i/(double)x);i++;
}for(i=1;i<=lim;i++){
if(i%2==0)printf("-%d*(%d/%d!)",lim,i,i);
elseprintf("+%d*(%d/%d!)",lim,i,i);
}printf("\n Sum of the series is %lf",s);getch();return 0;return 0;
}
int recFact(int x){
if(x==1)return 1;
elsereturn (x*recFact(x-1));
}
/*
OUTPUT 1========
Enter the limit n : 4+4*(1/1!)-4*(2/2!)+4*(3/3!)-4*(4/4!) Sum of the series is 1.333333
OUTPUT 2 ========
Enter the limit n : 2+2*(1/1!)-2*(2/2!) Sum of the series is 0.000000
*/
11.//swap using call by value and call by reference#include<stdio.h>#include<conio.h>
void swapVal(int x,int y);void swapRef(int *,int *);
int main(){
int num1,num2;clrscr();printf("\nEnter the first value : ");scanf("%d",&num1);printf("Enter the second value: ");scanf("%d",&num2);printf("\n\nUsing call by value \n");printf("Values before swapping x=%d y=%d ",num1,num2);swapVal(num1,num2);
printf("\n\nUsing call by refference \n");printf("\nValues before swapping x=%d y=%d ",num1,num2);swapRef(&num1,&num2);printf("\nValues after swapping x=%d y=%d ",num1,num2);getch();return 0;return 0;
}
void swapVal(int x,int y){
int temp;temp=x;x=y;y=temp;printf("\nValues after swapping x=%d y=%d ",x,y);
}void swapRef(int *x,int *y){
int temp;temp=*x;*x=*y;*y=temp;
}
/*OUTPUT======
Enter the first value : 10Enter the second value: 20
Using call by valueValues before swapping x=10 y=20Values after swapping x=20 y=10
Using call by refference
Values before swapping x=10 y=20Values after swapping x=20 y=10*/
12.//array sorting using dynamic memory allocation
#include<stdio.h>#include<conio.h>#include<stdlib.h>
int main(){
int *arr,lim,i,temp,j;clrscr();printf("\n Enter the size of array : ");scanf("%d",&lim);arr=(int*)malloc(lim * sizeof(int));printf("\n Enter the elements : ");for(i=0;i<lim;i++)
scanf("%d",&arr[i]);for(i=0;i<lim;i++)
for(j=0;j<lim;j++){
if(arr[i]<arr[j]){
temp=arr[i];arr[i]=arr[j];arr[j]=temp;
}}
printf("\n Sorted elements are\n");for(i=0;i<lim;i++){
printf("\t%d",arr[i]);
}free(arr);
getch();return 0;}
/*OUTPUT======
Enter the size of array : 10
Enter the elements : 5 6 2 1 45 78 25 30 15 3 8
Sorted elements are1 2 3 5 6 15 25 30 45 78*/13.//program to display the mark sheet of a student using strucute
#include<stdio.h>#include<conio.h>
struct student{
int roll,mark1,mark2,mark3,total;char name[20];float avg;
}st;
int main(){
clrscr();printf("Enter roll number : ");scanf("%d",&st.roll);printf("\nEnter the name : ");scanf("%s",&st.name);printf("\nMark obtained for subject1 : ");scanf("%d",&st.mark1);printf("\nMark obtained for subject2 : ");scanf("%d",&st.mark2);printf("\nMark obtained for subject3 : ");scanf("%d",&st.mark3);st.total=st.mark1+st.mark2+st.mark3;st.avg=st.total/3;printf("\nRoll Nmber : %d",st.roll);printf("\nName : %s",st.name);printf("\nSubject Max Mark | Obt Mark");printf("\nSubject1: 50 | %d",st.mark1);printf("\nSubject2: 50 | %d",st.mark2);
printf("\nSubject3: 50 | %d",st.mark3);printf("\
n=========================================");
printf("\nTotal : 150 | %d",st.total);printf("\
n=========================================");
printf("\nPercentage : 100 | %.2f%",st.avg);printf("\
n=========================================");
getch();return 0;}
/*OUTPUT======Enter roll number : 10
Enter the name : Rejo
Mark obtained for subject1 : 75
Mark obtained for subject2 : 82
Mark obtained for subject3 : 72
Roll Nmber : 10Name : RejoSubject Max Mark | Obt MarkSubject1 : 50 | 75Subject2 : 50 | 82Subject3 : 50 | 72=========================================Total : 150 | 229=========================================Percentage : 100 | 76.00%=========================================*/14.-15// write to data file, read from data file#include<stdio.h>#include<conio.h>
int main(){
FILE *fp;fp=fopen("data.dat","w");char ch;printf("\nEnter the data:");printf("\n(Enter ~ to store contents)\n");while((ch=getchar())!='~'){
putc(ch,fp);}fclose(fp);fp=fopen("data.dat","r");printf("\nContents of the file is : ");do{
ch=fgetc(fp);printf("%c",ch);
}while(ch!=EOF);fclose(fp);getch();return 0;
}
16./*Program to copy the contents of a file into another.
Before running this program create a text file "test.txt"with some contents in c:\tc\bin\.The c source file mustbe saved with the name "comm" and must be compiled.Afterthat goto Compile->Make.Then in TC interface gotoFILE->DOS shell and type "comm test.txt test1.txt".*/
#include<stdio.h>#include<conio.h>
void main(int argc,char *argv[]){
FILE *fpr,*fpw;char ch;if(argc!=3){
printf("\nInsufficient numebr of arguments..");exit(0);
}
fpr=fopen(argv[1],"r");fpw=fopen(argv[2],"w");if(!fpr){
printf("\nFile not found...");exit(1);
}while((ch=fgetc(fpr))!=EOF){
fputc(ch,fpw);}fclose(fpr);fclose(fpw);printf("\nFile copied....");getch();
}
/*
OUTPUT======(Before running this program create a text file "test.txt" withsome contents in c:\tc\bin\.The c source file must besaved in the name "comm" and compiled.Then in TC interface goto FILE->DOS shelland type "comm test.txt test1.txt". )
C:\TC\BIN>comm test.txt test1.txt
File copied....*/