-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser Defined Functions.c
More file actions
151 lines (138 loc) · 3.74 KB
/
User Defined Functions.c
File metadata and controls
151 lines (138 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Write a user defined function Max( ) which will find maximum number from entered three numbers.
#include <stdio.h>
int Max(int a, int b, int c) {
if (a >= b && a >= c) {
return a;
} else if (b >= a && b >= c) {
return b;
} else {
return c;
}
}
int main() {
int x, y, z;
printf("Enter three numbers: ");
scanf("%d %d %d", &x, &y, &z);
printf("The maximum number is %d\n", Max(x, y, z));
return 0;
}
// Write a user defined function Even ( ) which will check enter number is even or odd. If it is even, it return 1 else 0.
#include <stdio.h>
int Even(int num) {
return (num % 2 == 0) ? 1 : 0;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (Even(num)) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
return 0;
}
// Write a program to find factorial of given number using a recursive function.
#include <stdio.h>
int Factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * Factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", num, Factorial(num));
}
return 0;
}
// Write a uder defind function Isupper( ) which will check enter character is in upper case or not. If it is in upper case, convert into lower case using another user defined function Convert( ).
#include <stdio.h>
#include <ctype.h>
void Convert(char *ch) {
*ch = tolower(*ch);
}
int Isupper(char ch) {
return isupper(ch);
}
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (Isupper(ch)) {
Convert(&ch);
printf("The character in lowercase is %c\n", ch);
} else {
printf("The character is not uppercase.\n");
}
return 0;
}
// Write a user defined function Rank( ) which will print the index of the student has maximum total marks. Total number of student are 50 and three sessional marks. Write an appropriate main( ) for demonstrating the use of Rank( ).
#include <stdio.h>
#define NUM_STUDENTS 50
void Rank(int marks[][3]) {
int i, j;
int max_total = 0;
int max_index = 0;
int total;
for (i = 0; i < NUM_STUDENTS; i++) {
total = 0;
for (j = 0; j < 3; j++) {
total += marks[i][j];
}
if (total > max_total) {
max_total = total;
max_index = i;
}
}
printf("The student with the maximum total marks is at index %d\n", max_index);
}
int main() {
int marks[NUM_STUDENTS][3];
int i, j;
printf("Enter the sessional marks for 50 students (3 sessionals each):\n");
for (i = 0; i < NUM_STUDENTS; i++) {
printf("Student %d:\n", i + 1);
for (j = 0; j < 3; j++) {
printf("Sessional %d: ", j + 1);
scanf("%d", &marks[i][j]);
}
}
Rank(marks);
return 0;
}
// Write a user defined function Sort( ) which will sort the array element in ascending order.
#include <stdio.h
#define SIZE 10
void Sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
int main() {
int arr[SIZE];
int i;
printf("Enter %d elements:\n", SIZE);
for (i = 0; i < SIZE; i++) {
scanf("%d", &arr[i]);
}
Sort(arr, SIZE);
printf("Sorted array in ascending order:\n");
for (i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}