January 20, 2013

Simple C-programs


/*Program to enter two numbers by user and perform addition and multiplication operations*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int n1,n2,sum=0,mul=0 ;
     printf("\n enter any two numbers");
     scanf("%d%d",&n1,&n2);
     sum=n1+n2;
     mul=n1*n2;
     printf("\n sum of two numbers is %d",sum);
     printf("\n multiplication of two numbers is %d",mul);
     getch();
     }
output of sum and multiplication of two numbers




/*Program to enter three numbers by the user and show sum and average*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int n1,n2,n3,sum=0;
     float av=0;
     printf("\n enter any three numbers");
     scanf("%d%d%d",&n1,&n2,&n3);
     sum=n1+n2+n3;
     av=float(sum)/3.0;
     printf("\n sum is %d",sum);
     printf("\n average of three number is %f",av);
     getch();
     }
output of sum and average of three numbers



/*Program to enter length and breadth by user and show area*/

#include<stdio.h>
#include<conio.h>
void main()

{
     float ln,br,ar=0;
     printf("\n enter any length and breadth");
     scanf("%f%f",&ln,&br);
     ar=ln*br;
     printf("\n area is %f",ar);
     getch();
     }
output of area of rectangle having length and breadth



/*Program to enter base and height of any triangle so as to get area */

#include<stdio.h>
#include<conio.h>
void main()
{
     float ht,base,ar=0;
     printf("\n enter any height and base of the triangle");
     scanf("%f%f",&ht,&base);
     ar=0.5*base*ht;
     printf("\n area of the triangle is %f",ar);
     getch();
     }
output of area of triangle having base and height



/*Program to enter item code, item name, item price and item quantity by the user and find the cost given by him*/

#include<stdio.h>
#include<conio.h>
void main()
{
     int itcd;
     char itm[20];
     float pr,qty, tamt=0;
     printf("\n enter item code and item name");
     scanf("%d%s",&itcd,itm);
     printf("\n enter item price and quantity (in Kg)");
     scanf("%f%f",&pr,&qty);
     tamt=pr*qty;
     printf("\n item code is %d, \n item name is %s, \n item price is %f, \n quantity is  %f",itcd,itm,pr,qty);
     printf("\n total amount is %f",tamt);
     getch();
     }
output of customer having ID, name, price and its complete bill details




PRECAUTIONS

1. Use accurate header files as sometimes we entered getch(); but we don’t apply is header i.e. #include<conio.h>.
2. Never apply space in header files.
3. Never to apply ‘;’ after void main.
4. After declaring variables, printf() and scanf() statements apply ‘;’ so as prevent program from syntax error.
5. ‘&’ is not used in printf() statements.
6. ‘&’ is not used in declaring character and string variables in scanf() statements.
7. Apply ‘;’ after getch().
8. Always close the brackets if you have started.

1 comment:

  1. #include
    #include
    void main()
    {
    int i=74;
    printf("%c-%d7",i);
    getch();
    }

    Ans. I guess the question is really 'explain the output of this program', since the best way to find what the output is, is simply to run it.

    Firstly, this is written in the bizarre language known as 'Turbo C'. It does not conform to any C standard.

    Secondly, as Prasanna says, the output includes garbage> In
    printf("%c-%d7",i);
    variable values are inserted into the string, formatted according to the formatting character.
    %c means as a character.
    74 is the ASCII code for J. So you get J-7
    There is no value to match up with the %d, so the compiler fetches something from memory - a garbage value - and formats it as a decimal integer. So the actual output will depend on memory contents, and may vary.
    Here is a version converted to standard C:
    int main()
    {

    int i=65; // ASCII for A
    printf("%c-%dand so on",i); // you will get A-and so on...

    return 0;
    }
    BTW stop using Turbo C. I know your college uses it. Download and use CodeBlocks instead. Its free, and standards conformant.

    ReplyDelete