Monday, 17 August 2015

Program to find roots of quadratic equation using switch statement

/* Program to find the roots of a quadratic equtaion using switch statement */
#include<stdio.h>
#include<math.h>
main()
{
    int flag;
    float x, x1, x2;
    float a, b, c, d;
    float rpart, ipart;
    clrscr();
    printf("\n Enter 3 numbers: ");
    scanf("%f %f %f", &a, &b, &c);
    if(a==0)
   {
      x=-b/c;
      printf("\n Only root x : %7.3f", x);
      exit();
   }
   d=b*b-4*a*c;
   if(d>0)
       flag=1;
   else if(d==0)
      flag=2;
   else
      flag=3;

   switch(flag)
   {
      case 1:
          printf("\n Real & Distinct roots are: ");
          x1=(-b+sqrt(d))/(2*a);
          x2=(-b-sqrt(d))/(2*a);
          printf("\n x1=%7.3f \n x2=%7.3f", x1, x2);
          break;
      case 2:
          printf("\n Repeated roots are: ");
          x1=-b/(2*a);
          x2=x1;
          printf("\n x1 & x1 : %7.3f", x1);
          break;
      case 3:
          d=sqrt(abs(d));
          rpart=-b/(2*a);
          ipart=d/(2*a);
          printf("\n Complex Roots are: ");
          printf("\n x1=%7.3f+i%7.3f", rpart, ipart);
          printf("\n x2=%7.3f-i%7.3f", rpart, ipart);
    }
    getch();
    return;
}

To perform Arithmetic Operations Using Switch case

# include <stdio.h>
int main() {
    char o;
    float num1,num2;
    printf("Select an operator either + or - or * or / \n");
    scanf("%c",&o);
    printf("Enter two operands: ");
    scanf("%f%f",&num1,&num2);
    switch(o) {
        case '+':
            printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
            break;
        case '-':
            printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
            break;
        case '*':
            printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
            break;
        case '/':
            printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            printf("Error! operator is not correct");
            break;
    }
    return 0;
}

To find fibonacci series

/* Fibonacci Series c language */
#include<stdio.h>
 
int main()
{
   int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms\n");
   scanf("%d",&n);
 
   printf("First %d terms of Fibonacci series are :-\n",n);
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
 
   return 0;
}

To find factorial of number

#include <stdio.h>
 
int main()
{
  int c, n, fact = 1;
 
  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
 
  for (c = 1; c <= n; c++)
    fact = fact * c;
 
  printf("Factorial of %d = %d\n", n, fact);
 
  return 0;
}

To find Sum of square program

/*c program for accept number and calculate sum of square*/
#include<stdio.h>
int main()
{
 int num,sum=0,i;
 printf("Enter any number : ");
 scanf("%d"&num);
 for(i=1; i<=num; i++)
    sum = sum + (i*i);
 printf("Sum of square of %d = %d",num,sum);
 return 0;
}

To find the sum and average of n elements given by the user

#include<stdio.h>
#include<conio.h>
int main (int argc, char *argv[])
{
  clrscr();
  int i=0,r=0,n=0;
  float s,avg;
  printf("\n Enter the range:");
  scanf("%d",&r);
10    for(i=0;i<r;i++)
11    {
12        printf("\n Enter element %d :",(i+1));
13        scanf("%d",&n);
14        s+=n;
15    }
16    avg=s/r;
17    printf("\n The sum and average of %d numbers are %f and %f",r,s,avg);
18    getch();
19  }

To Check Whether a Number is Positive or Negative

#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<=0)
    {
        if (num==0)
          printf("You entered zero.");
        else
          printf("%.2f is negative.",num);
    }
    else
      printf("%.2f is positive.",num);
    return 0;
}

This program also can be solved using nested if else statement.

/* C programming code to check whether a number is negative or positive or zero using nested if...else statement. */

#include <stdio.h>
int main()
{
    float num;
    printf("Enter a number: ");
    scanf("%f",&num);
    if (num<0)      /* Checking whether num is less than 0*/
      printf("%.2f is negative.",num);
    else if (num>0)   /* Checking whether num is greater than zero*/
      printf("%.2f is positive.",num);
    else
      printf("You entered zero.");
    return 0;
}