Nothing Special   »   [go: up one dir, main page]

CTC Lab Record by 20B01A12A7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

/*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;

printf("Enter distance in inches :

"); scanf("%f",&di);

dy=di/36,df=di/12;

printf("\nDistance in yards is

%f",dy); printf("\nDistance in feet

is %f",df);

return 0;

Output :

Enter distance in inches :

8 Distance in yards is

0.222222 Distance in feet

is 0.666667
/*20B01A12A7

M.L.Rishika*/

/*2. write a program to convert the temperature from degree

centigrade to fahrenheit and vice versa*/

#include <stdio.h>

int main()

float C1,F1,C2,F2;

printf("Enter temperature in degree centigrade:

"); scanf("%f",&C1);

F1=(C1*9/5)+32;

printf("\nFahrenheit temperature is

%f",F1); printf("\nEnter temperature in

fahrenheit : "); scanf("%f",&F2);

C2=(F2-32)*5/9;

printf("\nTemperature in celsius is %f",C2);

return 0;

Output:

Enter temperature in degree centigrade :

36 Fahrenheit temperature is 96.800003

Enter temperature in Fahrenheit :

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;

printf("Enter three numbers :

");

scanf("%d%d%d",&a,&b,&c);

if(a>b)

if(a>c)

printf("\n%d is largest among %d,%d and %d",a,a,b,c);

else

printf("\n%d is largest among %d,%d and %d",c,a,b,c);

else

if(b>c)

{
printf("\n%d is largest among %d,%d and %d",b,a,b,c);

else

printf("\n%d is largest among %d,%d and %d",c,a,b,c);

return 0;

Output :

Enter three numbers : 7 6 8

8 is largest among 7,6 and 8


/*20B01A12A7

M.L.Rishika*/

/*4. write a simple program based on type

conversions (from int to float & float to int)*/

#include <stdio.h>

int main()

int a,b;

float

c,d;

printf("Enter two numbers :

"); scanf("%d%f",&a,&c);

b=(int)c;

d=(float)a

printf("\nfloat value of %d is %f",a,d);

printf("\ninteger value of %f is

%d",c,b);

return 0;

Output:

Enter two numbers : 3 2.6

float value of 3 is 3.000000

integer value of 2.600000 is 2


/*20B01A12A7
M.L.Rishika*/

/*5. write a program that displays all the numbers from x to y, that are

divisible by a and b.(x,y,a and b should be read from the keyboard)*/

#include <stdio.h>

int main()

int x,y,a,b;

printf("Enter four values : ");

scanf("%d%d%d%d",&x,&y,&a,

&b);

printf("\nThe numbers from %d to %d that are divisible by %d and %d are

",x,y,a,b); while(x<=y)

if(x%a==0 && x%b==0)

printf("%d ",x);

x++

return 0;

Output :

Enter four values : 1 10 2 4

The numbers from 1 to 10 that are divisible by 2 and 4 are 4 8


/*20B01A12A7

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.

Display the average as a floating-point number. (For example, if you entered

1, 2, and 0, the average should be 1.5.)*/

#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);

printf("Number of positive numbers are

%d",cnt1); printf("\nNumber of negative numbers

are %d",cnt2); printf("\nTotal is %d",sum);

printf("\nAverage value is %f",avg);

return 0;

Output:

Enter a number :

3 Enter a number

: 1 Enter a

number: 0

Number of positive numbers are 2

Number of negative numbers are 0

Total is 4

Average value is 2.000000


/*20B01A12A7

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;

printf("Enter five subject marks :

");

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");

else if(p>=80 && p<90)

printf("\nGrade is E");

else if(p>=70 && p<80)

printf("\nGrade is A");

else if(p>=60 && p<70)


{

printf("\nGrade is B");

else if("p>=50 && p<60")

printf("\nGrade is C");

else

printf("\nFail");

return 0;

Output:

Enter five subject marks : 99 98 99 100

99 Grade is O
/*20B01A12A7

M.L.Rishika*/

/*8.The total distance travelled by vehicle in ‘t’ seconds is given by distance s =

ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2).

Write a C program to find the distance travelled at regular intervals of time

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;

printf("Enter number of intervals :

"); scanf("%d",&n);

while(i<=n)

printf("\nEnter time in sec :

"); scanf("%d",&t);

printf("\nEnter initial velocity in m/sec

:"); scanf("%f",&u);

printf("\nEnter acceleration in m/(sec*sec)

:"); scanf("%f",&a);

s=s+(u*t+((a*t*t)/2))

; i++;

}
printf("\nTotal distance travelled is %f m",s);

return 0;

Output:

Enter number of intervals :

2 Enter time in sec : 10

Enter initial velocity in m/sec : 50

Enter acceleration in m/(sec*sec) :

3.5 Enter time in sec : 20

Enter initial velocity in m/sec : 60

Enter acceleration in m/(sec*sec) :

4.3

Total distance travelled is 2735.000000 m


/*20B01A12A7

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.

(Consider the operators +,-,*,/,% and use switch statement.)*/

#include <stdio.h>

int main()

int a,b;

char ch;

printf("Enter any operator :

"); scanf("%c",&ch);

printf("Enter two numbers

:"); 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:

printf("\nIt is not a required

operator"); break;

return 0;

Output:

Enter any operator : *

Enter two numbers : 6 4

Multiplication of 6 and 4 is 24
/*20B01A12A7

M.L.Rishika*/

/*10.Write a C program to find the sum of individual digits of a positive

integer and find the reverse of the given number.*/

#include<stdio.h>

int main()

int n,a,sum=0,r=0;

printf("Enter a positive number

:"); scanf("%d",&n);

while(n!=0)

a=n%10;

sum=sum+

a; n=n/10;

r=r*10+a;

printf("sum of individual digits of a number :

%d",sum); printf("\nReverse of a number : %d",r);

return 0;

Output:

Enter a positive number : 6754

sum of individual digits of a number : 22

Reverse of a number : 4576


/*20B01A12A7
M.L.Rishika*/

/*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;

printf("Enter number of terms

:"); scanf("%d",&n);

printf("\nThe fibonacci sequence upto %d terms are

",n); printf("\n%d %d ",a,b);

while(i<=(n-2))

c=a+b

; a=b;

b=c;

i++;

printf("%d ",c);

return 0;

Output:

Enter number of terms : 8

The Fibonacci sequence upto 8 terms

are 0 1 1 2 3 5 8 13
/*20B01A12A7

M.L.Rishika*/

/*12.Write C programs that use both recursive and non-recursive

functions

a.To find th factorial of a given integer.

b.To find the GCD (greatest common divisor) of two given integers.*/

/*12.a.i. Factorial of a given integer using recursion*/

#include <stdio.h>

int fact(int n);

int main()

int a,res;

printf("Enter a number :

"); scanf("%d",&a);

res=fact(a);

printf("\nThe factorial of %d is %d",a,res);

return 0;

int fact(int n)

if(n==0)

return 1;
}

else

return n*fact(n-1);

Output :

Enter a number : 5

The factorial of 5 : 120

/*20B01A12A7

M.L.Rishika*/

/*12.a.ii.Factorial of a given integer without recursion*/

#include <stdio.h>

int main()

int num,fact=1;

printf("Enter a number :

"); scanf("%d",&num);

for(;num!=0;num--)

fact=fact*num;

printf("\nThe factorial of a given integer is %d",fact);


return 0;

Output :

Enter a number : 4

The factorial of a given number is 24

/*20B01A12A7

M.L.Rishika*/

/*12.b.i.GCD of two given integers using recursion*/

#include <stdio.h>

int gcd(int n1,int n2);

int main()

int a,b,c;

printf("Enter two numbers :

"); scanf("%d%d",&a,&b);

c=gcd(a,b);

printf("\ngcd of %d and %d is %d",a,b,c);

return 0;

int gcd(int n1,int n2)

{
if(n1%n2==0)

return n2;

else

return gcd(n2,n1%n2);

Output :

Enter two numbers : 23 45

gcd of 23 and 45 is 1

/*20B01A12A7

M.L.Rishika*/

/*12.b.ii.GCD of two numbers without recursion*/

#include <stdio.h>
int main()

int n1,n2,a;

printf("Enter two numbers :

"); scanf("%d%d",&n1,&n2);

while(n2!=0)

a=n2;
n2=n1%n

2; n1=a;

printf("\nGCD of two given numbers is

%d",n1); return 0;

Output:

Enter two numbers : 16 4

GCD of two given numbers is 4


/*20B01A12A7
M.L.Rishika*/

/*13.Write a C program that uses functions to perform the following:

Addition of Two

Matrices*/ #include

<stdio.h>

int addition()

int a[10][10],b[10][10],c[10][10],i,j,m,n;

printf("Enter two numbers :

"); scanf("%d%d",&m,&n);

printf("\nFirst matrix : ");

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:

Enter two numbers : 2 2

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*/

/*14.write a c program to construct the following pyramid of numbers

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*/

/*14. b.printing pyramid 2

**

***

#include <stdio.h>

int main()

int i,j,n;

char

m='*';

printf("Enter a number of rows of a pyramid :

"); scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n");

for(j=1;j<=i;j+

+)

printf("%c ",m);

return 0;

Output:

Enter a number of rows of a pyramid : 3

**

***
/*20B01A12A7

M.L.Rishika*/

/*14.c.printing pyramid 3

23

456

#include <stdio.h>

int main()

int i,j,n,m=1;

printf("Enter number of rows of a pyramid :

"); scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n");

for(j=1;j<=i;j+

+)

printf("%d

",m); m++;

return 0;

Output: Enter number of rows of a

pyramid : 1

23

4 56
/*20B01A12A7

M.L.Rishika*/

/*14.d.printing pyramid 4 : A

B B

CC

DDD

DEE

EEE

i*/ #include <stdio.h>

int main()

int i,j,n;

printf("Enter number of rows of pyramid :

"); scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n");

for(j=1;j<=i;j+

+)

printf("%c ",65+i-1);

return 0;

Output : Enter number of rows of a pyramid

:5A
BB
CCC

D D D
DEEE
E E

/*20B01A12A7

M.L.Rishika*/

/*15.Write a c program to swap two numbers using call by reference method*/

#include <stdio.h>
int swap();

int main()

int a,b;
printf("Enter two numbers :

"); scanf("%d%d",&a,&b);

swap(&a,&b);

int swap(int *x,int *y)

{
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*/

/*16.write a c program to create an array with

calloc(),store the values in it and find their sum*/

#include<stdio.h>
#include<stdlib.h>

int main()

int n,*p,i,sum=0;

printf("Enter size of array :

"); 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);

printf("\nArray of given elements :

"); for(i=0;i<n;i++)

printf("%d ",*(p+i));

printf("\nThe sum of numbers in array is

%d",sum); free(p);
return 0;

Output:

Enter size of array : 5

Enter number : 1

Enter number : 2

Enter number : 3

Enter number : 4

Enter number : 5

Array of given elements : 1 2 3 4

5 The sum of numbers in array is

15

You might also like