CTC Lab Record by 20B01A12A7
CTC Lab Record by 20B01A12A7
CTC Lab Record by 20B01A12A7
M.L. Rishika*/
/*1. write a program that prompts the user to enter a distance in inches and outputs that
distance in yards and feet*/
#include <stdio.h>
int main()
float di,dy,df;
"); scanf("%f",&di);
dy=di/36,df=di/12;
printf("\nDistance in yards is
is %f",df);
return 0;
Output :
8 Distance in yards is
is 0.666667
/*20B01A12A7
M.L.Rishika*/
#include <stdio.h>
int main()
float C1,F1,C2,F2;
"); scanf("%f",&C1);
F1=(C1*9/5)+32;
printf("\nFahrenheit temperature is
C2=(F2-32)*5/9;
return 0;
Output:
98 Temperature in celsius is
36.666668
/*20B01A12A7
M.L.Rishika*/
/*3. Write a C program to find the largest of three numbers using nested if-else*/
#include <stdio.h>
int main()
int a,b,c;
");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
else
else
if(b>c)
{
printf("\n%d is largest among %d,%d and %d",b,a,b,c);
else
return 0;
Output :
M.L.Rishika*/
#include <stdio.h>
int main()
int a,b;
float
c,d;
"); scanf("%d%f",&a,&c);
b=(int)c;
d=(float)a
printf("\ninteger value of %f is
%d",c,b);
return 0;
Output:
/*5. write a program that displays all the numbers from x to y, that are
#include <stdio.h>
int main()
int x,y,a,b;
scanf("%d%d%d%d",&x,&y,&a,
&b);
",x,y,a,b); while(x<=y)
printf("%d ",x);
x++
return 0;
Output :
M.L.Rishika*/
/*6. Write a program that reads an unspecified number of integers, determines how
many positive and negative values have been read, and computes the total and
average of
the input values, not counting zeros. Your program ends with the input 0.
#include <stdio.h>
int main()
int
i,n=0,sum=0,cnt1=0,cnt2=0;
float avg;
do
printf("Enter a number :
"); scanf("%d",&i);
if(i>0)
cnt1=cnt1+1;
if(i<0)
cnt2=cnt2+1;
}
sum=sum+
i; n=n+1;
while(i!=0);
avg=sum/(n-
1);
return 0;
Output:
Enter a number :
3 Enter a number
: 1 Enter a
number: 0
Total is 4
M.L.Rishika*/
/*7. Write a C program for finding student Grade by reading marks as input*/
#include <stdio.h>
int main()
float e,m,ap,ctc,bee,t,p;
");
scanf("%f%f%f%f%f",&e,&m,&ap,&ctc,&b
ee); t=e+m+ap+ctc+bee;
p=(t/500)*100;
if(p>=90)
printf("\nGrade is O");
printf("\nGrade is E");
printf("\nGrade is A");
printf("\nGrade is B");
printf("\nGrade is C");
else
printf("\nFail");
return 0;
Output:
99 Grade is O
/*20B01A12A7
M.L.Rishika*/
ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2).
given the values of ‘u’ and ‘a’. The program should provide the flexibility to the user
to select his own time intervals and repeat the calculations for different values of ‘u’
and ‘a’.*/
#include <stdio.h>
int main()
int
i=1,n,t;
float
s,u,a;
"); scanf("%d",&n);
while(i<=n)
"); scanf("%d",&t);
:"); scanf("%f",&u);
:"); scanf("%f",&a);
s=s+(u*t+((a*t*t)/2))
; i++;
}
printf("\nTotal distance travelled is %f m",s);
return 0;
Output:
4.3
M.L.Rishika*/
/*9. Write a C program, which takes two integer operands and one operator
from the user,performs the operation and then prints the result.
#include <stdio.h>
int main()
int a,b;
char ch;
"); scanf("%c",&ch);
:"); scanf("%d%d",&a,&b);
switch(ch)
case '+':
printf("\nAddition of %d and %d is
%d",a,b,a+b); break;
case '-':
printf("\nSubtaraction of %d and %d is
%d",a,b,a-b); break;
case '*':
printf("\nMultiplication of %d and %d is
%d",a,b,a*b); break;
case '/':
printf("\nQuotient of %d and %d is
%d",a,b,a/b); break;
case '%':
printf("\nRemainder of %d and %d is
%d",a,b,a%b); break;
default:
operator"); break;
return 0;
Output:
Multiplication of 6 and 4 is 24
/*20B01A12A7
M.L.Rishika*/
#include<stdio.h>
int main()
int n,a,sum=0,r=0;
:"); scanf("%d",&n);
while(n!=0)
a=n%10;
sum=sum+
a; n=n/10;
r=r*10+a;
return 0;
Output:
/*11. A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1, Subsequent terms are found by adding the preceding two
terms in the sequence. Write a C program to generate the first n terms of the
sequence.*/
#include<stdio.h>
Int main()
int a=0,b=1,c,n,i=1;
:"); scanf("%d",&n);
while(i<=(n-2))
c=a+b
; a=b;
b=c;
i++;
printf("%d ",c);
return 0;
Output:
are 0 1 1 2 3 5 8 13
/*20B01A12A7
M.L.Rishika*/
functions
b.To find the GCD (greatest common divisor) of two given integers.*/
#include <stdio.h>
int main()
int a,res;
printf("Enter a number :
"); scanf("%d",&a);
res=fact(a);
return 0;
int fact(int n)
if(n==0)
return 1;
}
else
return n*fact(n-1);
Output :
Enter a number : 5
/*20B01A12A7
M.L.Rishika*/
#include <stdio.h>
int main()
int num,fact=1;
printf("Enter a number :
"); scanf("%d",&num);
for(;num!=0;num--)
fact=fact*num;
Output :
Enter a number : 4
/*20B01A12A7
M.L.Rishika*/
#include <stdio.h>
int main()
int a,b,c;
"); scanf("%d%d",&a,&b);
c=gcd(a,b);
return 0;
{
if(n1%n2==0)
return n2;
else
return gcd(n2,n1%n2);
Output :
gcd of 23 and 45 is 1
/*20B01A12A7
M.L.Rishika*/
#include <stdio.h>
int main()
int n1,n2,a;
"); scanf("%d%d",&n1,&n2);
while(n2!=0)
a=n2;
n2=n1%n
2; n1=a;
%d",n1); return 0;
Output:
Addition of Two
Matrices*/ #include
<stdio.h>
int addition()
int a[10][10],b[10][10],c[10][10],i,j,m,n;
"); scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j+
+)
scanf("%d",&a[i][j]);
printf("%d ",a[i][j])
printf("\nSecond matrix :
"); for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j+
+)
scanf("%d",&b[i][j]);
printf("%d ",b[i][j]);
printf("\nResult matrix
:"); for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j+
+)
c[i][j]=a[i][j]+b[i][j];
printf("%d ",c[i][j]);
int main()
addition();
Output:
First matrix:
1132
1 1
3 2
Second matrix
:2 1 3 2
21
32
Result matrix :
3 2
6 4
/*20B01A12A7
M.L.Rishika*/
a. 1 b. * c. 1 d. A
1 2 * * 2 3 BB
1 2 3 * * * 4 5 6 CCC
DDD
DEE
EEE
*/
/*20B01A12A7
M.L.Rishika*/
/*14.a.printing
pyramid 1
12
123
*/
#include <stdio.h>
int main()
int i,n,j;
printf("Enter a number :
"); scanf("%d",&n);
for(i=1;i<=n;i++)
printf("\n");
for(j=1;j<=i;j+
+)
printf("%d ",j);
return 0;
Output:
Enter a number :
31
12
123
/*20B01A12A7
M.L.Rishika*/
**
***
#include <stdio.h>
int main()
int i,j,n;
char
m='*';
"); scanf("%d",&n);
for(i=1;i<=n;i++)
printf("\n");
for(j=1;j<=i;j+
+)
printf("%c ",m);
return 0;
Output:
**
***
/*20B01A12A7
M.L.Rishika*/
/*14.c.printing pyramid 3
23
456
#include <stdio.h>
int main()
int i,j,n,m=1;
"); scanf("%d",&n);
for(i=1;i<=n;i++)
printf("\n");
for(j=1;j<=i;j+
+)
printf("%d
",m); m++;
return 0;
pyramid : 1
23
4 56
/*20B01A12A7
M.L.Rishika*/
/*14.d.printing pyramid 4 : A
B B
CC
DDD
DEE
EEE
int main()
int i,j,n;
"); scanf("%d",&n);
for(i=1;i<=n;i++)
printf("\n");
for(j=1;j<=i;j+
+)
printf("%c ",65+i-1);
return 0;
:5A
BB
CCC
D D D
DEEE
E E
/*20B01A12A7
M.L.Rishika*/
#include <stdio.h>
int swap();
int main()
int a,b;
printf("Enter two numbers :
"); scanf("%d%d",&a,&b);
swap(&a,&b);
{
int z;
z=*x
*x=*y;
*y=z;
printf("\nNumbers after swaping x=%d and y=%d",*x,*y);
Output:
Enter two numbers : 3 2
Numbers after swaping x=2 and y=3
/*20B01A12A7
M.L.Rishika*/
#include<stdio.h>
#include<stdlib.h>
int main()
int n,*p,i,sum=0;
"); scanf("%d",&n);
p=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
printf("Enter number
:"); scanf("%d",p+i);
sum=sum+*(p+i);
"); for(i=0;i<n;i++)
printf("%d ",*(p+i));
%d",sum); free(p);
return 0;
Output:
Enter number : 1
Enter number : 2
Enter number : 3
Enter number : 4
Enter number : 5
15