Lab4 211
Lab4 211
h>
2 #include <stdlib.h>
3
4 //5. Program to find square of a number using functions.
5
6 //declaring function
7 int square(int);
8
9 int main()
10 {
11 int r, n;
12 printf("enter the number to find square: ");
13 scanf("%d", &n);
14 //calling function to find square
15 r = square(n);
16 printf("the square of the given number is : %d", r);
17
18 return 0;
19 }
20
21 //defining body of function to find square of a number
22 int square(int n)
23 {
24 return (n *n);
25 }
26
27 OUTPUT
28 enter the number to find square: 10
29 the square of the given number is : 100
30
31 //2. Program to show table of a number using functions.
32
33 //function declaration
34 void table();
35
36 //main function
37 int main()
38 {
39 //calling function to show table of a number
40 table();
41 return 0;
42 }
43
44 //defining the body of the function
45 void table()
46 {
47 int n, i, r;
48 printf("enter the number of table: ");
49 scanf("%d", &n);
50 for (i = 1; i <= 10; i++)
51 {
52 r = n * i;
53 printf("%d*%d=%d\n", n, i, r);
54 }
55 }
56
57 //1. Program to find factorial of a number using recursion
58 //Function declaration
59 int fact(int);
60
61 int main()
62 {
63 int n, f;
64 printf("\nEnter a number: ");
65 scanf("%d", &n);
66 //calling a function to find factorial
67 f = fact(n);
68 printf("\nFactorial of %d is: %d", n, f);
69 return 0;
70 }
71
72 //function to find factorial of a number
73 int fact(int n)
74 {
75 if (n == 1)
76 return 1;
77 else
78 return (n* fact(n - 1));
79 }
80
81 OUTPUT
82 Enter a number: 3
83 Factorial of 3 is: 6
84
85 //3. Program to swap two numbers using functions
86 void swap(int *,int *);
87
88 int main()
89 {
90 int x, y, temp;
91 printf("Enter the value of x and y\n");
92 scanf("%d%d", &x, &y);
93 printf("Before Swapping\nx = %d\ny = %d\n", x, y);
94 swap(&x, &y);
95 printf("after Swapping\nx = %d\ny = %d\n", x, y);
96
97 return 0;
98 }
99
100 void swap(int *x, int *y)
101 {
102 int temp;
103 temp = *x;
104 *x = *y;
105 *y = temp;
106 }
107
108
109