C Programming Using Command Line Arguments

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

C Programs using command line arguments

1. Area of a Triangle

#include<stdio.h>
#include<stdlib.h>

int main(int x, char *y[])


{
int breadth = atoi(y[1]);
int height = atoi(y[2]);
float area;
area = (breadth*height)/2;
printf("%.2f",area);
}

2. Binary to Decimal

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int a, char *b[])
{
long long n = atoi(b[1]);

int decimalNumber = 0, i = 0, remainder;


while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
printf("%lld",decimalNumber);
return 0;
}
3. Factorial of a given number

#include<stdio.h>
#include<stdlib.h>

int main(int a, char *b[])


{
int i,x,f=1;
x = atoi(b[1]);
for(i=1; i<=x; i++)
{
f=f*i;
}
printf("%d",f);
}

4. Greatest among 10 numbers

#include<stdio.h>
#include<stdlib.h>

int main(int a, char *b[])


{
int i;
int greatest = atoi(b[1]);
for(i=1; i<=10; i++)
if(atoi(b[i]) > greatest)
{
greatest = atoi(b[i]);
}
printf("The greatest number is %d",greatest);
}
5. Hypotenuse of a Right triangle

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(int a, char *b[])


{
int side1 = atoi(b[1]);
int side2 = atoi(b[2]);
float hyp;
hyp = sqrt(pow(side1,2) + pow(side2,2));
printf("%.2f",hyp);

6. Odd or Even

#include<stdio.h>
#include<stdlib.h>

int main(int a, char *b[])


{
int num = atoi(b[1]);
if(num%2 == 0)
{
printf("%d is an even number",num);
}
else
{
printf("%d is an odd number",num);
}
}
7. Palindrome

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(int a, char *b[])


{
int l = 0;
int h = strlen(b[1]) - 1;

while (h>1)
{
if(b[1][l++] != b[1][h--])
{
printf("%s is not a palindrome", b[1]);
return 0;
}
}
{

printf("%s is a palindrome", b[1]);


}
}
8. Sum of prime numbers in a given range

#include<stdio.h>
#include<stdlib.h>
int main(int a, char *b[])
{

int num,i,count,min,max,sum=0;
min = atoi(b[1]);
max = atoi(b[2]);

for(num = min; num<=max; num++)


{

count = 0;

for(i=2; i<=num/2; i++)


{
if(num%i==0)
{
count++;
break;
}
}

if(count==0 && num!= 1)


sum = sum + num;
}

printf("Sum of prime numbers is: %d ",sum);

return 0;
}

You might also like