Showing posts with label Command Line. Show all posts
Showing posts with label Command Line. Show all posts

Check number is Odd or Even using command line

Write a program to check number is even or odd using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argcchar *argv[])
{
    if (argc != 2)
    {
        printf("Enter a number: ");
        return -1;
    }
    else
    {
        int num = atoi(argv[1]);
        if (num % 2 == 0)
        {
            printf("Number is even ");
        }
        else
        {
            printf("Number is odd ");
        }
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  24

Example:

Input: num = 24
Output: Number is even 


Check Armstrong number using command line

Write a program to check a given  number is Armstrong using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argcchar *argv[])
{
    if (argc != 2)
    {
        printf("Enter a number: ");
        return -1;
    }
    else
    {
        int num = atoi(argv[1]);
        int sum = 0;
        int n = num;
        while (n > 0)
        {
            int temp = n % 10;
            sum += pow(temp3);
            n = n / 10;
        }
        if (sum == num)
        {
            printf("Number is armstrong ");
        }
        else
        {
            printf("Number is not armstrong ");
        }
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  153

Example:

Input: num = 153
Output: Number is armstrong 


Sum of Digits of a number using command line

Write a program to find the sum of digits of a number using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    if (argc != 2)
    {
        printf("Enter a number ");
        return -1;
    }
    else
    {
        int num = atoi(argv[1]);
        int sum = 0;
        while (num > 0)
        {
            int temp = num % 10;
            sum += temp;
            num = num / 10;
        }
        printf("Sum of digits of number is %d"sum);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  45321

Example:

Input: num = 45321
Output: Sum of digits of number is 15


Swap two numbers using command line

Write a program to swap two numbers using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    if (argc != 3)
    {
        printf("Enter two numbers ");
        return -1;
    }
    else
    {
        int firstNum = atoi(argv[1]);
        int secondNum = atoi(argv[2]);
        printf("Numbers before swap:\n");
        printf("First = %d, Second = %d\n"firstNumsecondNum);
        int temp = firstNum;
        firstNum = secondNum;
        secondNum = temp;
        printf("Numbers after swap:\n");
        printf("First = %d, Second = %d"firstNumsecondNum);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  5 4

Example:

Input: firstNum=5, secondNum=4
Output: Numbers before swap:
First = 5, Second = 4
Numbers after swap:
First = 4, Second = 5


Find Average of two numbers using command line

Write a program to find the average of two numbers using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    if (argc != 3)
    {
        printf("Enter two numbers ");
        return -1;
    }
    else
    {
        int firstNum = atoi(argv[1]);
        int secondNum = atoi(argv[2]);

        float average = (float)(firstNum + secondNum) / 2;
        printf("Average is %f"average);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  4 5

Example:

Input: firstNum=4, secondNum=5
Output: Average is 4.500000 


Smallest of two numbers using command line

Write a program to find the smallest of two numbers using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    if (argc != 3)
    {
        printf("Enter two numbers ");
        return -1;
    }
    else
    {
        int firstNum = atoi(argv[1]);
        int secondNum = atoi(argv[2]);

        if (firstNum == secondNum)
            printf("Both are equal");
        else if (firstNum < secondNum)
            printf("Smallest is %d"firstNum);
        else if (secondNum < firstNum)
            printf("Smallest is %d"secondNum);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  5 4

Example:

Input: firstNum=5, secondNum=4
Output:Smallest is 4


Greatest of two numbers using Command Line

 Write a program to find the greatest of two numbers using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    if (argc != 3)
    {
        printf("Enter two numbers ");
        return -1;
    }
    else
    {
        int firstNum = atoi(argv[1]);
        int secondNum = atoi(argv[2]);

        if (firstNum == secondNum)
            printf("Both are equal");
        else if (firstNum > secondNum)
            printf("Greatest is %d"firstNum);
        else if (secondNum > firstNum)
            printf("Greatest is %d"secondNum);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  4 5

Example:

Input: firstNum=4, secondNum=5
Output: Greatest is 5 


Print an integer using command line

Write a program to print a number using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    if (argc != 2)
    {
        printf("Enter a number ");
        return -1;
    }
    else
    {
        int num = atoi(argv[1]);
        printf("Number is %d"num);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  5

Example:

Input: num = 5
Output:Number is 5 


Sum of two number using command line

Write a program to find the sum of two numbers using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments

Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    int firstNumsecondNum;
    int factorial = 1;
    
    if (argc!= 3)
    {
        printf("Please enter two numbers ");
        return -1;
    }
    else
    {
        firstNum = atoi(argv[1]);
        secondNum = atoi(argv[2]);
        int sum = firstNum + secondNum;
        printf("Sum is %d"sum);
    }
    return 0;
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  2 4

Example:

Input: firstNum=2, secondNum=4
Output:Sum is 6 


Factorial of given number using command line argument

Write a program to find the factorial of a given number using command line argument

//definition of main in command-line arguments

int main(int argc, char *argv[])

{

  //some coding

  1. argc – It is known as argument count, it stores the count of the number of arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments


Approach

C

#include <stdio.h>
#include <stdlib.h>
int main(int argcchar *argv[])
{
    int ni;
    int factorial = 1;
    n = atoi(argv[1]);
    for (i = 1i <= n; ++i)
    {
        factorial = factorial * i;
    }
    printf("Factorial is %d"factorial);
}
To Run a Program
  1. Go to the location/directory where your program is present.
  2. g++ proramName.c -o out
  3. ./out  5

Example:

Input:  5
Output: Factorial is 120