-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPracticeQs.c
392 lines (310 loc) · 7.4 KB
/
PracticeQs.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <stdio.h>
#include <math.h>
void namaste();
void bonjour();
int factorial(int n);
int fibonacci(int n);
void printAddress(int n);
void sumnproduct(int a, int b, int *sum, int *product, int *avg);
void reverse(int arr[], int n);
int printArray(int arr[], int n);
void printString(char arr[]);
int main()
{
/* 1.Calculate area of a square (side is given)
float side;
scanf("%f",&side);
float area = side * side;
printf("Area of square is: %f",area);*/
/* 2.Calculate area of a square(radius is given)
float radius;
float pi=3.14;
printf("Enter radius: ");
scanf("%f",&radius);
float area = pi*radius * radius;
printf("Area of square is: %f",area);*/
/*3.Check if a number is divisible by 2 or not
int a;
printf("Enter number: ");
scanf("%d",&a);
printf("%d",a % 2 == 0);*/
/*4.Check if given character is digit or not
char ch;
printf("Enter Character: ");
scanf("%c",&ch);
// printf("ch is digit %c",ch>='0' && ch<='9');
if(ch>='0' && ch<='9')
{
printf("%c is DIGIT.", ch);
}
else
{
printf("%c is NOT DIGIT.", ch);
}*/
/*5.print the smallest number
int a,b;
scanf("%d",&a);
scanf("%d",&b);
if(a<b){
printf("Smallest number : %d",a);
}
else{
printf("Smallest number : %d",b);
}*/
/*6.Write a program to check if a student passed or failed*/
/*int marks;
printf("Enter marks(0-100): ");
scanf("%d",&marks);
if(marks>=35 && marks<=100){
printf("Pass");
}
else if(marks>=0 && marks<35){
printf("Fail");
}
else{
printf("wrong marks");
}*/
// Using ternary
// marks>=35 ? printf("pass") : printf("fail");
/*7. Program to give grades to a student
if(marks <= 35){
printf("C");
}
else if(marks >= 35 && marks<70){
printf("B");
}
else if(marks >= 70 && marks<90){
printf("A");
}
else if(marks >= 90 && marks<=100){
printf("A+");
}
else{
printf("marks should be in 1-100");
}*/
/*8. Program to find if a character entered by user is upper case or not
char ch;
printf("Enter character: ");
scanf("%s",&ch);
if(ch>='A' && ch <= 'Z'){
printf("Uppercase");
}
else{
printf("lowercase");
}*/
/*9. Print the numbers from 0 to n,if n is given by user
int n;
printf("Enter Number: \n");
scanf("%d",&n);
for(int i=0;i<=n;i++){
printf("%d\n",i);
}*/
/*10. Print sum of first n natural numbers and also print them in reverse
int n;
int sum=0;
printf("Enter Number: \n");
scanf("%d",&n);
if(n==0){
printf("Natural numbers starts from 1");
}
printf("Reverse: ");
for(int i=1,j=n;i<=n && j>=1;i++,j--){
sum = sum + i;
printf("%d",j);
}
printf("\nSum of natural numbers: %d \n",sum);*/
/*11. Print the table of a number input by the user
int n;
printf("Enter Number: ");
scanf("%d",&n);
printf("Table of %d",n);
for(int i=1;i<=10;i++){
printf("\n%d",i*n);
}*/
/*12. Keep taking numbers as input until user enters an odd number
int n;
printf("Enter number: ");
while(n>0){
scanf("%d",&n);
if(n%2 != 0){
break;
}
}
printf("Thank you");*/
/*13. Keep taking numbers as input until user enters a number which is multiple of 7
int n;
printf("Enter number: ");
while(n>0){
scanf("%d",&n);
if(n%7 == 0){
break;
}
}
printf("Thank you");*/
/*14.Print all odd numbers from 5 to 50
for (int i = 5; i <= 50; i++)
{
if (i %2 == 0)
{
continue; // skip
}
printf("%d\n", i);
}*/
/*15. Print the factorial of a number n*/
/*16. Print reverse of table for a number n*/
/*17. Write a fun that prints namaste if user is indian and bonjour if the user is French
printf("enter f for french and i for indian: ");
char ch;
scanf("%c", &ch);
if (ch == 'i')
{
namaste();
}
else
{
bonjour();
}*/
/*18. Use library functions to calculate the square of a number given by user
int n;
printf("Enter number: ");
scanf("%d",&n);
printf("Square of a number is: %f",pow(n,2));*/
/*19. Factorial of n using recursion
int f = factorial(3);
printf( "Factorial is: %d",f);*/
/*20. Write a fun to print n terms of the fibonacci sequence
int n;
printf("Enter number: ");
scanf("%d",&n);
int f = fibonacci(n);
printf("%d",f);*/
/*21.Find Output
int *ptr;
int x;
ptr = &x;
*ptr = 0;
printf("x=%d\n",x);
printf("*ptr = %d\n",*ptr);
*ptr += 5;
printf("x=%d\n",x);
printf("*ptr = %d\n",*ptr);
(*ptr)++;
printf("x=%d\n",x);
printf("*ptr = %d\n",*ptr);*/
/*22. Print value of i from pointer to pointer
int i=22;
int *ptr = &i;
int **pptr = &ptr;
printf("%d",**pptr);*/
/*23. Will the address output be same?
int n=4;
printf("%p\n",&n);
printAddress(n);*/
/*24.Write a function to calculate the sum,product&average of 2 numbers.Print that average in the main function
int a = 3, b = 5;
int sum, product, avg;
sumnproduct(a, b, &sum, &product, &avg);
printf("sum=%d, product=%d, Average=%d\n ", sum, product, avg);*/
/*25. Write a program to enter price of 3 items and print their final cost with gst
float price[3];
printf("Enter Price: \n");
scanf("%f",&price[0]);
scanf("%f",&price[1]);
scanf("%f",&price[2]);
printf("total price 1: %f\n",price[0]+(0.18*price[0]));
printf("total price 1: %f\n",price[1]+(0.18*price[0]));
printf("total price 1: %f\n",price[2]+(0.18*price[0]));*/
/*26.Write a function to count the number of odd numbers in an array
int count = 0;
int arr[]={1,2,3,7,5};
for(int i=0;i<5;i++){
if(arr[i] % 2 !=0){
count++;
}
}
printf("Total count of odd numbers: %d",count);*/
/*27.Write a function to revere an array
int arr[] = {1, 2, 3, 4, 5};
for (int i = 4; i >= 0; i--)
{
printf("%d\n", arr[i]);
}
// classical method
reverse(arr, 5);
printArray(arr, 5);*/
/*28.Create a string firstname and lastname to store details of user and print all the characters using a loop*/
char firstname[] = "Arpita";
char lastname[] = "Sawant";
printString(firstname);
printString(lastname);
return 0;
}
void namaste()
{
printf("Namaste\n");
}
void bonjour()
{
printf("Bonjour\n");
}
int factorial(int n)
{
if (n == 1)
{
return 1;
}
int fact = factorial(n - 1);
int num = fact * n;
return num;
}
int fibonacci(int n)
{
if (n == 1 || n == 0)
{
if (n == 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
}
int fibN1 = fibonacci(n - 1);
int fibN2 = fibonacci(n - 2);
int fibN = fibN1 + fibN2;
// printf("fib of %d is: %d",n,fibN);
return fibN;
}
void printAddress(int n)
{
printf("%p\n", &n);
}
void sumnproduct(int a, int b, int *sum, int *product, int *avg)
{
*sum = a + b;
*product = a * b;
*avg = (a + b) / 2;
}
void reverse(int arr[], int n)
{
for (int i = 0; i < n / 2; i++)
{
int firstval = arr[i];
int secval = arr[n - i - 1];
arr[n - i - 1] = firstval;
arr[i] = secval;
}
}
int printArray(int arr[], int n){
for(int i=0;i<n;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
void printString(char arr[]){
for(int i=0;arr[i]!='\0';i++){
printf("%c",arr[i]);
}
printf("\n");
}