fahimkamal63

Exercise: 7

Mar 1st, 2021
405
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×