Advertisement
ppupil2

ws3

Feb 27th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <conio.h>
  5.  
  6. void binary(int a); // convert decimal to binary ~> print
  7. int summary(int a); // calculate summary of all digits of a
  8. int reverse(int a); // find the reverse of a
  9. int main() {
  10.     int n, n1, sum, rev, m, n2, z;
  11.     char ch, ch1, ch2; // char variables for inputcheck, can be reused
  12.     float a, b, c, delta, x1, x2;
  13.    
  14.     /*Q1*/
  15.     printf("Q1:\n\n");
  16.    
  17.     /* input & check validation*/
  18.     while (1)
  19.     {
  20.         printf("Enter  n = ");
  21.         fflush(stdin);
  22.         scanf("%d%c",&n,&ch);
  23.         if ((ch == '\n') || (ch == ' ')) {
  24.             break;
  25.         }
  26.         else {
  27.             printf("n must be a number, please re-enter!\n");
  28.         }
  29.     }
  30.    
  31.     /* convert to binary */
  32.     printf("%d in binary number format is: ", n);
  33.     binary(n);
  34.    
  35.     /* input again & summary, reverse digits */
  36.     printf("\n\nEnter  n = ");
  37.     scanf("%d", &n1);
  38.     sum = summary(n1);
  39.     printf("The sum of all digits in %d is %d\n", n1, sum);
  40.     rev = reverse(n1);
  41.     printf("The reverse number of %d is %d\n", n1, rev);
  42.        
  43.     /*Q2*/
  44.     printf("\n--------------------------------------------------------\nQ2:\n\n");
  45.    
  46.     /* input & check validation*/
  47.     ch = '\0'; ch1 = '\0'; ch2 = '\0';
  48.     while (1) {
  49.         printf("Enter a, b, c = ");
  50.         fflush(stdin);
  51.         scanf("%f%c%f%c%f%c", &a, &ch, &b, &ch1, &c, &ch2);
  52.         if (a != 0 && ch == ' ' && ch1 == ' ' && ch2 == '\n') {
  53.             break;
  54.         }
  55.         else {
  56.             printf("Invalid input!\n");
  57.         }
  58.     }
  59.    
  60.     /* solve the quadratic equation */ 
  61.     delta = pow(b,2) - 4*a*c;
  62.     if (delta<0) {
  63.         printf("No real root\n");
  64.     }
  65.     else if (delta == 0) {
  66.         x1 = -b/(2*a);
  67.         printf("Root 1 = Root 2 = %.4f\n", x1);
  68.     }
  69.     else {
  70.         x1= (-b + sqrt(delta)) /(2*a);
  71.         x2= (-b - sqrt(delta)) /(2*a);
  72.         printf("Root 1 = %.4f\n", x1);
  73.         printf("Root 2 = %.4f\n", x2);
  74.     }
  75.    
  76.     /*Q3*/
  77.     printf("\n--------------------------------------------------------\nQ3:\n\n");
  78.    
  79.     /* input & check validation*/
  80.     ch = '\0'; ch1 = '\0';
  81.     while (1) {
  82.         printf("Enter m, n: ");
  83.         fflush(stdin);
  84.         scanf("%d%c%d%c", &m, &ch, &n2, &ch1);
  85.         if (ch == ' ' && ch1 == '\n' && m<n2) {
  86.             break;
  87.         }
  88.         else {
  89.             printf("Invalid input!\n");
  90.         }
  91.     }
  92.    
  93.     /* find palindrome numbers */
  94.     printf("Palindrome numbers from %d to %d are:",m ,n2);
  95.     for (int i = m; i<=n2; i++) { // find first number and print " %d"
  96.        if(i == reverse(i)) {
  97.         printf(" %d", i);
  98.         z = i;
  99.         break;
  100.        }
  101.     }
  102.     for (int i = z+1; i<=n2; i++) { // find the other numbers and print ", %d"
  103.         if (i == reverse(i)) {
  104.             printf(", %d",i);
  105.         }
  106.     }
  107.     printf("\n");
  108.    
  109.     return(0); 
  110. }
  111.  
  112. /* function convert decimal to binary ~> print */
  113. void binary(int a) {
  114.     int bi[40], i;
  115.     for (i = 0; a>0; i++) {
  116.         bi[i] = a%2;
  117.         a /= 2;
  118.     }
  119.     for (int j = i-1;j >= 0; j--) {
  120.         printf("%d", bi[j]);
  121.     }
  122. }
  123.  
  124. /* function calculate summary of all digits of a */
  125. int summary(int a) {
  126.     int sum = 0;
  127.     while (a>0) {
  128.         sum += a%10;
  129.         a /= 10;
  130.     }
  131.     return(sum);
  132. }
  133.  
  134. /* function find the reverse of a */
  135. int reverse(int a) {
  136.     int rv = 0;
  137.     while (a>0) {
  138.         rv = rv*10 + a%10;
  139.         a /= 10;
  140.     }
  141.     return (rv);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement