Showing posts with label Conditional. Show all posts
Showing posts with label Conditional. Show all posts

Find roots of quadratic equation (ax^2+bx+c=0)

Program:

#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,alpha,beta,delta,realpart,imaginarypart;
printf("Enter a,b and c: \n");
scanf("%f %f %f",&a,&b,&c);
delta=(b*b)-(4*a*c);
if(delta==0)
{
  printf("alpha=beta=%f",-b/(2*a));
}
else if(delta<0)
{
realpart=-b/(2*a);
imaginarypart=(sqrt(-delta))/(2*a);
printf("alpha=%f+%fi \n beta=%f-%fi\n",realpart,imaginarypart,realpart,imaginarypart);
}
else
{
alpha=(-b+sqrt(delta))/(2*a);
beta=(-b-sqrt(delta))/(2*a);
printf("alpha=%f \n beta=%f\n",alpha,beta);
}
return 0;

}

Output:





Simple calculator (using switch-case)

Program:

#include<stdio.h>
int main()
{
char op;
float n1,n2,res;
printf("Enter two numbers: \n");
scanf("%f %f",&n1,&n2);
printf("Enter Operation (+,-,*,/): ");
fflush(stdin);
scanf("%c",&op);
switch(op)
{
case '+': res=n1+n2;
  break;
case '-': res=n1-n2;
  break;
case '*': res=n1*n2;
  break;
case '/': res=n1/n2;
  break;
default : printf("Invalid");
}
printf("Result: %.2f",res);
return 0;

}

Output:




Simple calculator (using if...else if)

Program:

#include<stdio.h>
int main()
{
char op;
float n1,n2,res;
printf("Enter two numbers: \n");
scanf("%f %f",&n1,&n2);
printf("Enter Operation (+,-,*,/): ");
fflush(stdin);
scanf("%c",&op);
if(op=='+')
    res=n1+n2;
else if (op=='-')
    res=n1-n2;
else if (op=='*')
    res=n1*n2;
else if (op=='/')
    res=n1/n2;
else
{
printf("Invalid");
}

printf("Result: %.2f",res);
return 0;

}

Output:



Leap year

Program:

#include<stdio.h>
int main()
{
int y;
printf("Enter year: ");
scanf("%d",&y);
if((y%4==0 && y%100!=0) || (y%400==0))
{
printf("%d is a leap year",y);
}
else
{
printf("%d is not a leap year",y);
}
return 0;

}

Output:




Check vowel character

Program:

#include<stdio.h>
int main()
{
char ch;
printf("Enter one character: ");
scanf("%c",&ch);
if(((ch>='a') && (ch<='z')) || ((ch>='A') && (ch<='Z')))
{
switch(ch)
{
case 'a' : case 'A':
case 'e' : case 'E':
case 'i' : case 'I':
case 'o' : case 'O':
case 'u' : case 'U':
       printf("%c is vowel",ch);
       break;
default  :
   printf("%c is not vowel",ch);   
}
}
else
{
printf("%c is not alphabet",ch);
}
return 0;

}

Output:






Find maximum of three numbers

Program:

#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter three numbers: \n");
scanf("%d %d %d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("%d is maximum number",x);
}
else
{
    printf("%d is maximum number",z);
}
}
else
{
if(y>z)
{
printf("%d is maximum number",y);
}
else
{
    printf("%d is maximum number",z);
}
}
return 0;

}

Output:



Find maximum of two numbers

Program:

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two numbers: \n");
scanf("%d %d",&x,&y);
if(x>y)
{
printf("%d is maximum",x);
}
else
{
printf("%d is maximum",y);
}
return 0;

}

Output:



Even or odd number

Program:

#include<stdio.h>
int main()
{
int num;
printf("Enter number: ");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is even",num);
}
else
{
printf("%d is odd",num);
}
return 0;

}

Output:




x^y using recursion

Program: #include<stdio.h> int power(int a,int b); int main() { int x,y,ans; printf("Enter x and y:\n "); scanf(...