Write a program to find xaximum and minimum of two numbers without using any loop or condition.

int main () 
{
int a = 15, b = 20;
printf("max = %d\n", ((a + b) + abs(a - b)) / 2);
printf("min = %d", ((a + b) - abs(a - b)) / 2);
return 0;
}

 

Output:

 

 

max = 20

min = 15


Write a program to find sum of two numbers without using + operator.

#include 

int main()
{
int a = 5;
int b = 5;
int sum = -( -a-b );
printf("Sum of two numbers(%d,%d) is %d",a,b,sum);
return 0;
}

Output

Sum of two numbers(5,5) is 10


Write a program to find factorial of a number.

#include 

int main()
{
int num,i;
long int fact;

printf("Enter an integer number: ");
scanf("%d",&num);

/*product of numbers from num to 1*/
fact=1;
for(i=num; i>=1; i--)
fact=fact*i;

printf("\nFactorial of %d is = %ld",num,fact);

return 0;
}

Write a program to accept a number from user and check it it is Armstrong number or not.

#include

int main()
{
int temp,rem,sum=0,n;
printf("Enter any number : ");
scanf("%d", &n);
temp=n;
while(n > 0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(temp==sum)
printf("Armstrong Number");
else
printf("Not an Armstrong Number");
return 0;
}

What will be output of the following program?

#include 

int main()

{

    int a, b = 10;

    a = -b--;

    printf("a = %d, b = %d", a, b);

    return 0;

}

a = -10, b = 9

The statement ‘a = -b–;’ compiles fine. Unary minus and unary decrement have save precedence and right to left associativity. Therefore ‘-b–‘ is treated as -(b–) which is valid. So -10 will be assigned to ‘a’, and ‘b’ will become 9.
Try the following program as an exercise.


What will be the output of the program?

int main() 

{

  int x = 10;

  static int y = x;

  

  if(x == y)

     printf("Equal");

  else if(x > y)

     printf("Greater");

  else

     printf("Less");

  

  getchar();

  return 0;

}

Output: Compiler Error
In C, static variables can only be initialized using constant literals.


Back to Top