forked from eggheadio-github/stack-overflow-copy-paste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment1.c
564 lines (544 loc) · 13.3 KB
/
assignment1.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include<stdio.h>
//(a) //use to find the value of b and c if a =300 but this one fault cuz 300can not >//
/*main( )
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ; //no curly bracket it execute only one line after if//
c = 200 ; printf ( "\n%d %d", b, c ) ;
}*/
/*(b)main( ) //a=500>=400 condition is true so so it print true number b=300,c=200//
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ;
} */
/*(c)//
main( )
{
int x = 10, y = 20 ;
if ( x == y );//buz of semi column it execute 10 20 as simple//but if we clear semi column it won't show anything//
printf ( "\n%d %d", x, y ) ;
} */
/*(d)
main( )
{
int x = 3,
y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else //cuz of semicolumn it execute so result is 3 &5 it is false//
printf ( "\n%d", y ) ;
}*/ //output is 3 and 5 cuz it is false condition if it true it will show only 3//
/*(e)
main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ; //output x and y are equal// it is true//
else
printf ( "\nx and y are not equal" ) ; }*/
/*(f)
main( )
{
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
printf ( "\nx = %d y = %d z = %d", x, y, z ) ;//x=10 y=10 z=0//z is fault cuz z=10 can not<10//
} */
/*(g)
main( )
{
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;//execute from right to left k>40 is false //
//k=50 assignt new value so it is true //and k==35 it is not true cuz new value of k is 50 so 50 can not equal 35 so it is false//
//output is 0 50 0//
} */
/*(h)
main( )
{ int i = 65 ;
char j = 'A' ;
if ( i == j )
printf ( "C is WOW" ) ; //output is c is wow //so it is true that i=j//
else
printf( "C is a headache" ) ;
} */
/*(i)
main( )
{
int a = 5, b, c ;
b = a = 15 ;
c = a < 15 ;
printf ( "\na = %d b = %d c = %d", a, b, c ) ; //output a=15 b=15 c=0 //c=0 cuz c=a=5 so 15 can not<15 so it is false//
} */
/*(j)
main( )
{
int x = 15 ;
printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ; //output is 1 20 1 cuz it execute from right to left//
} */
//[B] Point out the errors, if any, in the following programs:
/*(a) main( )
{ float a = 12.25, b = 12.52 ;
if ( a = b )//error cuz wrong"="it has to == that can true and the true one the output didn't show
printf ( "\na and b are equal" ) ; //output is a n b are equal//false
}*/
/*(b)
main( )
{
int j = 10,
k = 12 ;
if ( k >= j )
{
{
k = j ;
j = k ; //has a lot of curly bracket//
}
}
} */
//(c)
/*main( )
{
if ( 'X' < 'x' )
printf ( "\nascii value of X is smaller than that of x" ) ;// no error cuz ascii value of 'X'<'x'//
} */
/*(d)
main( )
{ int x = 10 ;
if ( x >= 2 )
then //error cuz of then //
printf ( "\n%d", x ) ;
} */
/*(e)
main( )
{ int x = 10 ;
if x >= 2//no bracket()//so it is error//
printf ( "\n%d", x ) ;
} */
/*(f)
main( )
{ int x = 10, y = 15 ;
if ( x % 2 = y % 3 )// error cuz '=' it has to have '=='//
printf ( "\nCarpathians" ) ;
}*/
/*(g)
main( )
{
int x = 30 , y = 40 ;
if ( x == y )
printf( "x is equal to y" ) ;
elseif ( x > y ) //error cuz of else if//
printf( "x is greater than y" ) ;
elseif ( x < y )
printf( "x is less than y" ) ;
}*/
/*(h)
main( )
{ int x = 10 ;
if ( x >= 2 )
then //error cuz of then//
printf ( "\n%d", x ) ;
} */
/*(i)
main( )
{
int a, b ;
scanf ( "%d %d",a, b ) ;
if ( a > b ) ;//cuz of semi column//if don't have it no error;
printf ( "This is a game" ) ;
else
printf ( "You have to play it" ) ;
}*/
//[C] What would be the output of the following programs:
/*(a)
main( )
{
int i = 4, z = 12 ;
if ( i =5 || z > 50 ) //new value so it always true
printf ( "\nDean of students affairs" ) ;
else
printf ( "\nDosa" ) ; //output is dean of student affair//
} */
/*(b)
main( )
{
int i = 4, z = 12 ;
if ( i = 5 && z > 5 )
printf ( "\nC Programming" ) ;
else printf ( "\nWish C was Easy !" ) ; //output is c programming//
} */
/*(c)
main( )
{
int i = 4,
j = -1, k = 0, w, x, y, z ;
w = i || j || k ;
x = i && j && k ;
y = i || j && k ;
z = i && j || k ;
printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ; //output is w=1 x=0 y=1 z=1 follow binary//
//condition is not true in x//
} */
/*(d)
main( )
{
int i = 4, j = -1, k = 0, y, z ;
y = i + 5 && j + 1 || k + 2 ;
z = i + 5 || j + 1 && k + 2 ;
printf ( "\ny = %d z = %d", y, z ) ; //output is x=1 y=1 no condition false//
} */
/*(e) main( )
{
int i = -3, j = 3 ;
if ( !i + !j * 1 ) //output is bennarivo cuz condition false//it execute
printf ( "\nMassaro" ) ;
else
printf ( "\nBennarivo" ) ;
} */
/*(f)
main( )
{
int a = 40 ;
if ( a > 40 && a < 45 )
printf ( "a is greater than 40 and less than 45" ) ;
else printf ( "%d", a ) ; //output is 40 so it show else not show print it mean that condition is false//
} */
/*(g) main( )
{
int p = 8, q = 20 ;
if ( p == 5 && q > 5 )
printf ( "\nWhy not C" ) ;
else
printf ( "\nDefinitely C !" ) ; //output is definitely c//cuz condtion is false//p=8 can not print p==5
} */
/*(h)
main( )
{
int i = -1, j = 1, k ,l ;
k = i && j ; l = i || j ; //just make us confused//
printf ( "%d %d", i, j ) ; //output is i=-1 and j=1//
} */
/*(i)
main( )
{
int x = 20 , y = 40 , z = 45 ;
if ( x > y && x > z )
printf( "x is big" ) ;
else if ( y > x && y > z )
printf( "y is big" ) ;
else if ( z > x && z > y )
printf( "z is big" ) ; //output is z is big cuz condition is false//
} */
/*(j)
main( )
{
int i = -1, j = 1, k ,l ;
k = !i && j ; l = !i || j ;//just make us confused//
printf ( "%d %d", i, j ) ;//output is -1 and 1 //
} */
/*(k)
main( )
{
int j = 4, k ;
k = !5 && j ;
printf ( "\nk = %d", k ) ;//output k=0 cuz false&true=false//
} */
//[D] Point out the errors, if any, in the following programs:
/*(a) /* This program /* is an example of /* using Logical operators */
/*main( )
{
int i = 2, j = 5 ;
if ( i == 2 && j == 5 )
printf ( "\nSatisfied at last" ) ; //no error output is satisfied at last//
} */
/*(b)
main( )
{
int code, flag ;//output is nothing cuz conditon is false//cuz flag and code is not declare in num
if ( code == 1 & flag == 0 )
printf ( "\nThe eagle has landed" ) ;
}*/
/*(c)
main( )
{
char spy = 'a', password = 'z' ;
if ( spy == 'a' or password == 'z' ) //error is cuz of 'or' it has to use &&//
printf ( "\nAll the birds are safe in the nest" ) ;
}*/
/*(d)
main( )
{
int i = 10, j = 20 ;
if ( i = 5 ) && if ( j = 10 )//error cuz of &&//
printf ( "\nHave a nice day" ) ;
}*/
/*(a)
main( )
{
int x = 10 , y = 20;
if ( x >= 2 and y <=50 )//it error cuz of and it has to use '&'//
printf ( "\n%d", x ) ;
} */
/*(b)
main( )
{ int a, b ;
if ( a == 1 & b == 0 )
printf ( "\nGod is Great" ) ; //no error but function is false//
} */
/*(c)
main( )
{ int x = 2;
if ( x == 2 && x != 0 ) ;//cuz of semicolumn//
{
printf ( "\nHi" ) ;
printf( "\nHello" ) ;
}
else
printf( "Bye" ) ;
} */
/*(d)
main( )
{ int i = 10, j = 10 ;
if ( i &&( j == 10 ) ) //no error cuz condition true//i have a value so it is true and j have a value
//it is true too so it execute and if j false conditioln will false//
printf ( "\nHave a nice day" ) ;
}*/
//[E] What would be the output of the following programs:
/*(a)
main( )
{
int i = -4, j, num ;
j = ( num < 0 ? 0 : num * num ) ; //this row like if(?) and else(:) if (if) false it woll execute else
printf ( "\n%d", j ) ; //output=1//
}*/
/*(b)
main( )
{ int k,
num = 30 ;
k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;//it just make us confuse//
printf ( "\n%d", num ) ; //output is 30//
} */
/*(c)
main( )
{
int j = 4 ; ( !j != 1 ? printf ( "\nWelcome") : printf ( "\nGood Bye") ) ;//output is welcome
//cuz condition is true//
}*/
//[F] Point out the errors, if any, in the following programs:
/*(a)
main( )
{
int tag = 0, code = 1 ;
if ( tag == 0 ) ( code > 1 ? printf ( "\nHello" ) ? printf ( "\nHi" ) ) ;//error cuz of ? 2 time//
//if true have to use only ? and : one
else printf ( "\nHello Hi !!" ) ;
} */
/*(b)
main( )
{
int ji = 68 ;
printf ( "\nji >= 65 ? %d : %c", ji ) ;//error output//if dun want output error use one more ,ji
//for (%d: %c,ji,ji)out put will true//
}*/
/*(c)
main( )
{
int i = 10, j ; i >= 5 ? ( j = 10 ) : ( j = 15 ) ;
printf ( "\n%d %d", i, j ) ; //no error //
}*/
/*(d)
main( )
{ int a = 5 , b = 6 ; a == b ; printf ( "%d",a);
}*/
/*(e)
main( )
{
int n = 9 ; ( n == 9 ? printf( "You are correct" ) : printf( "You are wrong" ) ) ;
}*/ //no error//condition true//
/*(f)
main( )
{
int kk = 65 ,ll ;
ll = ( kk == 65 : printf ( "\n kk is equal to 65" ) : printf ( "\n kk is not equal to 65" ) ) ;
printf( "%d", ll ) ; //error cuz of : we dun have to use : in front we have to use ? instead//
}*/
/*(g)
main( )
{
int x = 10, y = 20 ;
x == 20 && y != 10 ? printf( "True" ) : printf( "False" ) ;//no error//output false //
}*/
//[F] Rewrite the following programs using conditional operators.
/*(a)
main( )
{
int x, min, max ;
scanf ( "\n%d %d", &max, &x ) ;
if ( x > max ) max = x ;
else min = x ;
//rewrite/////////x>max||max==x?printf("true"):printf("false");
} */
/*(b)
main( )
{ int code ; scanf ( "%d", &code ) ;
if ( code > 1 )
printf ( "\nJerusalem" ) ;
else if ( code < 1 )
printf ( "\nEddie" ) ;
else printf ( "\nC Brain" ) ;
//rewrite///(code>1?printf("true"):(code<1?printf("false"):printf("unavailable")));
} */
/*(c)
main( )
{
float sal ;
printf ("Enter the salary" ) ;
scanf ( "%f", &sal ) ;
if ( sal < 40000 && sal > 25000 )
printf ( "Manager" ) ;
else if ( sal < 25000 && sal > 15000 )
printf ( "Accountant" ) ;
else printf ( "Clerk" ) ;
//rewrite////sal<40000&&sal>25000?printf("true"):(sal<25000&&sal>15000?printf("false"):printf("unavailable"));
}*/
/*#include<math.h>
main()
{
float a,b,c;
float d,root1,root2;
printf("enter the value of a");
scanf("%f",&a);
printf("enter the value of b");
scanf("%f",&b);
printf("enter the value of c");
scanf("%f",&c);
d=b*b-4*a*c;
if(d>0)
{
printf("root1 and 2 are real number");
root1=(-b+sqrt(d)/2*a);
printf("root1 is %.2f",root1);
root2=(-b-sqrt(d)/2*a);
printf("root2 is %.2f",root2);
}
else if (d==0)
{
root1=root2=(-b/2*a);
printf("root1=root2 is %.2f",root1);
}
else
printf("root1 and 2 are complex number");
}*/
/*main()
{
int a;
printf("enter the number ");
scanf("%d",&a);
if(a<0)
printf("number is negative");
else if(a==0)
printf("number = 0");
else
printf("number is positive");
}*/
/*main()
{
int a;
printf("enter the years");
scanf("%d",&a);
if(a%4==0)
{
if(a%100==0&&a%400==0)
printf("this year is leap year");
else
printf("this is common year");
}
}*/
/*main()
{
int a,b,c;
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
printf("enter the value of c");
scanf("%d",&c);
if(a>=b && a>=c)
printf("Greates is %d",&a);
else if(b>=a && b>=c)
printf("Greates is %d",&b);
else if(c>=a && c>=b)
printf("Greates is %d",&c);
}*/
/*main()
{
char sa;
printf("enter the character");
scanf("%c",&sa);
if(sa=='a'||sa=='e'||sa=='i'||sa=='o'||sa=='u')
{
printf("the character is vowel");
}
else
{
printf("the character is consonnent");
}
}*/
/*main()
{
int a;
printf("enter the value of a");
scanf("%d",&a);
if(a%2==0)
{
printf("the number is even number");
}
else
{
printf("the number is odd number");
}
}*/
/* main()
{
int a,b,c;
printf("enter the value of a");
scanf ("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
c=a;
a=b;
b=c;
printf("after swaping is %d",a);
printf("after swaping is %d",b);
}*/
/*main()
{
int a,b,c;
printf("enter the value of a");
scanf ("%d",&a);
printf("enter the value of b");
scanf ("%d",&b);
c=a*b;
printf("the area of rectangle is %d",c);
}*/
// #include <stdio.h>
//
// main()
// {
// //char c;
// float celcius, Fahrenheit;
// printf ("enter the value of celcius\n");
// scanf ("%f",&celcius);
// Fahrenheit=(celcius*9/5)+32;
// printf ("%f deg celcius is fahrenheit %f",celcius,Fahrenheit);
// printf("Enter a character: ");
//
// scanf("%c", &c);
//
// // %d displays the integer value of a character
// // %c displays the actual character
// printf("ASCII value of %c = %d",c,c);
// return 0;