Practical List - 2
1. Write a program to add two given n integer
number using function.
Program
#include <stdio.h>
#include <conio.h>
void add(int, int);
void main()
{
clrscr();
int num1, num2;
printf("Enter the 1st number =
");
scanf("%d", &num1);
printf("Enter the 2nd number =
");
scanf("%d", &num2);
add(num1, num2);
getch();
}
void add(int num1, int num2)
{
printf("\nThe sum = %d",
num1 + num2);
}
Output:
Enter the 1st number = 4
Enter the 2nd number = 5
The sum = 9
2. Write a program to calculate area of circle
using function.
Program:
#include <stdio.h>
#include <conio.h>
#define PI 3.14
float areaOfCircle(float);
void main()
{
clrscr();
float radius;
printf("Enter the radius =
");
scanf("%f", &radius);
float area = areaOfCircle(radius);
printf("\nThe area of circle =
%f", area);
getch();
}
float areaOfCircle(float radius)
{
return PI * radius * radius;
}
Output:
Enter the radius = 14
The area of circle = 615.440002
Please follow me for more practicals....
3. Write a program to find out maximum number
out of 3 number using function.
Program:
#include <stdio.h>
#include <conio.h>
int findMax(int, int, int);
void main()
{
clrscr();
int num1, num2, num3;
printf("Enter 1st number =
");
scanf("%d", &num1);
printf("Enter 2nd number =
");
scanf("%d", &num2);
printf("Enter 3rd number =
");
scanf("%d", &num3);
int maxNumber = findMax(num1, num2,
num3);
printf("\nMaximum number =
%d", maxNumber);
getch();
}
int findMax(int num1, int num2, int num3)
{
if (num1 > num2)
{
if (num1 > num3)
{
return
num1;
}
else
{
return
num3;
}
}
else
{
if (num2 > num3)
{
return
num2;
}
else
{
return
num3;
}
}
return num1;
}
Output:
Enter 1st number = 3
Enter 2nd number = 4
Enter 3rd number = 5
Maximum number = 5
4. Write a program to add first n number using
function.
Program:
#include <stdio.h>
#include <conio.h>
int sumToN(int);
void main()
{
clrscr();
int num;
printf("Enter the number =
");
scanf("%d", &num);
int sum = sumToN(num);
printf("\nThe sum = %d",
sum);
getch();
}
int sumToN(int num)
{
int sum = 0;
for (int i = 1; i <= num; i++)
{
sum += i;
}
return sum;
}
Output:
Enter the number = 5
The sum = 15
Please follow me for more practicals....
5. Write a program to find sum of odd number
between 1 to n where n is entered by user.
Program:
#include <stdio.h>
#include <conio.h>
int sumToN(int);
void main()
{
clrscr();
int num;
printf("Enter the number =
");
scanf("%d", &num);
int sum = sumToN(num);
printf("\nThe sum of Odd number
= %d", sum);
getch();
}
int sumToN(int num)
{
int sum = 0;
for (int i = 1; i <= num; i += 2)
{
sum += i;
}
return sum;
}
Output:
Enter the number = 5
The sum of Odd number = 9
6. Write a program to demonstrate string.h
function.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char str[50];
printf("Enter the string =
");
gets(str);
printf("\n%d",
strlen(str));
printf("\n%s",
strlwr(str));
printf("\n%s",
strupr(str));
printf("\n%s",
strrev(str));
getch();
}
Output:
Enter the string = Mehdiali Kadiwala
17
mehdiali kadiwala
MEHDIALI KADIWALA
ALAWIDAK ILAIDHEM
Please follow me for more practicals....
7. Write a program to demonstrate math.h
function.
Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
// clrscr();
printf("Math.h
Function\n");
printf("\n%f", ceil(3.6));
printf("\n%f",
floor(3.6));
printf("\n%f", sqrt(16));
printf("\n%f", pow(2, 4));
printf("\n%d", abs(-12));
getch();
}
Output:
Math.h Function
4.000000
3.000000
4.000000
16.000000
12
8. Write a program to swap variable value using
call by value.
Program
#include <stdio.h>
#include <conio.h>
void swap(int, int);
void main()
{
clrscr();
int x,
y;
printf("\n\nEnter the value of x = ");
scanf("%d",
&x);
printf("Enter the value of y = ");
scanf("%d", &y);
swap(x,
y);
printf("\nValue outside function: x = %d, y = %d", x, y);
getch();
}
void swap(int x, int y)
{
int temp
= x;
x = y;
y =
temp;
printf("\nValue inside function: x = %d, y = %d", x, y);
}
Output:
Enter the value of x = 4
Enter the value of y = 5
Value inside function: x = 5, y = 4
Value outside function: x = 4, y = 5
Please follow me for more practicals....
9. Write a program to swap variable value using
call by reference.
Program:
#include <stdio.h>
#include <conio.h>
void swap(int *, int *);
void main()
{
clrscr();
int x, y;
printf("\n\nEnter the value of
x = ");
scanf("%d", &x);
printf("Enter the value of y =
");
scanf("%d", &y);
swap(&x, &y);
printf("\nValue outside
function: x = %d, y = %d", x, y);
getch();
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
printf("\nValue inside
function: x = %d, y = %d", *x, *y);
}
Output:
Enter the value of x = 4
Enter the value of y = 5
Value inside function: x = 5, y = 4
Value outside function: x = 5, y = 4
Please follow me for more practicals....
10. Write a program to find factorial of given
number using recursion.
Program:
#include <stdio.h>
#include <conio.h>
int factorial(int);
int main()
{
clrscr();
int num;
printf("Enter the num =
");
scanf("%d", &num);
int fact = factorial(num);
printf("\nFactorial = %d",
fact);
getch();
return 0;
}
int factorial(int num)
{
if (num == 0)
{
return 1;
}
return num * factorial(num - 1);
}
Output:
Enter the num = 5
Factorial = 120