-->

Tuesday, October 6, 2015

if..else program collection

11) Write a program to get marks of any 5 subjects and to get total marks with percentage and division.
                                                                                                                          solution is here

12) Write a program to read annual salary of an employee and decide tax withheld as follos.
         salary                                          tax
            <=100000                                0%
           upto 150000                              15%
          above 150000                             25%
                                                                                                                        click to see solution

13)Write a program to input cost price and selling price and determine whether there is loss or profit
                                                                                                                       click to see solution  

14)Write a program to read age of person  and find out the category among following according to age.
           category                                         age
            child                                            0 to 12
            teenage                                         13 to 19
           adult life                                      20 to 30
           mature life                                   31 to 50
          old age                                         over 50
                                                                                                                            click to see solution

15) A company pays its employee on hourly basis.If an employee works for 8rs hours he gets  100/hr
, and 120/hr for additional hours.Write a program to read working hours of an employee and calculate total salary.
                                                                                                                            click to see solution


16)Write a program to display name of day on the basis of entered number
. for example, 2 for Monday.                                                                                click to see solution
                                                                                                                           
17) A company provides commission on the basis of total sales as follows.

         sales<10000-------------------------------->no commission
         sales >=10000 and <100000------------->5% commission
          sales>100000                  ---------------->10%commission
                                                                                                                           click to see solution

18)Write a program to get greatest value among three numbers using nested if.
                                                                                                                            click to see solution

19)A book store gives 10% discount to  a buyer if the buyer buys books more than Rs 1000.Write a program to asking the cost of books and calculate discount amount.
                                                                                                                            click to see solution

click to next page for more programs' collection

Monday, October 5, 2015

Write a program to know a triangle is equilateral or not.

//a program to know a triangle is equilateral or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;                          // x,y z are sides
printf("enter 3 sides\n");
scanf("%d%d%d",&x,&y,&z);
if(x==y && y==z)
{
printf("it is an equilateral  triangle");
}
else
{
printf("it is not");
}
getch();
}

note:
you can use also use angle concept.

Write a program to know a triangle is right angled or not

//a program to know a triangle is right angled or not
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;                          // x,y z are angles
printf("enter 3 angles\n");
scanf("%d%d%d",&x,&y,&z);
if(x==90 || y==90 || z==90)
{
printf("it is right angled triangle");
}

else
{
printf("it is not");
}
getch();
}

Write a program to get sum or product or difference or quotient of two numbers using switch.

//program to get sum or product or difference or quotient of two numbers using switch.
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
int number1,number2;
printf("enter your choice\n");
scanf("%d",&choice);
 switch(choice)
          {
                  case 1:
                            printf("we are going to sum two numbers\n");
                            printf("enter two numbers\n");
                            scanf("%d%d",&number1,&number2);
                           printf("the sum=%d",number1+number2);
                 break;
               case 2:
                            printf("we are going to get difference of two numbers\n");
                            printf("enter two numbers\n");
                            scanf("%d%d",&number1,&number2);
                            printf("the sum=%d",number1-number2);
                 break;

               case 3:
                            printf("we are going to get product of two numbers\n");
                            printf("enter two numbers\n");
                            scanf("%d%d",&number1,&number2);
                           printf("the sum=%d",number1*number2);
                 break;
             case 4:
                            printf("we are going to get quotient of two numbers\n");
                            printf("enter two numbers\n");
                            scanf("%d%d",&number1,&number2);
                           printf("the sum=%d",number1%number2);
                 break;

              default:
                            printf("sorry, not a valid input\n");
                            printf("thank u, terminating....\n");
          }
getch();
}

note:
here, you can use 'char' data type instead 'int' and you can input data before switch statement as well.

                         



Write a program to get smallest number among three numbers.

//a program to get smallest number among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
printf("enter 3 numbers\n");
scanf("%d%d%d",&x,&y,&z);
if(x<y && x<z)
{
printf("x is smallest");
}
elseif(y<x && y<z)
{
printf("y is smallest\n");
}
else
{
printf("z is smallest");
}
getch();
}

Write a program to know a number entered by you lies between two values/range or not

//a program to know a number entered by you lies between two values/range or not
#include<stdio.h>
#include<conio.h>
void main()
{
float n,range_v,range_v1;
printf("enter a number to check\n");
scanf("%f",&n);
printf("enter range values");
scanf("%f%f",&range_v,&range_v1);
if(n>range_v && n<range_v1)
{
printf("it lies in range");
}
else 
{
printf("it does not\n");
}
getch();
}

Write a program to get greatest number among three numbers.

//a program to get greatest number among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
printf("enter  numbers\n");
scanf("%d%d%d",&x,&y,&z);
if(x>y && x>z)
{
printf("x is greatest");
}
elseif(y>x && y>z)
{
printf("y is greatest\n");
}
else
{
printf("z is greatest");
}
getch();
}

Write a program to know two numbers entered by you are same or not.

//program to know two numbers entered by you are same or not.
#include<stdio.h>
#include<conio.h>
void main()
{
float n,n1;
printf("enter a number\n");
scanf("%f%f",&n,&n1);
if(n==n1)
{
printf("they are equal");
}
else
{
printf("they are not");
}
getch();
}

Write program to know a number is positive or negative or zero.

