Practice program of using an Operators.

 Practice program

1. Write a  program that enters temperature in Celsius and converts that into Fahrenheit.

solution : first you can write program in your IDE create a logic how to solve the problem 

※  convert temperature  Celsius  to Fahrenheit use 

๐Ÿ˜Š Formula : ℉=( ℃ × 1.8) + 32 OR ℉=( ℃ × 9/5) +32

#include <stdio.h>
int main()
{
    /*program to convert temperature Celsius to Fahrenheit*/
    float C, F;
    printf("Input the Temperature in degree Celsius:");
    scanf("%f", &C);
    F = (C * 9 / 5) + 32;
    printf("%.2f degree Celsius = %.2f degree Fahrenheit", C, F);
    return 0;
}

Output: Input the Temperature in degree Celsius:31.5
31.50 degree Celsius = 88.70 degree Fahrenheit

2. Write a program to accepts the radius of a circle and calculates the area and perimeter of that circle.

solution : logic of that program :

※ accept the value of radius by the user.

※ And put the value in the given formulae

Area of Circle =  3.14159  × radius × radius

Perimeter of Circle / Circumference of Circle = 2  × 3.14159 × radius

※ print the output.

#include <stdio.h>
int main()
{
    /*program to find the perimeter and area of circle at given radius by the user*/
    float radius,area,perimeter;
    printf("Enter the radius:");
    scanf("%f",&radius);
    perimeter=2* 3.14159*radius;
    area=  3.14159  * radius * radius;
    printf("Radius of a circle:%.3f\n",radius);
    printf("perimeter of a circle:%.3f\n",perimeter);
    printf("Area of a circle:%.3f\n",area);
    return 0;
}
Output:
Enter the radius:3.812
Radius of a circle:3.812
perimeter of a circle:23.951
Area of a circle:45.652

3. Write a program to accept a number in decimal and print the number in octal and hexadecimal.

Here the conversion specification character %o and %x , thus the octal and Hexadecimal equivalent of the decimal number stored in the variable dec is displayed.

#include<stdio.h>
int main()
{
    int dec=12;
    printf("Octal equivalent of %d = %o",dec,dec);
    printf("\n Hexadecimal equivalent of %d = %x",dec,dec);
    return 0;
}

Output :
Octal equivalent of 12 = 14 Hexadecimal equivalent of 12 = c

4.Write a program to accept any number and print the value of remainder after  dividing it by 3.

※ store the number by the user.

※ After storing the number it divide by 3 and store remainder in rem  variable

※ after that print remainder .

※ end the program


#include <stdio.h>
int main()
{
    /*Write a program to accept any number and print the value of remainder after  dividing it by 3.*/
    int num, rem;
    printf("Enter the Number :");
    scanf("%d", &num);
    /*Note : modular operator not applied on floating point number */
    rem = num % 3; // use can find the remainder by using modular operator
    printf("remainder of %d after dividing by 3 is: %d", num, rem);
    return 0;
}

Output:
Enter the Number :20
remainder of 20 after dividing by 3 is: 2
Enter the Number :20.22
remainder of 20 after dividing by 3 is: 2

5. Accept any two number, if the first number is greater than second then print the difference of these two numbers, otherwise print their sum. write this program using ๐Ÿ˜ŠTernary operator.

syntax:
Test_Expression ? expression1 : expression2 ;

※ store the numbers a and b  by the user.

※ After storing the number if first number is greater than second then a-b , other wise  second number is greater than first than a + b.

※ after that print .

※ end the program.

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter the value of a and b:\n");
    scanf("%d%d",&a,&b);
    // here be use a ternary operator
    a>b?printf("difference of a and b is %d",a-b):printf("Sum of a and b is %d ",a+b);
    return 0;
}

Output: 
Enter the value of a and b: 
20 
11
 Difference of a and b is 9

6.Write a program that accepts marks in five subject and calculates the total percentage marks.

※ input the marks of student into five subjects out of 100  by the user in m1, m2, m3, m4, m5

※ find the total marks m1 + m2 + m3+ m4 + m5

※ find percentage of a student : sum / 5 OR  (sum /500)*100 and store in the variable per.

※ after that print the total percentage gain by the student .

※ end the program.

#include<stdio.h>
int main()
{
    float m1,m2,m3,m4,m5,per,sum;
    printf("Marks in English:");
    scanf("%f",&m1);
    printf("Marks in Hindi:");
    scanf("%f",&m2);
    printf("Marks in Physics:");
    scanf("%f",&m3);
    printf("Marks in Maths:");
    scanf("%f",&m4);
    printf("Marks in Chemistry:");
    scanf("%f",&m5);
    sum=m1+m2+m3+m4+m5;
    per=sum/5;
    printf("Total percentage marks:%.2f%%",per);
    return 0;
}

Output:
Marks in English:59
Marks in Hindi:69
Marks in Physics:98
Marks in Maths:68
Marks in Chemistry:65
Total percentage marks:71.80%

Comments

Popular posts from this blog

18 Amazing programs in C.