Monday, March 27, 2023

 

C Basic: Exercises

 1. Write a C program to print your name, date of birth, and mobile number.         #include <stdio.h>                int main()                  {                  printf("Name : Alexandra Abramov\n");                  printf("DOB : July 14, 1975\n");                 printf("Mobile : 99-9999999999\n");                  return(0);                  }

2. Write a C program to print the following characters in reverse.

                         #include <stdio.h>             int main()             {             char char1 = 'X';             char char2 = 'M';             char char3 = 'L';             printf("The reverse of %c%c%c is %c%c%c\n",      char1, char2, char3, char3, char2, char1);         return(0);             }

3.Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches

 #include <stdio.h> /* height and width of a rectangle in inches */ int width; int height; int area; int perimeter; int main() { height = 7; width = 5; perimeter = 2*(height + width); printf("Perimeter of the rectangle = %d inches\n", perimeter); area = height * width; printf("Area of the rectangle = %d square inches\n", area); return(0); }

4. Write a C program to convert specified days into years, weeks and days

 #include <stdio.h> int main() { int days, years, weeks; days = 1329; // Converts days to years, weeks and days years = days/365; weeks = (days % 365)/7; days = days- ((years*365) + (weeks*7)); printf("Years: %d\n", years); printf("Weeks: %d\n", weeks); printf("Days: %d \n", days); return 0; }

5.Write a C program that accepts two integers from the user and calculates the sum of the two integers. 

#include <stdio.h> int main() { int x, y, sum; printf("\nInput the first integer: "); scanf("%d", &x); printf("\nInput the second integer: "); scanf("%d", &y); sum = x + y; printf("\nSum of the above two integers = %d\n", sum); return 0; }

 6.Write a C program that accepts three integers and finds the maximum of three

 #include <stdio.h> #include <stdlib.h> int main() { int x, y, z, result, max; printf("\nInput the first integer: "); scanf("%d", &x); printf("\nInput the second integer: "); scanf("%d", &y); printf("\nInput the third integer: "); scanf("%d", &z); result=(x+y+abs(x-y))/2; max=(result+z+abs(result-z))/2; printf("\nMaximum value of three integers: %d", max); printf("\n"); return 0; }

 

 

 

No comments:

Post a Comment