//program to know a number is positive or negative or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
float n;
printf("enter a number\n");
scanf("%f",&n);
if(n>0)
{
printf("it is +ve");
}
else if(n<0)
{
printf("it is -ve\n");
}
else
{
printf("it is zero");
}
getch();
}

program to know a number is even or odd

//program to know a number is even or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
printf("enter a number\n");
scanf("%d",&x);
if(x%2==0)
{
printf("it is an even number");
}
else
{
printf("it is an odd number\n");
}
getch();
}


Wednesday, September 9, 2015

if-else programs collection

1) Write a program to know a number is even or odd.

                                                solution
2) Write a program to know a number is divisible by both 3 and 5 or not.

                                                 solution
3) Write program to know a number is positive or negative or zero.
                                                 solution

4)Write a program to know two numbers entered by you are same or not.

                                              solution
5) Write a program to get greatest number among three numbers.

                                                                                                   solution
6)Write a program to know a number entered by you lies between two values/range or not.

                                                                                                  solution
7)Write a program to get smallest number among three numbers.

                                                                                                 solution
8)Write a program to get sum or product or difference or quotient of two numbers using switch.


                                                                                             solution
9) Write a program to know a triangle is right angled or not.

                                                                                         solution

10) Write a program to know a triangle is equilateral or not.

                                                                                         solution
                                                                               
                                                                             
                                                              goto next section




Tuesday, April 14, 2015

program to know ascii value

// program to know ascii value
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
a=getchar();
printf("its ascii value=%d",a);
getch();
}

program to convert total seconds into hour,minutes and seconds

// program to convert total seconds into hour,minutes and seconds
#include<stdio.h> #include<conio.h> void main() { clrscr(); int total_seconds,hr,minutes,seconds; printf("enter total seconds \n"); scanf("%d",&total_seconds); hr=total_seconds/3600; // to get hour after dividing total seconds by 3600 printf("hours =%d\n",hr); hr=total_seconds%3600; // after getting hours, we have to get total left second minutes=hr/60; // to get total minutes printf("total minutes=%d\n",minutes); seconds=hr%60; // it gives total left seconds as seconds after getting months printf("the seconds=%d\n",seconds); getch(); }

note:
Here I have used same variable again and again. But I you want then you can use different variable

program to convert days into year ,month and days

// program to convert total days into year,month and days #include<stdio.h> #include<conio.h> void main() { clrscr(); int total_days,year,month,days; printf("enter total days \n"); scanf("%d",&total_days); year=total_days/365; // to get year after dividing total days by 365 printf("year =%d\n",year); year=total_days%365; // after getting years, we have to get total left days month=year/30; // to get total months printf("total months=%d\n",month); days=year%30; // it gives total left days as days after getting months printf("the days=%d\n",days); getch(); }

note:
Here I have used same variable again and again. But I you want then you can use different variable.

program to get values in of x in quadratic equation

//program to get values of x in quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2;
printf("enter values of a,b and c\n");
scanf("%f%f%f",&a,&b,&c);
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("the first value=%f\n",x1);
printf("second value is=%f",x2);
getch();
}

note:

according to formula the condition should be satisfied.like
1) (b2-4ac)>0 i.e. b2>4ac
2)a>0 for 2a
otherwise the program terminates abnormally without any result.
later we will see this program with 'if' structure'.

Logically that is said to be right.

Monday, April 13, 2015

program to get total and percentage of a student

// program to get total and percentage of a student
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int english,maths,physics,chemistry;
float total,percentage;
printf(“enter marks of different subjects \n”);
scanf(“%d%d%d%d”,&english,&maths,&physics,&chemistry);
total=english+maths+physics+chemistry;
percentage=(float)total/4; //called type casting to convert total/4 into floating value
printf(“the total marks is=%f\ and percentage=%f\n”,total,percentage);
getch();
}

note:
you can also use like,
percentage=(float)(total/4);
 or
percentage=float(total/4);
you get same output.

program to get value of f(x)

// program to get value of f(x)=x2+2x+3
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,f_x;
printf(“enter x \n”);
scanf(“%f”,&x);
f_x=(pow(x,2)+2*x+3); //pow() function exists inside math.h
printf(“the result is=%f\n”,f_x);
getch();
}

input and output programs

continued.......

P19)write a program to get value of  f(x)=x2+2x+3.Here, x is a variable.
                                                                                                                  solution
P20)Write a program to get total and percentage of a student whose marks in different subjects are given below.
                                                                                                       
subjects marks
english45
maths65
physics45
chemistry45
                                                                                                                   solution

P21)Write a program to get value of quadratic equation ax2+bx+c.Here, symbols have as usual meaning. (use simple method)                                                              
                                                                                                                   solution

P22)Write a program to convert number of days into year,months and days.
                                                                                                                    solution

P23)Write a program to convert number of seconds into hour,minutes and seconds.
                                                                                                                    solution

P24)write a program to know ascii value of entered character.
                                                                                                                    solution
                                                                                                               

Tuesday, April 7, 2015

program to get value of expression y raised to power of x

// program to get value of xy
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,y;
float result;
printf(“enter x and y\n”);
scanf(“%f%f”,&x,&y);
result=pow(x,y); //pow() function exists inside math.h
printf(“the result is=%f\n”,result);
getch();
}


note: similarly you can get others' value.