Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Exercise: 7
- 01.03.2021
- */
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int main (int argc , char * argv []) {
- long n, i;
- long result = 0;
- char *a = argv [1];
- char str[30];
- int choice;
- if ( argc == 1) {
- while(1){
- printf("Enter 1 to use Brute Force.\n");
- printf("Enter 2 to use Gauss Adder.\n");
- printf("Enter 3 to exit.\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- if (choice == 1){
- result = 0;
- printf ( "Please enter Value of n: " );
- scanf ( "%s", str ) ;
- n = strtoul(str, NULL, 10);
- for (i = 1; i <= n; i++){
- result += i;
- }
- printf("Using a brute force adder, the answer is: %ld.\n", result);
- }
- else if(choice == 2){
- result = 0;
- printf ( "Please enter Value of n: " );
- scanf ( "%s", str ) ;
- n = strtoul(str, NULL, 10);
- result = (n * (n+1))/2;
- printf("Using a gauss adder, the answer is: %ld.\n", result);
- }
- else if (choice == 3){
- break;
- }
- else{
- printf("Enter valid choice.\n");
- }
- }
- }
- else {
- n = strtoul(a, NULL, 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement