C programming Lab :-
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.
- 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.
- 3. Write a C program to display multiple variables.
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.
SOURCE CODE:
#include<stdio.h>
int main(){
printf("#####\n");
printf("#\n");
printf("#\n");
printf("####\n");
printf("#\n");
printf("#\n");
return 0;
}
OUTPUT:
#####
#
#
####
#
#
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.
SOURCE CODE:
#include<stdio.h>
int main(){
float length, width, area, perimeter;
printf("Enter the length and width of the rectangle : ");
scanf("%f%f",&length,&width);
area = length*width;
perimeter = 2*(length+width);
printf("Area of rectangle : %f square inches\n",area);
printf("Perimeter of rectangle : %f inches\n",perimeter);
return 0;
}
OUTPUT:
Enter the length and width of the rectangle : 7 5
Area of rectangle : 35.000000 square inches
Perimeter of rectangle : 24.000000 inches
3. Write a C program to display multiple variables.
SOURCE CODE:
#include<stdio.h>
int main(){
int i;
short int si;
long int li;
float f;
double d;
long double ld;
char c;
printf("Enter int, short int and long int values : ");
scanf("%d%hd%ld",&i,&si,&li);
printf("Enter float, double and long double values : ");
scanf("%f%lf%Lf",&f,&d,&ld);
printf("Enter a character : ");
scanf(" %c",&c);
printf("Given int value : %d\n",i);
printf("Given short int value : %hd\n",si);
printf("Given long int value : %ld\n",li);
printf("Given float value : %f\n",f);
printf("Given double value : %lf\n",d);
printf("Given long double value : %Lf\n",ld);
printf("Given character : %c\n",c);
return 0;
}
OUTPUT:
Enter int, short int and long int values : 12345 123 1234567
Enter float, double and long double values : 1.2 3.14 9.334
Enter a character : G
Given int value : 12345
Given short int value : 123
Given long int value : 1234567
Given float value : 1.200000
Given double value : 3.140000
Given long double value : 9.334000
Given character : G
EXERCISE-2
1. Write a C program to calculate the distance between the two points.
SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main(){
float x1,x2,y1,y2,dist;
printf("Enter x1 and x2 : ");
scanf("%f%f",&x1,&x2);
printf("Enter y1 and y2 : ");
scanf("%f%f",&y1,&y2);
dist = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
printf("Distance between two points : %f\n",dist);
return 0;
}
OUTPUT:
Enter x1 and x2 : 5 9
Enter y1 and y2 : 4 8
Distance between two points : 5.656854
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".
SOURCE CODE:
#include<stdio.h>
int main(){
int p,q,r,s;
printf("Enter p,q,r,s values:");
scanf("%d%d%d%d",&p,&q,&r,&s);
if(r>0 && s>0 && p%2==0 && q>r && s>p && r+s>p+q)
printf("Correct values");
else
printf("Wrong values");
return 0;
}
OUTPUT-1:
Enter p,q,r,s values:4 8 2 13
Correct values
OUTPUT-2:
Enter p,q,r,s values:9 3 1 45
Wrong values
EXERCISE-3
1. Write a C program to convert a string to a long integer.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
long int l;
char str[30];
printf("Enter a string:\n");
scanf("%s",str);
l=atol(str);
printf("String to long integer is : %ld",l);
return 0;
}
OUTPUT:
Enter a string:
123456789
String to long integer is : 123456789
2. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
float radius, length, width, base, height, area;
int choice;
do{
printf("1. Area of circle\n2. Area of rectangle\n3. Area of triangle\n4. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter radius : ");
scanf("%f",&radius);
area = 3.14 * radius * radius;
printf("Area of circle : %f\n",area);
break;
case 2:
printf("Enter length and width : ");
scanf("%f%f",&length,&width);
area = length * width;
printf("Area of rectangle : %f\n",area);
break;
case 3:
printf("Enter base and height : ");
scanf("%f%f",&base,&height);
area = (base*height)/2;
printf("Area of triangle : %f\n",area);
break;
case 4:
exit(0);
}
} while(1);
return 0;
}
OUTPUT:
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 1
Enter radius : 4
Area of circle : 50.240002
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 2
Enter length and width : 4 8
Area of rectangle : 32.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 3
Enter base and height : 2 3
Area of triangle : 3.000000
1. Area of circle
2. Area of rectangle
3. Area of triangle
4. Exit
Enter your choice : 4
3. Write a C program to calculate the factorial of a given number.
SOURCE CODE:
#include<stdio.h>
int main(){
int num,fact=1,i;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
fact = fact*i;
printf("Factorial of %d is %d",num,fact);
return 0;
}
OUTPUT:
Enter a number: 5
Factorial of 5 is 120
EXERCISE-4
1. Write a program in C to display the n terms of even natural number and their sum.
SOURCE CODE:
#include<stdio.h>
int main(){
int n,i=1,c=1,sum=0;
printf("Enter number of terms : ");
scanf("%d",&n);
printf("Even natural numbers are : \n");
while(c<=n){
if(i%2==0){
printf("%d ",i);
sum=sum+i;
c++;
}
i++;
}
printf("\nSum of even natural numbers is: %d\n",sum);
return 0;
}
OUTPUT:
Enter number of terms : 5
Even natural numbers are :
2 4 6 8 10
Sum of even natural numbers is: 30
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.
SOURCE CODE:
#include<stdio.h>
int main(){
int n,i;
float sum=0;
printf("Enter range:");
scanf("%d",&n);
printf("Series is:\n");
for(i=1;i<=n;i++){
printf("1/%d",i);
if(i<n)
printf("+");
sum = sum + (float)1/i;
}
printf("\nSum of series is: %f",sum);
return 0;
}
OUTPUT:
Enter range:5
Series is:
1/1+1/2+1/3+1/4+1/5
Sum of series is: 2.283334
3. Write a C program to check whether a given number is an Armstrong number or not.
SOURCE CODE:
/*Program to check whether the given number is Armstrong number or not.*/
#include<stdio.h>
#include<math.h>
int main(){
int n,temp,arm=0,c=0;
printf("Enter number:");
scanf("%d",&n);
temp=n;
while(temp>0){
temp=temp/10;
c++;
}
temp=n;
while(temp>0){
arm=arm+pow((temp%10),c);
temp=temp/10;
}
if(n==arm)
printf("Given number is Armstrong.");
else
printf("Given number is not Armstrong.");
return 0;
}
OUTPUT-1:
Enter number:371
Given number is Armstrong.
OUTPUT-2:
Enter number:151
Given number is not Armstrong.
EXERCISE-5
1. Write a program in C to print all unique elements in an array.
SOURCE CODE:
#include<stdio.h>
int main(){
int a[20],n,i,j,k;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
for(k=j;k<n-1;k++)
a[k]=a[k+1];
n--;
j--;
}
}
}
printf("\nUnique elements in array are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter array elements:
1 2 3 2 1
Unique elements in array are:
1 2 3
2. Write a program in C to separate odd and even integers in separate arrays.
SOURCE CODE:
#include<stdio.h>
int main(){
int arr[30],evenArr[30],oddArr[30],i,j=0,k=0,ea=0,oa=0,n;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++){
if(arr[i]%2==0)
evenArr[j++]=arr[i];
else
oddArr[k++]=arr[i];
}
printf("Even elements in the array:\n");
for(i=0;i<j;i++)
printf("%d\t",evenArr[i]);
printf("\nOdd elements in the array:\n");
for(i=0;i<k;i++)
printf("%d\t",oddArr[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter 5 elements:
10 25 30 35 55
Even elements in the array:
10 30
Odd elements in the array:
25 35 55
3. Write a program in C to sort elements of array in ascending order.
SOURCE CODE:
#include<stdio.h>
int main(){
int arr[20],n,i,j,temp;
printf("Enter array size:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Before sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nAfter sorting array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
return 0;
}
OUTPUT:
Enter array size:5
Enter 5 elements:
3 4 2 5 1
Before sorting array elements are:
3 4 2 5 1
After sorting array elements are:
1 2 3 4 5
EXERCISE-6
1. Write a program in C for multiplication of two square Matrices.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx1[20][20],mtrx2[20][20],mulmtrx[40][40],n1,n2,i,j,k;
printf("Enter matrix sizes:");
scanf("%d%d",&n1,&n2);
if(n1==n2){
printf("Enter first matrix elements\n");
for(i=0;i<n1;i++)
for(j=0;j<n1;j++)
scanf("%d",&mtrx1[i][j]);
printf("Enter second matrix elements\n");
for(i=0;i<n2;i++)
for(j=0;j<n2;j++)
scanf("%d",&mtrx2[i][j]);
for(i=0;i<n1;i++){
for(j=0;j<n2;j++){
mulmtrx[i][j]=0;
for(k=0;k<n1;k++)
mulmtrx[i][j]=mulmtrx[i][j]+mtrx1[i][k]*mtrx2[k][j];
}
}
printf("\nResult matrix is\n");
for(i=0;i<n1;i++){
for(j=0;j<n1;j++)
printf("%d\t",mulmtrx[i][j]);
printf("\n");
}
}
else
printf("Matrix multiplication is not possible");
return 0;
}
OUTPUT-1:
Enter matrix sizes:3 3
Enter first matrix elements
1 2 3
4 5 6
7 8 9
Enter second matrix elements
9 8 7
6 5 4
3 2 1
Result matrix is
30 24 18
84 69 54
138 114 90
OUTPUT-2:
Enter matrix sizes:3 2
Matrix multiplication is not possible
2. Write a program in C to find transpose of a given matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10],r,c,i,j,temp;
printf("Enter matrix size:\n");
scanf("%d %d",&r,&c);
printf("Enter matrix elements:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
if(r>=c){
for(i=0;i<c;i++){
for(j=0;j<r;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
else{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
printf("Transpose of the given matrix is\n");
for(i=0;i<c;i++){
for(j=0;j<r;j++)
printf("%d ",mtrx[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-1:
Enter matrix size:
2 3
Enter matrix elements:
1 2 3
4 5 6
Transpose of the given matrix is
1 4
2 5
3 6
OUTPUT-2:
Enter matrix size:
3 2
Enter matrix elements:
1 2
3 4
5 6
Transpose of the given matrix is
1 3 5
2 4 6
Lab 9.1
Aim:
Write a c program to store information using structures with dynamic memory
allocation :
The following program uses Student structure to allocate
dynamically memory to store sz number of students and to print all the student
details
#include
<stdio.h>
struct
Student
{
int sno;
char sname[20];
};
int
main()
{
struct Student *s;
int i,sz;
printf("Enter No of students to store
?");
scanf("%d",&sz);
/* memory allocation for sz number of
students */
s=(struct Student*)malloc(sz*sizeof(struct
Student));
/* reading students data */
for(i=0;i<sz;i++)
{
printf("\nEnter student%d number
and name?",i+1);
scanf("%d%s",&(*(s+i)).sno,&(*(s+i)).sname);
}
puts("\nOutput");
for(i=0;i<sz;i++)
{
printf("\n%d %s",(*(s+i)).sno,(*(s+i)).sname);
}
return 0;
}
Output :
Enter No of students to store ?2
Enter student1 number and name?101 babe
Enter student2 number and name?102 Harish
Output
101 babe
102 Harish
...Program finished with exit code 0
Press ENTER to exit console.
Lab 9.2
Aim:
Write a c program to demonstrate how to handle the pointers:
The
following program uses sub function sum(int *,int ) to carry the reference, size and returns
The sum
of digits
#include
<stdio.h>
/*
function to return sum of sz numbers from the starting address */
int
sum(int*n,int sz)
{
int i,nsum=0;
for(i=0;i<sz;i++)
{
nsum+=*(n+i);
}
return
nsum;
}
int
main()
{
int no[100];
int i,sz,ns,numsum;
printf("\nEnter How many Number to
Enter?");
scanf("%d",&sz);
printf("\nEnter %d numbers?",sz);
for(i=0;i<sz;i++)
scanf("%d",&no[i]);
printf("\nHow many first numbers to
some?");
scanf("%d",&ns);
numsum=sum(no,ns);
printf("\n number sum =
%d",numsum);
return 0;
}
Enter How many Number to Enter?5
Enter 5 numbers?12 23 34 45 11
How many first numbers to some?3
number sum = 69
Lab 10.1
Aim:
Write a program to demonstrate the use of & (address of) , * (value at
address)
Operator
In the following
program iptr is initialized with first element address. And accessed the values
using * operator.
#include
<stdio.h>
int
main()
{
int *iptr,no[10];
int i,sz;
printf("\nEnter How many Number to
Enter?");
scanf("%d",&sz);
printf("\nEnter %d numbers?",sz);
for(i=0;i<sz;i++)
scanf("%d",&no[i]);
/* initializing first element address to
iptr */
iptr=&no[0]; /*
or iptr=no; */
puts("Elements in reverse..");
for(i=sz-1;i>=0;i--)
printf("%5d",*(iptr+i));
return 0;
}
Output ;
Enter How many Number to Enter?5
Enter 5 numbers?12 23 34 45 56
Elements in reverse..
56 45 34 23 12
Lab 10.2
Aim:
Write a program in c two add two numbers using pointers
#include
<stdio.h>
int
main()
{
int
first, second, *p, *q, sum;
printf("Enter
two integers to add\n");
scanf("%d%d",
&first, &second);
p =
&first; q = &second;
sum =
*p + *q;
printf("Sum
of the numbers = %d\n", sum);
return
0;
}
Output:
Enter two integers to add
34 45
Sum of the numbers = 79
Lab 11.1
Aim:
Write a program in c to add numbers using call by reference
The
following program uses add function( add(int *,int ) ) to carry the reference, size and returns
sum of numbers. The add function
called by passing reference or address.
#include
<stdio.h>
/*
function to return sum of sz numbers from the starting address */
int add(int*n,int
sz)
{
int i,nsum=0;
for(i=0;i<sz;i++)
{
nsum+=*(n+i);
}
return
nsum;
}
int
main()
{
int no[100];
int i,sz,ns,numsum;
printf("\nEnter How many Number to
Enter?");
scanf("%d",&sz);
printf("\nEnter %d numbers?",sz);
for(i=0;i<sz;i++)
scanf("%d",&no[i]);
numsum=sum(&no[0],sz); /* sum called by passing reference */
printf("\n number sum =
%d",numsum);
return 0;
}
Output :-
Enter How many Number to Enter?5
Enter 5 numbers?12 2 4 5 11
number sum = 34
Lab 11.2
Aim:
Write a program in c to find the largest element using dynamic memory
allocation.
#include
<stdio.h>
#
include <stdlib.h>
int
main()
{
int *no;
int i,sz,large,index=0;
printf("\nEnter How many Number to
Enter?");
scanf("%d",&sz);
/* dynamic memory allocation capable to store
sz integers*/
no=(int*)malloc(sizeof(int) * sz);
printf("\nEnter %d numbers?",sz);
for(i=0;i<sz;i++)
scanf("%d",&no[i]);
/* finding largest element */
large=no[0];
for(i=1;i<sz;i++)
{
if(large<no[i]){
large=no[i];
index=i;}
}
printf("\n largest element is %d ,
value = %d",index+1,large);
return
0;
}
Output :-
Enter How many Number to Enter?5
Enter 5 numbers?12 23 44 3 11
largest element is 2 , value = 44