C Program for a Command Line Calculator


 

// Program by Akash Tripathi (@proakash256) 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc , char *argv)
{
    char *operation;
    strcpy(operation , argv[1]);
    int n1 = atoi(argv[2]); // atoi() is a function in stdlib.h that converts
    int n2 = atoi(argv[3]); // a String to an integter.
    // printf("Operation is %s.\n" , operation);
    // printf("First Number is %d.\n" , n1);
    // printf("Second Number is %d.\n" , n2);
    // printf("Second Number is %d.\n" , n2);
    if(strcmp(operation , "add") == 0)
        printf("\nThe Addition of %d and %d is %d\n\n" , n1 , n2 , (n1 + n2));
    else if(strcmp(operation , "subtract") == 0)
        printf("\nThe Subtraction of %d and %d is %d\n\n" , n1 , n2 , (n1 - n2));
    else if(strcmp(operation , "multiply") == 0)
        printf("\nThe Multiplication of %d and %d is %d\n\n" , n1 , n2 , (n1 * n2));
    else if(strcmp(operation , "divide") == 0)
        printf("\nThe Division of %d and %d is %d\n\n" , n1 , n2 , (n1 / n2));
    return 0;
}

Comments