Back | Reverse | Quick Reply | Post Reply |

onegai..
Link | by anime on 2010-10-15 12:12:43
Can you guys help me think of making a TURBO C program using CASE statement, IF Statement, If Else Statement, For loop Statement and While Loop Statement.

(calculating numbers like 1+1=2 is not allowed coz it's common)

so I'm having a hard time thinking what kind of program should I make..suggestions pls.. :((

sig avi

Re: onegai..
Link | by gendou on 2010-10-15 13:05:14 (edited 2010-10-15 13:08:32)
You're welcome.
#include 
#include 

int main(int argc, char* argv[])
{
    switch(argc)
    {
        case 1:
        {
            printf("Why don't you say hello?\n");
            break;
        }
        case 2:
        {
            if(!strcmp(argv[1], "hello"))
            {
                printf("Hello to you, too!\n\n");
                printf("Commands: fib [number], prime [number].\n");
            }
            else
            {
                printf("First, please say hello!\n");
            }
            break;
        }
        case 3:
        {
            if(!strcmp(argv[1], "fib"))
            {
                int n = atoi(argv[2]);
                if(n < 0) { printf("You must enter a non-negative integer, child!\n"); break; }
                if(n > 90) { printf("Sorry, I can only compute up to number 90...\n"); break; }
                long long fib[] = { 0, 1 };
                int i;
                for(i = 0; i < n; i++)
                {
                    fib[i % 2] = fib[0] + fib[1];
                }
                printf("Fibonacci number %d = %llu\n", n, fib[i % 2]);
            }
            else if(!strcmp(argv[1], "prime"))
            {
                bool prime = true;
                int n = atoi(argv[2]);
                if(n < 1) { printf("You must enter a positive integer, child!\n"); break; }
                int i = 2;
                while(i < n)
                {
                    if(n % i == 0) { prime = false; break; }
                    i++;
                }
                if(prime)
                {
                    printf("%d is prime!\n", n);
                }
                else
                {
                    printf("%d is not prime.\n", n);
                }
            }
            else
            {
                printf("Your mom can %s %s!\n\n", argv[1], argv[2]);
                printf("Commands: fib [number], prime [number].\n");
            }
            break;
        }
    }
}


by the way, try typing "a.out eat poop" lol!


Re: onegai..
Link | by anime on 2010-10-15 20:45:23
thank you gendou-sama, I'll try and see this code :))

sig avi

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0035 seconds at 2024-12-30 11:43:50