Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

LAB PROGRAMS

The document contains a series of C programming exercises that include tasks such as printing shapes, calculating areas and perimeters, displaying variables, computing distances, and checking for Armstrong numbers. Each exercise is accompanied by sample programs and their outputs. The exercises aim to reinforce programming concepts and problem-solving skills in C.

Uploaded by

cplabconnector
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

LAB PROGRAMS

The document contains a series of C programming exercises that include tasks such as printing shapes, calculating areas and perimeters, displaying variables, computing distances, and checking for Armstrong numbers. Each exercise is accompanied by sample programs and their outputs. The exercises aim to reinforce programming concepts and problem-solving skills in C.

Uploaded by

cplabconnector
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EXERCISE 1

1. Write a C program to print a block F using hash (#), where the F has a height of six
characters and width of five and four characters.

PROGRAM :

#include<stdio.h>

int main( )

printf("######\n");

printf("#\n");

printf("#\n");

printf("#####\n");

printf("#\n");

printf("#\n");

printf("#");

return 0;

OUPTUT :
2 )Write a C program to compute the perimeter and area of a rectangle with a height of
7 inches and width of 5 inches.

PROGRAM:

#include <stdio.h>

int main( )

int a,p;

a=7*5;

p=2*(7+5);

printf("Area of the rectangle is %d\n",a);

printf("Perimeter of a rectangle is %d\n",p);

return 0;

OUTPUT:

Area of the rectangle is 35

Perimeter of a rectangle is 24
3) Write a C program to display multiple variables.

PROGRAM :

#include<stdio.h>

int main( )

char c='V';

int i=678;

float f=12.23;

double d=12.123456789;

printf("Character = %c\n",c);

printf("Integer = %d\n",i);

printf("Float = %f\n",f);

printf("Double = %lf\n",d);

return 0;

Output:

Character = V

Integer = 678

Float = 12.230000

Double = 12.123457
EXERCISE 2

1) Write a C program to calculate the distance between the two points.

PROGRAM:

#include<stdio.h>
#include<math.h>
int main( )
{
int x1,x2,y1,y2;
int x,y;
float d;
printf("Enter the coordinates of first point(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates of the second point(x2,y2):");
scanf("%d%d",&x2,&y2);
x = (x2-x1);
y = (y2-y1);
d = sqrt((x*x)+(y*y));
printf("Distance = %.2f\n",d);
return 0;
}

Output:

Enter the coordinates of first point(x1,y1):3 7


Enter the coordinates of the second point(x2,y2):5 8

Distance = 2.24
2) Write a C program that accepts 4 integers p, q, r, s from the user where r and s are
positive and p is even. If q is greater than r and s is greater than p and if the sum of r
and s is greater than the sum of p and q print "Correct values", otherwise print
"Wrong values".

PROGRAM:

#include <stdio.h>

int main()

int p, q, r, s;

printf("\nInput the first integer: ");

scanf("%d", &p);

printf("\nInput the second integer: ");

scanf("%d", &q);

printf("\nInput the third integer: ");

scanf("%d", &r);

printf("\nInput the fourth integer: ");

scanf("%d", &s);

if((q > r) && (s > p) && ((r+s) > (p+q)) && (r > 0) && (s > 0) && (p%2 == 0))
{
printf("\nCorrect values\n");
}
else
{
printf("\nWrong values\n");
}
return 0;
}

OUTPUT :

i) Input the first integer: 2

Input the second integer: 4

Input the third integer: 3


Input the fourth integer: 5

Correct values

ii)

Input the first integer: 9

Input the second integer: 4

Input the third integer: 5

Input the fourth integer: 2

Wrong values
EXERCISE 3

1) Write a C program to convert a string to a long integer

PROGRAM:

int main()

char str[20];

printf("Enter a String for Integer conversion \n");

gets(str);

printf("Integer: %ld \n", atol(str));

return 0;

OUTPUT :

i) Enter a String for Integer conversion

123abcd

Integer: 123

ii) Enter a String for Integer conversion

abcdef

Integer: 0
2) Write a program in C which is a Menu-Driven Program to compute the area of the
various geometrical shape

PROGRAM :

#include <stdio.h>

void main ()

int choice,r,l,w,b,h;

float area;

printf("Input 1 for area of circle\n");

printf("Input 2 for area of rectangle\n");

printf("Input 3 for area of triangle\n");

printf("Input your choice : ");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Input radius of the circle : ");

scanf("%d",&r);

area=3.14*r*r;

break;

case 2:

printf("Input length and width of the rectangle : ");

scanf("%d%d",&l,&w);

area=l*w;

break;

case 3:

printf("Input the base and hight of the triangle :");

scanf("%d%d",&b,&h);
area=0.5*b*h;

break;

printf("The area is : %f\n",area);

OUTPUT :

i)Input 1 for area of circle

Input 2 for area of rectangle

Input 3 for area of triangle

Input your choice : 1

Input radius of the circle : 6

The area is : 113.040001

ii) Input 1 for area of circle

Input 2 for area of rectangle

Input 3 for area of triangle

Input your choice : 3

Input the base and hight of the triangle :6

The area is : 21.000000


3) Write a C program to calculate the factorial of a given number

PROGRAM:

#include <stdio.h>

int main()

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else

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

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

OUTPUT:

i) Enter an integer: -5

Error! Factorial of a negative number doesn't exist.

ii) Enter an integer: 15

Factorial of 15 = 1307674368000
Exercise 4:

1) Write a program in C to display the n terms of even natural number and their sum.

PROGRAM:

#include<stdio.h>

main()

int n,i,sum;

printf("\nEnter how many even numbers you want:");

scanf("%d",&n);

sum=0;

i=1;

printf("%d even natural numbers are:",n);

do

printf("%d ",2*i);

sum=sum+(2*i);

i=i+1;

}while(i<=n);

printf("\nSum of these numbers is %d",sum);

OUTPUT:

i) Enter how many even numbers you want:12

12 even natural numbers are:2 4 6 8 10 12 14 16 18 20 22 24

Sum of these numbers is 156

ii) Enter how many even numbers you want:15

15 even natural numbers are:2 4 6 8 10 12 14 16 18 20 22 24 26 28 30

Sum of these numbers is 240


2) Write a program in C to display the n terms of harmonic series and their
sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.

PROGRAM :
#include<stdio.h>
main()
{
int n,i;
float sum=0.0;

printf("\nEnter n value:");
scanf("%d",&n);
printf("\nThe given harmonic series : ");
for(i=1;i<=n;i++)
{
printf("1/%d +",i);
sum=sum+(1.0/i);
}
printf("\b");
printf("= %f",sum);
}
OUTPUT:

i) Enter n value:5

The given harmonic series : 1/1 +1/2 +1/3 +1/4 +1/5 = 2.283334

ii) Enter n value:12

The given harmonic series : 1/1 +1/2 +1/3 +1/4 +1/5 +1/6 +1/7 +1/8 +1/9 +1/10 +1/11 +1/12
= 3.103211
3) Write a C program to check whether a given number is an Armstrong number or not.

PROGRAM:

#include<stdio.h>

main()

int n,temp,rem,sum=0;

printf("\nEnter number:");

scanf("%d",&n);

temp=n;

while(n!=0)

rem=n%10;

sum=sum+(rem*rem*rem);

n=n/10;

if(temp==sum)

printf("\nArmstrong Number");

else

printf("\nNot Armstrong Number");

OUTPUT :
i) Enter number:153

Armstrong Number

ii) Enter number:513

Not Armstrong Number


EXERCISE 5

1) Write a program in C to print all unique elements in an array.

PROGRAM :

#include <stdio.h>

int main()

int a[10], n,ctr=0;

int i, j;

printf("Enter the number of elements to be stored in the array: ");

scanf("%d",&n);

printf("\nEnter the elements in the array :\n");

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

scanf("%d",&a[i]);

printf("\nThe unique elements found in the array are: \n");

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

ctr=0;

for(j=0; j<n; j++)

/*Increment the counter when the search value is duplicate.*/

if (i!=j)

if(a[i]==a[j])

ctr++;
}

if(ctr==0)

printf("%d ",a[i]);

OUTPUT:

i) Enter the number of elements to be stored in the array: 5

Enter the elements in the array :

The unique elements found in the array are:

5 6 3

ii) Enter the number of elements to be stored in the array: 12

Enter the elements in the array :

2
1

12

89

89

The unique elements found in the array are:

2 1 3 12

You might also like