Tag: c programming

  • C Programming – Operators – Part 1

    • Ex 1 : Modulo (%) Operator
    • Ex 2 : Arithmetic Operations (Integer Values)
    • Ex 3 : Arithmetic Operations (Float Values)
    • Ex 4 : Arithmetic Operation (With Using Variable)
    • Ex 5 : Addition Operator (Character Variable)
    • Ex 6 : To get student mark with out scanf function-I
    • Ex 7 : To get student mark with out scanf function-II
    • Ex 8 : To get student mark with scanf function-III
    • Ex 9 : Area of Circle
    • Ex 10 :Salary Calculation
    • Ex 11 :Conversion-kilometer to Centimeter,feet,inches,meter
    • Ex 12 :Fahrenheit and Celsius temperature conversion :
    • Ex 13 :Write a program to interchange values
    • Ex 14 :To Sum of Digits (3 Digit Number)

    Example : 1 Modulo (%) Operator;

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,b;
    clrscr();
    a=7;
    b=2;
    c=a%b;
    printf(“Ans=%d”,c);
    c=a/b;
    printf(“Ans=%d”,c);
    getch();
    }

    Output :

    Ans=1
    Ans=3


    Go Top

    Example : 2 Arithmetic Operations (Integer Values)

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=7,b=2;
    clrscr();
    printf(“\nSum of A,B=%d”,a+b);
    printf(“\nSubtraction of A,B=%d”,a-b);
    printf(“\nMultiplication of A,B=%d”,a*b);
    printf(“\nDivision of A,B=%d”,a/b);
    printf(“\nModulo Division of A,B=%d”,a%b);
    printf(“\nAll Arithmetic Operations of A,B =%d”,((a+b)*(a-b)/5)%2);
    getch();
    }

    Output :

    Sum of A,B=9
    Subtraction of A,B=5
    Multiplication of A,B=14
    Division of A,B=3
    Modulo Division of A,B=1
    All Arithmatic Operations of A,B =1


    Go Top

    Example : 3 Arithmetic Operations (Float Values)

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float a=7,b=2;
    clrscr();
    printf(“\nSum of A,B=%f”,a+b);
    printf(“\nSum of A,B=%.2f”,a+b);
    printf(“\nSubtraction of A,B=%.2f”,a-b);
    printf(“\nMultiplication of A,B=%.2f”,a*b);
    printf(“\nDivision of A,B=%.2f”,a/b);

    /* This line not worked ————–

    printf(“\nModulo Division of A,B=%.2f”,a%b);

    ———————————–*/

    printf(“\nAll Arithmatic Operations of A,B =%.2f”,(a+b)*(a-b)/5);
    getch();
    }

    Output :

    Sum of A,B=9.000000
    Sum of A,B=9.00
    Subtraction of A,B=5.00
    Multiplication of A,B=14.00
    Division of A,B=3.50
    All Arithmatic Operations of A,B =9.00


    Go Top

    Example : 4 Arithmetic Operation (With Using Variable)

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,b,sum,sub,mult,div,remind;
    clrscr();
    a=10;
    b=3;
    sum=a+b;
    sub=a-b;
    mult=a*b;
    div=a/b;
    remind=a%b;
    printf(“\nSum = %d”,sum);
    printf(“\nSubtraction = %d”,sub);
    printf(“\nMultiplication = %d”,mult);
    printf(“\nDivision=%d”,div);
    printf(“\nModulo Division = %d”,remind);
    getch();
    }

    Output :

    Sum = 13
    Subtraction = 7
    Multiplication = 30
    Division=3
    Modulo Division = 1


    Go Top

    Example : 5 Addition Operator (Character Variable)

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char c1,c2;
    clrscr();
    c1=’a’;
    c2=’b’;
    printf(“\n%d”,c1); /* It Prints ascii value of a */
    printf(“\n%d”,c2);
    printf(“\n%d”,c1+c2);
    getch();
    }

    Output :

    97
    98
    195


    Go Top

    Example : 6 To get student mark with out scanf function-I

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float m1,m2,m3;
    clrscr();
    m1=80;
    m2=90;
    m3=100;
    printf(“\nSum of Marks = %.2f”,m1+m2+m3);
    printf(“\nAverage of Marks = %.2f”,(m1+m2+m3)/3);
    getch();
    }

    Output :

    Sum of Marks = 270.00
    Average of Marks = 90.00


    Go Top

    Example : 7 To get student mark with out scanf function-II

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float m1,m2,m3,tot,avrg;
    clrscr();
    m1=80;
    m2=90;
    m3=100;
    tot=m1+m2+m3;
    avrg=tot/3;
    printf(“\nSum of Marks = %.2f”,tot);
    printf(“\nAverage of Marks = %.2f”,avrg);
    getch();
    }

    Output :

    Sum of Marks = 270.00
    Average of Marks = 90.00

    Go Top

    Example : 8 To get student mark with scanf function-III

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float m1,m2,m3,tot,avrg;
    clrscr();
    printf(“\nEnter Mark 1=”);
    scanf(“%f”,&m1);
    printf(“\nEnter Mark 2=”);
    scanf(“%f”,&m2);
    printf(“\nEnter Mark 3=”);
    scanf(“%f”,&m3);
    tot=m1+m2+m3;
    avrg=tot/3;
    printf(“\nSum of Marks = %.2f”,tot);
    printf(“\nAverage of Marks = %.2f”,avrg);
    getch();
    }

    Output :

    Enter Mark 1=100
    Enter Mark 2=90
    Enter Mark 3=100

    Sum of Marks = 290.00
    Average of Marks = 96.67


    Go Top

    Example : 9 Area of Circle :

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float radius,area;
    clrscr();
    printf(“\nEnter Radius of Circle : “);
    scanf(“%f”,&radius);
    area=3.14*radius*radius;
    printf(“\nArea of Circle = %.2f”,area);
    getch();
    }

    Output :

    EEnter Radius of Circle : 2
    Area of Circle = 12.56


    Go Top

    Example : 10 Salary Calculation :

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float salary,allowance,deduction;
    clrscr();
    printf(“\nEnter Ganesh’s Basic Salary = “);
    scanf(“%f”,&salary);
    allowance=salary*(10.0/100); /* 10% of Basic Salary */
    deduction=salary*(20.0/100); /* 3% of Basic Salary */
    printf(“\nAllowance of Salary Rs.=%.2f”,allowance);
    printf(“\nDeduction of Salary Rs.=%.2f”,deduction);
    printf(“\nNet Salary = %.2f”,salary+allowance-deduction);
    getch();
    }

    Output : 1

    Enter Ganesh’s Basic Salary = 2000

    Allowance of Salary Rs.=200.00
    Deduction of Salary Rs.=400.00
    Net Salary = 1800.00


    Go Top

    Example : 11 Conversion kilometer to Centimeter,feet,inches,meter.

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float km;
    clrscr();
    printf(“Enter Kilometer Value = “);
    scanf(“%f”,&km);
    printf(“\n%.2f Kilo Meter = %.2f Centimeter”,km,100000*km);
    printf(“\n%.2f Kilo Meter = %.2f Feet”,km,km*3280.839895);
    printf(“\n%.2f Kilo Meter = %.2f Inches”,km,km*39370.07874);
    printf(“\n%.2f Kilo Meter = %.2f Meter”,km,km*1000);
    getch();
    }
    /*
    Note :
    1 Km = 1000 meter
    1 Km = 3280.839 895 feet
    1 kilometer = 39370.078 74 inch
    1 kilometer = 100000 centimeter
    */

    Output :

    Enter Kilometer Value = 3

    3.00 Kilo Meter = 300000.00 Centimeter
    3.00 Kilo Meter = 9842.52 Feet
    3.00 Kilo Meter = 118110.24 Inches
    3.00 Kilo Meter = 3000.00 Meter


    Go Top

    Example : 12 Fahrenheit and Celsius temperature conversion :

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    float fh,cel;
    clrscr();
    printf(“\nEnter Faherenheit Value = “);
    scanf(“%f”,&fh);
    cel= ((fh-32)/9.0 )*5;
    printf(“\nCelsius Value = %.2f”,cel);
    getch();
    }

    /* Note :

    To convert Fahrenheit temperatures into Celsius:

    * Begin by subtracting 32 from the Fahrenheit number.
    * Divide the answer by 9.
    * Then multiply that answer by 5.
    */

    Output :

    Enter Faherenheit Value = 50

    Celsius Value = 10.00


    Go Top

    Example : 13 Write a program to interchange values

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,b,t;
    clrscr();
    printf(“\nEnter First Value = “);
    scanf(“%d”,&a);
    printf(“\nEnter Second Value = “);
    scanf(“%d”,&b);
    printf(“\nBefore Swap a = %d,b=%d”,a,b);
    t=b;
    b=a;
    a=t;
    printf(“\nAfter Swap a=%d,b=%d”,a,b);
    getch();
    }

    Output :

    Enter First Value = 2
    Enter Second Value = 3

    Before Swap a = 2,b=3
    After Swap a=3,b=2


    Go Top

    Example : 14 To Sum of Digits (3 Digit Number)

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n,s=0;
    clrscr();
    printf(“\nEnter Three Digit Value”);
    scanf(“%d”,&n);
    s=s+(n%10);
    n=n/10;
    s=s+(n%10);
    n=n/10;
    s=s+(n%10);
    printf(“\nSum of Digits = %d”,s);
    getch();
    }

    Output :

    Enter Three Digit Value 123
    Sum of Digits = 6