Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

LAB#03

PROGRAMMING IN C

1. OBJECTIVE:
1.1. The objective of C language is simple, designed to enable sophisticated objective-oriented
programming. Easy to analyze program. Help to understand the logic of programming.

1.2. The objective of C language is simple, designed to enable sophisticated objective-oriented


programming. Easy to analyze program. Help to understand the logic of programming.

2. INTRODUCTION:
C is a procedural programming language. It was mainly developed as a system programming
language to write an operating system. The main features of C language include low-level access to
memory, a simple set of keywords, and clean style, these features make C language suitable for
system programming’s like an operating system.

TASK#01:
Input the length and width of the rectangle and radius of the circle. Write two functions to calculate area of
rectangle and area of circle.

 Code:
#include<stdio.h>
int area_of_circle(int radius);
int area(int length,int breadth);
int main()
{
int radius=5,circle_area;
int len=8,bre=4,rectangle_area;
circle_area=area_of_circle(radius);
rectangle_area=area(len,bre);
printf("Area of circle is:%d\n",circle_area);
printf("Area of rectangle is:%d\n",rectangle_area);
return 0;
}
int area_of_circle(int radius)
{
int z;
z=3.14*radius*radius;
return z;
}
Programming Fundamental Page 1
int area(int length,int breadth)
{
int b;
b=length*breadth;
return b;
}
 Output:

TASK#02:
A five digit positive integer is entered through the keyboard, write a function to calculate sum of digits of
the 5 digit number.

 Code:
#include<stdio.h>
int digit(int x);
int main()
{
int a;
printf("Enter 5 digit Number:");
scanf("%d",&a);
digit (a);
}
int digit(int x)
{
int b,c,d,e,f,sum;
b=x%10;
printf("\n 1st digit is:%d",b);
c=(x/10)%10;
printf("\n 2nd digit is:%d",c);
d=(x/100)%10;
printf("\n 3rd digit is:%d",d);
e=(x/1000)%10;
printf("\n 4th digit is:%d",e);
f=(x/10000)%10;
printf("\n 5th digit is:%d",f);
sum=b+c+d+e+f;
Programming Fundamental Page 2
printf("\n Sum of digit is:%d",sum);
return sum;
}
 Output:

TASK#03:
Write a C program to add ,subtract, multiply and divide two numbersusing functions.

 Code:
#include<stdio.h>
int sum(int x,int y);
int sub(int x,int y);
int mul(int x,int y);
int div(int x,int y);
int main()
{
int x=6,y=4;
int add,subtract,multiply,divide;
add=sum(x,y);
subtract=sub(x,y);
multiply=mul(x,y);
divide=div(x,y);
printf("Sum:%d\n",add);
printf("Difference:%d\n",subtract);
printf("Multiplication:%d\n",multiply);
printf("Division:%d\n",divide);
return 0;
}
int sum(int x,int y)
{
int P;
P=x=y;
return P;

Programming Fundamental Page 3


}
int sub(int x,int y)
{
int Q;
Q=x-y;
return Q;
}
int mul(int x,int y)
{
int R;
R=x*y;
return R;
}
int div(int x,int y)
{
int S;
S=x/y;
return S;
}
 Output:

TASK#01:
Write a program which input a number and pass it by function. The function take square of the number and
return it.

 Code:
#include<stdio.h>
int square(int x);
int main()
{
int S, squ;
printf("Enter the number:");
scanf("%d",&S);
squ=square(S);
printf("The Square of number is:%d\n",squ);
Programming Fundamental Page 4
return 0;
}
int square(int x)
{
int c;
c=x*x;
return c;
}
 Output:

TASK#02:
Write a program which input a 4 digit number and send it to the function. Function calculate the sum of 1st
and last digit of this number and return it to the main.

 Code:
#include<stdio.h>
int sum(int x,int y);
int main()
{
int a,b,c,d;
printf("ENTER A NUMBER:");
scanf("%d",&a);
b=a%10;
printf("THE LAST DIGIT IS:%d\n",b);
c=(a/1000)%10;
printf("THE FIRST DIGIT IS:%d\n",c);
d=sum(b,c);
printf("THE SUM OF FIRST AND LAST DIGIT IS:%d\n",d);
return 0;
}
int sum(int x,int y);
int t;
t=x+y;
return ;
}

Programming Fundamental Page 5


 Output:

TASK#03:
Write a program which two values in main and send it to the function. Function swaps these two values
without using third variable and print in the function.

 Code:
#include<stdio.h>
int swap(int x,int y);
int main()
{
int var1,var2;
printf("Enter two integers:\n");
scanf("%d%d",&var1,&var2);
swap(var1,var2);
}
int swap(int x, int y)
{
x=x+y;
y=x-y;
x=x-y;
printf("After swapping\nfirst variabe=%d\nsecond variable=%d\n",x,y);
}
 Output:

3.CONCLUSION:
After analyzing these program, we came to know that how to make function for different purpose. It
help us to solve different question by simple call a function in program.

Programming Fundamental Page 6

You might also like