{%hackmd @themes/orangeheart %} # Lab Practices ## Reversing Strings Write a C program to print the following characters in a reverse way. est Characters: 'X', 'M', 'L' Expected Output: ``` The reverse of XML is LMX ``` ### Code **Brute Force Way** ```C #include <stdio.h> int main() { char a,b,c; printf("Type your characters:\n"); scanf("%c%c%c",&a,&b,&c); printf("The reverse of %c%c%c is %c%c%c",a,b,c,c,b,a); } ``` **Using `<string.h>` and arrays** ```c #include <stdio.h> #include <string.h> int main() { char a[3]; printf("Type your string here!\n"); scanf("%s",&a); printf("The reverse of %s",a); printf(" is %s",strrev(a)); return 0; } ``` ## Perimeter and Radius Write a C program to take a number from the input as the radius and compute the perimeter and area of a circle using the input. ``` Perimeter = 2 * 3.14 * radius Area= 3.14 * radius * radius Expected Output: Perimeter of the Circle = xxx.xx inches Area of the Circle = xxx.xx square inches. ``` ### Code ```C #include <stdio.h> #define pi 3.14 int main() { float radius, perimeter, area; printf("Radius?\n"); scanf("%f", &radius); perimeter = 2*radius*pi; area = pi*radius*radius; printf("The perimeter of the circle is %.2f inches\n",perimeter); printf("the area of the circle is %.2f square inches\n",area); } ``` ## Precedences ### Code ```c #include <stdio.h> int main() { int a = 125, b = 12345; long ax = 1234567890; short s = 4043; float x = 2.13459; double dx = 1.1415927; char c = 'W'; unsigned long ux = 2541567890; printf("a = %d\nb = %d\nax = %ld\ns = %hu\nx = %f\ndx = %lf\nc = %c\nux = %lu\n",a,b,ax,s,x,dx,c,ux); printf("a+c = %d\nx+c = %f\n",a+c,x+c); printf("%lf\n",dx+x); printf("%ld",((int) dx + ax)); printf("%f\n",a+x); printf("%d\n",s+b); printf("%ld\n",ax+b); printf("%hu\n",s+c); printf("%ld\n",ax+c); printf("%lu\n",ax+ux); } ``` ## Time Formatter (Days) ```c #include <stdio.h> int main() { int days, day, week, year; printf("How many days?\n"); scanf("%d",&days); year = days/365; week = (days%365)/7; day = (days%365)%7; printf("%d years %d weeks %d days",year,week,day); } ``` ## Find the Greatest Value ```C #include <stdio.h> int main() { int a,b,c; printf("type a b and c\n"); scanf("%d\n%d\n",&a,&b,&c); if (a > b && a > c) printf("a is the greatest\n"); else if (b > a && b > c) printf("c is the greatest\n"); else printf("b is the greatest\n"); return 0; } ``` ## Time Formatter (Seconds) ```C #include <stdio.h> int main() { int seconds, second, minute, hour; printf("How many seconds?\n"); scanf("%d",&seconds); hour = seconds/3600; minute = (seconds%3600)/60; second = (seconds%3600)%60; printf("%d hours %d minutes %d seconds",hour,minute,second); } ``` Simplified: ```C #include <stdio.h> int main() { int seconds; printf("How many seconds?\n"); scanf("%d",&seconds); printf("%d hours %d minutes %d seconds",seconds/3600,(seconds%3600)/60,(seconds%3600)%60); } ``` ## Nesting Conditionals ```C #include <stdio.h> int main() { int p,q,r,s; printf("p,q,r,s\n"); scanf("%d%d%d%d",&p,&q,&r,&s); if (r < 0 || s < 0 || p%2 != 0){ printf("Wrong Values \n"); } if (q > r && s > p){ if (r+s > p+q){ printf("Correct Values\n"); } else{ printf("Wrong Values\n"); } } else{ printf("Wrong Values\n"); } } ``` ## Range of 80 ```C #include <stdio.h> int main() { int n; scanf("%d",&n); if (n>0 && n<80){ printf("In the range of 80"); } else{ printf("Not in the range of 80"); } } ``` ## Sum of Odds ```C #include <stdio.h> int main() { int a,b,c,d,e,sum = 0; scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); if (a%2 != 0) sum += a; if (b%2 != 0) sum += b; if (c%2 != 0) sum += c; if (d%2 != 0) sum += d; if (e%2 != 0) sum += e; printf("sum is %d",sum); } ``` ## Closer to 100 ```c #include <stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); if (a > 0 && b > 0){ if (a > 100 & b > 100){ a -= 100; b -= 100; if (a > b){ printf("b is closer!\n"); } else{ printf("a is closer!\n"); } } else{ a = 100 - a; b = 100 - b; if (a > b){ printf("b is closer!\n"); } else{ printf("a is closer!\n"); } } } } ```