Program should read the units consumed for a customer and calculate the total bill.

#include<stdio.h>
#include<conio.h>

void main()
{
int units;
float bill=0;
clrscr();

printf("Enter the number of units consumed\n");
scanf("%d",&units); //input the no. of units consumed

if(units>=0 && units<=200) //units in the range of 0-200
  bill=bill+0.50*units;
else
  if(units>=201 && units<=400) //units in the range of 201-400
    bill=bill+100+0.65*(units-200);
  else
    if(units>=401 && units<=600) //units in the range of 401-600
      bill=bill+230+0.85*(units-400);
    else        //units above 600
      bill=bill+390+(units-600);

printf("Total bill=%.2f\n",bill); //print the user's total bill
getch();
}

 

Write a program that accepts options from user. The options can be ‘+’ or ‘-‘ or ‘*’ or ‘/’. The program should also accept two integers and print the appropriate result depending on the option.

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b;
char option;
clrscr();
printf("Enter options out of + - * or / :");
scanf("%c",&option);			//take option from user to perform which operation
printf("Enter two integers : ");
scanf("%d %d",&a,&b);			/*input 2 integers on which add,subtract,multiply or divide operation is to be performed*/

switch(option)    //put a switch case on option for menu driven programming
{
case '+': printf("%d\n",a+b);
    break;
case '-': printf("%d\n",a-b);
    break;
case '*': printf("%d\n",a*b);
    break;
case '/': if(b!=0)
    printf("%f\n",(float)(a)/b);
    else 
          printf("Divide by zero error\n");
    break;
default : printf("Incorrect data\n");
    break;
}
getch();
}

 

Write a program that accepts one alphabet and prints the color name starting with that alphabet.

#include<stdio.h>
#include<conio.h>

int main()
{
char x;
clrscr();
printf("Enter one alphabet\n");
scanf("%c",&x);

switch(x)
{
  case 'r':
  case 'R':printf("Red\n"); break;  //if 'r' or 'R' then print Red
  case 'b':
  case 'B':printf("Blue, Black or Brown\n"); break;
  /*if 'b' or 'B' then print Blue,Black or Brown */
  case 'g':
  case 'G':printf("Green\n");break; //if 'g' or 'G' then print Green
  case 'o':
  case 'O':printf("Orange\n");break;//if 'o' or 'O' then print Orange
  case 'y':
  case 'Y':printf("Yellow\n"); break; //if 'y' or 'Y' then print Yellow
  default:printf("Unknown colour\n"); break; //else print unknown colour
}
getch();
return 0;
}

 

Write a program that accepts three numbers that represent the coefficients of a quadratic equation and print the roots of the quadratic equation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
float a,b,c,d;
clrscr();
printf("Enter coefficents of quadratic equation"); //given quadratic eqn is ax^2+bx+c
scanf("%f %f %f",&a,&b,&c);
if(a==0)
{
if(b==0)
{
printf("Incorrect data\n"); //if a and b are both 0 then it is not an equation
}
else
{
printf("Root of linear equation :- %f\n",-c/b); //if a=0 then eqn bx+c is a linear eqn
}
}
else    //use mathematical formulae for finding roots of quadratic eqn 
{
d=b*b - 4*a*c; 
if(d==0)   //both roots are equal
printf("Root :- %f\n",-b/(2*a));
else
if(d>0) // different and real roots
{
printf("Root1 :- %f\n",(-b+sqrt(d))/(2*a));
printf("Root2 :- %f\n",(-b-sqrt(d))/(2*a));
}
else   //different and imaginary roots
{
printf("Root1 :- %f + i(%f)\n",-b/(2*a),sqrt(-d)/(2*a));
printf("Root2 :- %f - i(%f)\n",-b/(2*a),sqrt(-d)/(2*a));
}
}
getch();
return 0;
}

 

Write a program that accepts principal amount, period, age of depositor, gender of depositor and calculates the final amount payable.

#include<stdio.h>
#include<conio.h>

