Advertisement
Guest User

Assignment

a guest
Aug 25th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.47 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #include<math.h>
  5. #include<stdbool.h>
  6.  
  7. int menu () {
  8.     int choice;
  9.     printf("----------------Menu-Selector-----------------\n");
  10.     printf("------------------Assignment------------------\n");
  11.     printf("==============================================\n");
  12.     printf("= 1.  Sum of Integers                        =\n");
  13.     printf("= 2.  Swap values                            =\n");
  14.     printf("= 3.  vowels, consonants, others counter     =\n");
  15.     printf("= 4.  ASCII table                            =\n");
  16.     printf("= 5.  ASCII between 2 chars (ascending)      =\n");
  17.     printf("= 6.  Print prime numbers                    =\n");
  18.     printf("= 7.  Date Checker                           =\n");
  19.     printf("= 8.  Relative Position of the circle        =\n");
  20.     printf("= 9.  Print Factorial by n                   =\n");
  21.     printf("= 10. Print Fibonacci sequence by n          =\n");
  22.     printf("= 11. Fibonacci element checker              =\n");
  23.     printf("= 12. Sum Digits                             =\n");
  24.     printf("= 13. Fraction to real number convert        =\n");
  25.     printf("= 14. GCD and LCM by 2 pos int               =\n");
  26.     printf("= 15. Min Max of a nonnegative int           =\n");
  27.     printf("= 16. Small menu with process primes, minmax =\n");
  28.     printf("= Others. Quit                               =\n");
  29.     printf("==============================================\n");
  30.     printf("Please choose one: ");
  31.     scanf("%d", &choice);
  32.    
  33.     return choice;
  34. }
  35. //EX1
  36. void sumOfInt () {
  37.     int x;
  38.     int S = 0;
  39.     do {
  40.         printf("Input x: ");
  41.         scanf("%d", &x);
  42.         if (x != 0) {
  43.             S = S + x;
  44.         }
  45.     }
  46.     while (x != 0);
  47.     printf("S = %d\n", S);
  48. }
  49.  
  50. //EX2
  51. void intSwap () {
  52.     int x;
  53.     int y;
  54.     int temp;
  55.    
  56.     do {
  57.         printf("Input x: ");
  58.         scanf("%d", &x);
  59.         printf("Input y: ");
  60.         scanf("%d", &y);
  61.        
  62.         temp = x;
  63.         x = y;
  64.         y = temp;
  65.        
  66.         printf("the Number after swap:\n");
  67.         printf("+x' = %d\n", x);
  68.         printf("+y' = %d\n", y);
  69.     }
  70.     while (x != 0 && y != 0);
  71. }
  72.  
  73. //EX3
  74. void charCheck () {
  75.     char chars;
  76.     int nVowels = 0;
  77.     int nConsonants = 0;
  78.     int nOthers = 0;
  79.    
  80.     printf("Enter the string: ");
  81.    
  82.     do {
  83.         chars = getchar();
  84.         chars = toupper(chars);
  85.         if (chars >='A' && chars <= 'Z') {
  86.             switch (chars) {
  87.                 case 'A':
  88.                 case 'E':
  89.                 case 'I':
  90.                 case 'O':
  91.                 case 'U':
  92.                     nVowels++;
  93.                     break;
  94.                 default:
  95.                     nConsonants++;
  96.             }  
  97.         } else {
  98.             if (chars != 10) {
  99.                 nOthers++;
  100.             }              
  101.         }
  102.     }
  103.     while (chars != '\n');
  104.    
  105.     printf("Number of vowels: \t%d\n", nVowels);
  106.     printf("Number of consonants: \t%d\n", nConsonants);
  107.     printf("Number of others: \t%d\n", nOthers);
  108. }
  109.  
  110. //EX4
  111. void asciiTable () {
  112.     int code;
  113.     for (code = 0; code <= 255; code++)
  114.         printf("%c, %d, %o, %X\n", code, code, code, code);
  115.     if (code !=0 && code %20==0) {
  116.         getchar();
  117.     }
  118. }
  119.  
  120. //EX5
  121. void asciiDiff () {
  122.     char firstChar;
  123.     char secondChar;
  124.     char temporaryChar;
  125.     char charLoop;
  126.     int difference;
  127.    
  128.     printf("firstChar:  ");
  129.     scanf("%c", &firstChar);
  130.     getchar();
  131.     printf("secondChar: ");
  132.     scanf("%c", &secondChar);
  133.    
  134.     if (firstChar > secondChar) {
  135.         temporaryChar = firstChar;
  136.         firstChar = secondChar;
  137.         secondChar = temporaryChar;
  138.     }
  139.     difference = secondChar - firstChar;
  140.     printf("difference: %d\n", difference);
  141.     for (charLoop = firstChar; charLoop <= secondChar; charLoop++) {
  142.         printf("%c : %d, %o, %X\n", charLoop, charLoop, charLoop, charLoop);
  143.     }
  144. }
  145.  
  146. //EX6
  147. int primeChecker (int n) {
  148.     //var
  149.     int m;
  150.     int i;
  151.     //square root of n
  152.     m = sqrt(n);
  153.    
  154.     if ( n < 2) {
  155.         //Condition 1 is not satisfied
  156.         return 0;
  157.     }
  158.     for (i = 2; i <= m; i++) {
  159.         if (n % i == 0) {
  160.             //n is divided by i
  161.             //n is not a prime
  162.             return 0;
  163.         }
  164.  
  165.     }
  166.     //n is a prime
  167.     return 1;
  168. }
  169.  
  170. //EX7
  171. int validDate (int day, int month, int year) {
  172.     //max day of months 1, 3, 5, 7, 8, 10, 12
  173.     int maxDays = 31;
  174.    
  175.     //Basic checking
  176.     if (day < 1 || day > 31 || month < 1 || month > 12) {
  177.         return 0;
  178.     }
  179.    
  180.     //update maxd of a month
  181.     if ( month == 4 || month == 6 || month == 9 || month == 11) {
  182.         maxDays = 30;
  183.         }
  184.         else if (month == 2) {
  185.             if (year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0)) {
  186.             maxDays = 29;
  187.             }
  188.             else {
  189.                 maxDays = 28;
  190.             }
  191.         }  
  192.        
  193.     return day <= maxDays;
  194. }
  195.  
  196. //EX8
  197. int getRelPos (double x, double y, double r) {
  198.     //d2= x2+ y2  
  199.     double d2 = x*x + y*y;
  200.     //r2
  201.     double r2 = r*r;
  202.     if (d2 < r2){
  203.         return 1;
  204.     }
  205.     else if (d2 == r2) {
  206.         return 0;
  207.     }
  208.     else {
  209.         return -1;
  210.     }
  211. }
  212.  
  213. //EX9
  214. double factorial (int n) {
  215.     double p;
  216.     int i;
  217.     p = 1;
  218.    
  219.     for (i = 2; i <= n; i++) {
  220.         p *= i;
  221.     }
  222.    
  223.     return p;
  224. }
  225.  
  226. //EX10
  227. double fibo (int n) {
  228.     int t1 = 1;
  229.     int t2 = 1;
  230.     int f = 1;
  231.     int i ;
  232.    
  233.     for (i = 3; i <= n; i++) {
  234.       f = t1 + t2;
  235.       t1= t2;
  236.       t2=f;
  237.     }
  238.    return f;
  239. }
  240.  
  241. // EX11
  242. int accept () {
  243.     int n;
  244.     do {
  245.         printf("Type n: ");
  246.         scanf("%d", &n);
  247.     }
  248.     while (n < 1);
  249.     return n;
  250. }
  251.  
  252. int isFibonacci (int n) {
  253.     int t1=1;
  254.     int t2=1;
  255.     int f=1;
  256.     if (n==1) {
  257.         return 1;           /* n belongs to the Fibonacci sequence*/
  258.     }
  259.     while (f<n) {           /* Find out the Fibo number f to n */
  260.         f= t1 + t2;
  261.         t1=t2;
  262.         t2=f;
  263.     }  
  264.     return n == f;          /* if n==f ' n is Fibo element ' return 1 */
  265. }
  266.  
  267. //EX12
  268. int sumDigits (int n) {
  269.     int sum;
  270.     sum = 0;                        /* initialize sum of digits */
  271.     do {
  272.         int remainder = n % 10 ;    /* Get a digit at unit position */
  273.         n = n/10;
  274.         sum += remainder;
  275.     }
  276.     while (n>0);
  277.    
  278.     return sum;
  279. }
  280.  
  281. //EX13
  282. double makeDouble (int ipart, int fraction) {
  283.     double d_f;
  284.     d_f = fraction;
  285.     while (d_f >=1) {
  286.         d_f = d_f/10;               //create the fraction <1
  287.     }
  288.     if (ipart<0) {
  289.         return ipart - d_f;         //case-51 - 0.139
  290.     }
  291.     return ipart + d_f ;            //case 32 + 0.25
  292. }
  293.  
  294. //EX14
  295. int gcd(int a, int b) {
  296.     while ( a != b )
  297.     if (a > b) {
  298.         a -=b;
  299.     }
  300.     else {
  301.         b -= a;
  302.     }
  303.     return a;
  304. }
  305.  
  306. int lcm (int a, int b) {
  307.     return a*b/ gcd(a,b);
  308. }
  309.  
  310. //EX15
  311. void printMinMaxDigits(int n) {
  312.     int digit;                  /* Variable for extracting 1 digit */
  313.     int min, max;               /* Result variables */
  314.     digit = n % 10;             /* get the first rightmost digit: 3 */
  315.     n = n / 10;                 /* 1029, the remainder needs to proceed after*/
  316.     min = max = digit;          /* initialize results */
  317.     while (n>0) {
  318.         digit = n % 10;         /* Get the next digit */
  319.         n=n/10;
  320.         if (min > digit) {
  321.             min = digit;        /* update results */
  322.         }
  323.         if (max < digit) {
  324.             max = digit;
  325.         }
  326.    }
  327.    printf("\nMin = %d", min);
  328.    printf("\nMax = %d", max);
  329. }
  330.  
  331. //EX16
  332. void menuEX16 () {
  333.     printf("----Select a function----");
  334.     printf("\n1. Process Prime");
  335.     printf("\n2. Print min, max digit in an integer");
  336.     printf("\nother. Exit");
  337. }
  338.  
  339. void operation1 () {
  340.     int x;
  341.     bool boolean = true;
  342.    
  343.     printf("insert x: ");
  344.     scanf("%d", &x);
  345.    
  346.     for (int i = 2; i <= round (sqrt(x)); i++) {
  347.         if (x % i ==0) {
  348.             boolean = false;
  349.             break;
  350.         }
  351.     }
  352.     if (boolean) {
  353.         printf ("X is a prime\n");
  354.     } else {
  355.         printf ("X is not a prime\n");
  356.     }
  357. }
  358.  
  359. void operation2 () {
  360.     int x;
  361.     int max = 0;
  362.     int remainder;
  363.     printf("insert x: ");
  364.     scanf("%d", &x);
  365.    
  366.     do {
  367.         remainder = x % 10;
  368.         if (remainder > max) {
  369.             max = remainder;
  370.         }
  371.         x /= 10;
  372.     } while (x > 0);
  373.     printf("%d is the biggest digit of x\n", max);
  374. }
  375. int main () {
  376.     _flushall();
  377.     int i;
  378.     int n;
  379.     int day = 0;
  380.     int month = 0;
  381.     int year = 0;
  382.     double x;
  383.     double y;
  384.     double r;
  385.     int result;
  386.     double sumOfFactor;
  387.     int num;
  388.     double totalOfFibo;
  389.     int sum;
  390.     int a;
  391.     int b;
  392.     int d;
  393.     int m;
  394.     int ipart;
  395.     int fraction;
  396.     double realValue;
  397.     int ex16chose;
  398.    
  399.     int intMenu;
  400.    
  401.     do {
  402.         intMenu = menu();
  403.         switch (intMenu) {
  404.             case 1:
  405.                 system("cls");
  406.                 sumOfInt();
  407.                 system("pause");
  408.                 break;
  409.             case 2:
  410.                 system("cls");
  411.                 intSwap();
  412.                 system("pause");
  413.                 break;
  414.             case 3:
  415.                 system("cls");
  416.                 charCheck();
  417.                 system("pause");
  418.                 break;
  419.             case 4:
  420.                 system("cls");
  421.                 asciiTable();
  422.                 system("pause");
  423.                 break;
  424.             case 5:
  425.                 system("cls");
  426.                 asciiDiff();
  427.                 system("pause");
  428.                 break;
  429.             case 6:
  430.                 system("cls");
  431.                 n = 0;
  432.                 do {
  433.                 printf("Enter max range: ");
  434.                 scanf("%d",&n);
  435.                 }
  436.                 while (n < 2);
  437.            
  438.                 for(i = 2; i <= n; i++) {
  439.                     if (primeChecker(i) == 1) {
  440.                         printf("%d, ", i);
  441.                     }
  442.                 }
  443.                
  444.                 printf("\n");
  445.        
  446.                 system("pause");
  447.                 break;
  448.             case 7:
  449.                 system("cls");
  450.                 printf("please input the full date (format dd/mm/yyyy): ");
  451.                 scanf("%d/%d/%d", &day, &month, &year);
  452.                
  453.                 if(validDate(day,month,year)){
  454.                     printf("Valid\n");
  455.                     }
  456.                     else {
  457.                         printf("Invalid\n");
  458.                     }
  459.            
  460.                 system("pause");
  461.                 break;
  462.             case 8:
  463.                 system("cls");
  464.                 printf("x: ");
  465.                 scanf("%lf", &x);
  466.                 printf("y: ");
  467.                 scanf("%lf", &y);
  468.                
  469.                 do {
  470.                     printf("r: ");
  471.                     scanf("%lf", &r);
  472.                 }
  473.                 while (r < 0);
  474.                 result = getRelPos(x,y,r);
  475.                 if (result == 1) {
  476.                     printf("The point is in the circle\n");
  477.                     }
  478.                     else if (result==0) {
  479.                         printf("The point is on the circle\n");
  480.                         }
  481.                         else {
  482.                             printf("The point is out of the circle\n");
  483.                             }
  484.                
  485.                 system("pause");
  486.                 break;
  487.             case 9:
  488.                 system("cls");
  489.                 do {
  490.                     printf("pls input your num to compute facotrial: ");
  491.                     scanf("%d", &num);
  492.                 }
  493.                 while (num < 0);
  494.                
  495.                 sumOfFactor = factorial(num);
  496.                
  497.                 printf("Total: %0.2lf\n", sumOfFactor);
  498.                
  499.                 system("pause");
  500.                 break;
  501.             case 10:
  502.                 system("cls");
  503.                 do {
  504.                     printf("type your number to compute n^th value by using Fibonacci sequence: ");
  505.                     scanf("%d", &n);
  506.                 }
  507.                 while (n < 1);
  508.                
  509.                 totalOfFibo = fibo(n);
  510.                
  511.                 printf("total = %0.2lf\n", totalOfFibo);
  512.                
  513.                 system("pause");
  514.                 break;
  515.             case 11:
  516.                 system("cls");
  517.                 n = accept();
  518.                
  519.                 if(isFibonacci(n) == 1){
  520.                     printf("It is a Fibonacci element.\n");
  521.                     } else {
  522.                         printf("It is not a Fibonacci element.\n");
  523.                     }
  524.            
  525.                 system("pause");
  526.                 break;
  527.             case 12:
  528.                 system("cls");
  529.                 do {
  530.                     printf("Type n: ");
  531.                     scanf("%d", &n);
  532.                     if(n > 0) {
  533.                         sum = sumDigits(n);
  534.                         printf("SUM = %d\n", sum);
  535.                     }
  536.                 }
  537.                 while (n >= 0);
  538.                
  539.                 system("pause");
  540.                 break;
  541.             case 13:
  542.                 system("cls");
  543.                 printf("accept Integral part: ");
  544.                 scanf("%d", &ipart);
  545.                 do {
  546.                     printf("accept Fraction: ");
  547.                     scanf("%d", &fraction);
  548.                 }
  549.                 while (fraction < 0);
  550.                
  551.                 realValue = makeDouble(ipart, fraction);
  552.                
  553.                 printf("Real numer is: %0.4lf", realValue);
  554.                
  555.                 printf("\n");
  556.            
  557.                 system("pause");
  558.                 break;
  559.             case 14:
  560.                 system("cls");
  561.                 do {
  562.                     printf("Enter a: ");
  563.                     scanf("%d",&a);
  564.                     printf("Enter b: ");
  565.                     scanf("%d",&b);    
  566.                 }
  567.                 while (a <= 0 || b <= 0);
  568.                    
  569.                 d = gcd(a,b);
  570.                 m = lcm (a,b);
  571.                
  572.                 printf("d = %d\n", d);
  573.                 printf("m = %d\n", m);
  574.            
  575.                 system("pause");
  576.                 break;
  577.             case 15:
  578.                 system("cls");
  579.                 do {
  580.                     printf("Enter n: ");
  581.                     scanf("%d",&n);
  582.                     printMinMaxDigits(n);
  583.                 }
  584.                 while (n < 0);
  585.                 system("pause");
  586.                 break;
  587.             case 16:
  588.                 system("cls");
  589.                 menuEX16();
  590.                 scanf("%d", &ex16chose);
  591.                 switch (ex16chose) {
  592.                     case 1:
  593.                         operation1();
  594.                         break;
  595.                     case 2:
  596.                         operation2();
  597.                         break;
  598.                     default:
  599.                         printf("exiting");
  600.                         break;
  601.                 }
  602.                 system("pause");
  603.                 break;
  604.             default:
  605.                 printf("Good Bye\n");
  606.                 system("pause");
  607.                 _flushall();
  608.         }  
  609.     system("cls");
  610.     } while (intMenu > 0 && intMenu < 17);
  611.    
  612.     return 0;    
  613. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement