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

EXPERIMENT NO.

04

TITLE: SOLVING SIMPLE NUMERICAL PROBLEMS WITH AND


WITHOUT FUNCTIONS.
INTRODUCTION:
A computer program cannot handle all the tasks by itself. Instead, it request other program –like
entities called ‘FUNCTION’ in C to get its tasks done.

OBJECTIVE:
A function is a self-contained block statements that performs a coherent task of some kind. Every
C program can be thought of as a collection of these functions.

POST LAB TASKS:

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

Solution:

#include <stdio.h>
#include <stdlib.h>
int swap(int A,int B); /*declaration of a function*/

int main()
{int A,B;
printf("ENTER THE VALUE OF A and B");
scanf("%d%d",&A,&B);
swap(A,B);/*calling of a function*/
}
int swap(int A,int B)/*defining a function*/
{

A=A+B;
B=A-B;/*logic for swaping*/
A=A-B;
printf("A=%dB=%d",A,B);
return 0;
}
Output:

TASK 2:
Write a Program which input a number and pass it by function. The function takes square of the
number and return it.
Solution:
#include <stdio.h>
#include <stdlib.h>
int square(int num); /*declaration of a function*/

int main()
{int num,SQUARE;
printf("ENTER THE NUMBER");
scanf("%d",&num);
square(num); /*calling of a function*/

SQUARE=square(num);
printf("SQUARE=%d",SQUARE);
return 0;
}
int square(int num) /*defining a function*/

{
int s;
s=num*num;/*formula for squaring*/
return s;
}
Output:

TASK 3:
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 main.
Solution:
#include <stdio.h>
#include <stdlib.h>
int Sumdigit(int num); /*declaration of a function*/

int main()
{int num,sum1;
printf("ENTER THE FOUR DIGIT NUMBER");
scanf("%d",&num);
Sumdigit(num); /*calling of a function*/
sum1=Sumdigit(num);
printf("First and Last digit sum=%d\n",sum1);
return 0;
}
int Sumdigit(int num) /*defining a function*/
{
int Firstdigit,Lastdigit,sum2;
Firstdigit=num/1000; /*finding first and last digit*/
Lastdigit=num%10;
sum2=Firstdigit+Lastdigit;/*adding first and last digit*/
return sum2;
}
Output:
CONCLUSION:
From this lab we can learn how to add, subtract, multiply and divide using function. We can
swap numbers, find values from different formulas using function. So we can find the solution of
different problems using function.

You might also like