void main()
{
int n,age;     //n-period, p-principal amount
float p,amt=0,rate;
char gen;
clrscr();

printf("Enter principal amount\n");
scanf("%f",&p);  //input principal

printf("Enter period\n");
scanf("%d",&n); //input period

printf("Enter age\n");
scanf("%d",&age);//input depositor's age

printf("Enter gender: m for male and f for female\n");
scanf(" %c",&gen);  //enter depositor's gender

/* select appropriate rate of interest depending on the values of
gender and age */

if(gen=='m' || gen=='M')
{
  if(age>=25 && age<=40)
    rate=9.05;
  else
    if(age>=41 && age<=59)
      rate=9.25;
    else
      if(age>=60)
  rate=9.50;
}

else if(gen=='f' || gen=='F')
{
  if(age>=25 && age<=40)
    rate=9.15;
  else
    if(age>=41 && age<=59)
      rate=9.35;
    else
      if(age>=60)
  rate=9.60;

}
/*calculate simple interest and add to principal to get the amount payable */

amt=p+((p*n*rate)/100);
printf("The final amount payable is %f\n",amt);

getch();
}



 

Write a program that accepts three integers and finds out the highest out of them.

#include<stdio.h>
#include<conio.h>

int main()
{
int x,y,z;
clrscr();
printf("Enter three integers=");
scanf("%d%d%d",&x,&y,&z);

 if(x>y)  //if x is greater than y then either x or z can be the greatest number
    if(x>z)   //x is greater than y and greater than z therefore x is greatest
       printf("Highest number=%dn",x);
    else    //x is greater than y and less than z therefore z is greatest
       printf("Highest number=%dn",z);
 else  //y is greater than x then either y or z can be the greatest number
    if(y>z)  //y is greater than x and y is greater than z therefore y is greatest
       printf("Highest number=%dn",y);
    else    //y is greater than x and less than z therefore z is greatest
       printf("Highest number=%dn",z);

getch();
return 0;
}

 

Write a program that accepts marks(M) of a subject out of 100 and prints the class of student: If M>=70 then class is Distinction; If M>=60 and M=50 and M=40 and M<50 then class is Third class; If M<40 then class is Fail.

#include<stdio.h>
#include<conio.h>

int main()
{
int m;
clrscr();
printf("Enter marks out of 100n");
scanf("%d",&m);
if(m<0||m>100) //check if the marks are valid
printf("Invalid marksn");
else
  if(m>=70)  //criteria for distinction
  printf("Distinctionn");
  else
     if(m>=60)    //criteria for first class
     printf("First classn");
     else
  if(m>=50)  //criteria for second class
  printf("Second classn");
  else
     if(m>=40) //criteria for third class
     printf("Third classn");
     else
        printf("Failn");

getch();
return 0;
}

 

Write a program that accepts a three digit positive integer and prints whether it is Armstrong number or not.

#include<stdio.h>
#include<conio.h>

void main()
{
int n,a,b,c,d;
clrscr();

printf("Enter one positive integer :-");  //the number to be checked
scanf("%d",&n);

if(n>=100&&n<=999) //to input only three digit numbers
{
 a=n/100;
 d=n%100;
 b=d/10;
 c=d%10;

 if(a*a*a + b*b*b + c*c*c == n)  //condition for a number to be armstrong number
   printf("%d is an armstrong numbern",n);

 else
   printf("%d is not an armstrong numbern",n);
}
 else
 printf("Number incorrectn");
 getch();
}

 

Write a program that accepts a three digit positive integer and prints the sum of digits, product of digits and reverse of the number.

#include<stdio.h>
#include<conio.h>

int main()
{
int n,a,b,c,d;
clrscr();
printf("Enter a 3-digit positive integern");
scanf("%d",&n);
if(n<100 || n>999) //to check if the number is a 3-digit number
  printf("Invalid numbern");
else
{
  a=n/100; //to obtain the hundreds place digit
  d=n%100; //to obtain remaining 2-digit number
  b=d/10;  //to obtain tens place digit
  c=d%10;  //to obtain units place digit

  printf("Sum of digits = %dn",a+b+c);
  printf("Product of digits = %dn",a*b*c);
  printf("Reverse number = %03dn",100*c+10*b+a);
}
getch();
return 0;
}

 

Write a program that accepts a positive integer and prints whether it ends with 7 or not.

#include<stdio.h>
#include<conio.h>

int main()
{
int n;
clrscr();
printf("Enter a positive integern");
scanf("%d",&n);   //input the number

if(n<=0)   //to check if the number is positive
printf("Invalid numbern");
else
{
  if(n%10==7) //the remainder obtained on dividing the number by 10 is the units digit
    printf("%d ends with 7n",n);
  else
    printf("%d does not end with 7n",n);
}
getch();
return 0;
}