C Lab Questions
C Lab Questions
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 5
Largest is 15
OUTPUT 2
========
Enter Three numbers : 25 60 7
Largest is 60
OUTPUT 3
========
Enter Three numbers : 28 9 45
Largest 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 : english
Entered string is not palindome..
OUTPUT 2
========
Enter the string : malayalam
Entered 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 : 4
Not a prime number..
OUTPUT 2
========
Enter Number to be checked : 7
Prime number..
*/
#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 : 754
Not an armstrong number..
OUTPUT 1
========
Enter number to be checked : 153
Armstrong number..
*/
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 spaces
for(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
*/
#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 : 1
2
3
4
5
6
7
8
9
10
Sum of 10 elements is 55
Average of 10 elements is 5.000000
*/
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 addition
printf("\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 1
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]);
//read matrix 2
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]);
//calculation
printf("\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");
//calculation
for(j=0;j<col;j++)
{
for(i=0;i<row;i++)
printf("%d\t",mat[i][j]);
printf("\n");
}
}
/*
OUTPUT 1
========
1.Matrix addition
2.Matrix multiplication
3.Transpose of a matrix
4.Exit
Enter your choice : 1
Enter the number of rows and columns of matrix 1 : 2 3
Enter the number of rows and columns of matrix 2 : 2 3
Enter the elements of 1st matrix (row wise):
456
789
Enter the elements of 2nd matrix (row wise):
146
785
Result
5 9 12
14 16 14
OUTPUT 2
========
1.Matrix addition
2.Matrix multiplication
3.Transpose of a matrix
4.Exit
Enter your choice : 2
Enter the number of rows and columns of matrix 1 : 3 3
Enter the number of rows and columns of matrix 2 : 2 3
Enter the elements of 1st matrix (row wise):
234
521
347
Enter the elements of 2nd matrix (row wise):
234
876
Result
28 27 26
26 29 32
38 37 36
OUTPUT 3
========
1.Matrix addition
2.Matrix multiplication
3.Transpose of a matrix
4.Exit
Enter your choice : 3
Enter the number of rows and columns of matrix : 3 3
Enter the elements of 2nd matrix (row wise):
457
961
283
Transpose of Matrix :
4 9 2
5 6 8
7 1 3
*/
#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;
else
return x*recFact(x-1);
}
/*
OUTPUT
======
Enter number: 5
Factorial of 5 is 120
*/
#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--;
}
}
#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);
else
printf("+%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;
else
return (x*recFact(x-1));
}
/*
OUTPUT 1
========
OUTPUT 2
========
*/
/*
OUTPUT
======
#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;
}
}
}
free(arr);
getch();return 0;
}
/*
OUTPUT
======
#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
Roll Nmber : 10
Name : Rejo
Subject Max Mark | Obt Mark
Subject1 : 50 | 75
Subject2 : 50 | 82
Subject3 : 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.
#include<stdio.h>
#include<conio.h>
/*
OUTPUT
======
(Before running this program create a text file "test.txt" with
some contents in c:\tc\bin\.The c source file must be
saved in the name "comm" and compiled.
Then in TC interface goto FILE->DOS shell
and type "comm test.txt test1.txt". )
File copied....
*/