Advertisement
fahimkamal63

Exercise: 7

Mar 1st, 2021
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. /*
  2. Exercise: 7
  3. 01.03.2021
  4. */
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8.  
  9. int main (int argc , char * argv []) {
  10.     long n, i;
  11.     long result = 0;
  12.     char *a = argv [1];
  13.     char str[30];
  14.     int choice;
  15.  
  16.     if ( argc == 1) {
  17.         while(1){
  18.             printf("Enter 1 to use Brute Force.\n");
  19.             printf("Enter 2 to use Gauss Adder.\n");
  20.             printf("Enter 3 to exit.\n");
  21.             printf("Enter your choice: ");
  22.             scanf("%d", &choice);
  23.             if (choice == 1){
  24.                 result = 0;
  25.                 printf ( "Please enter Value of n: " );
  26.                 scanf ( "%s", str ) ;
  27.                 n = strtoul(str, NULL, 10);
  28.                 for (i = 1; i <= n; i++){
  29.                     result += i;
  30.                 }
  31.                 printf("Using a brute force adder, the answer is: %ld.\n", result);
  32.             }
  33.             else if(choice == 2){
  34.                 result = 0;
  35.                 printf ( "Please enter Value of n: " );
  36.                 scanf ( "%s", str ) ;
  37.                 n = strtoul(str, NULL, 10);
  38.                 result = (n * (n+1))/2;
  39.                 printf("Using a gauss adder, the answer is: %ld.\n", result);
  40.             }
  41.             else if (choice == 3){
  42.                 break;
  43.             }
  44.             else{
  45.                 printf("Enter valid choice.\n");
  46.             }
  47.         }
  48.     }
  49.     else {
  50.         n = strtoul(a, NULL, 10);
  51.     }
  52.  
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement