Interactive Exercises 2

 As continuous lets do some more exercises

1 . Write a program that converts inches to centimeters. For example, if the user enters 16.9
for a Length in inches, the output would be 42.926cm. (Hint: 1 inch = 2.54 centimeters.)



ANSWER


#include<stdio.h>
int main()
{
float inch, centimeter;
/* inpur from user */
printf ("Enter the inch value : ");
scanf("%f", &inch);
/* converting inch to centimeter*/
centimeter = inch * 2.54;
/* display the value in centimeter */
printf("the given value %f inch converted to %f cm", inch, centimeter);
return 0;
}



2. Write a program to evaluate the polynomial shown here:

3
x3 - 5x2 + 6 for x = 2.55




ANSWER

#include<stdio.h>
int main()
{
double x = 2.55;
double result;
result = (3 * x * x * x) - ( 5 * x * x)  + 6;
printf("Answer for the polynomial equation is : %f", result); 
return 0;


3. Write a program that evaluates the following expression and displays the results
(remember to use exponential format to display the result):
(3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8)




ANSWER 

#include<stdio.h>
int main()
{
/* (3.31 × 10-8 × 2.01 × 10-7) / (7.16 × 10-6 + 2.01 × 10-8)*/
float result;
result = (3.31e-8 * 2.01e-7) / (7.16e-6 + 2.01e-8);
printf("Value of the expression is : %e", result);
return 0;
}


4. Write a program to input your mid term marks of a subject marked out of 30 and the final exam marks out of 100. Calculate and print the final results.
Final Result = Mid Term + 70% of Final Mark




ANSWER

#include <stdio.h>

int main() {
    int midtermmarks, finalmarks;
    float finalresult;

    /* Input midterm marks */
    printf("Enter midterm marks (out of 30): ");
    scanf("%d", &midtermmarks);

    /* Input final exam marks */
    printf("Enter final exam marks (out of 100): ");
    scanf("%d", &finalmarks);

    /* Calculate final result */
if (midtermmarks <= 30 && finalmarks <= 100)
{
    finalresult = midtermmarks + 0.7 * finalmarks;
}
    /* Print final result*/
    printf("Final result: %.2f\n", finalresult);

    return 0;
}


5. Swap two values stored in two different variables.




#include<stdio.h>
int main()
{
int a = 10, b = 5, c;
c = a;   /* assigning a for c */
a = b;   /* assigning b for a */
b = c;    /* assigning c for b */
printf("Value of a is %d and b is %d", a, b);
return 0;
}





6. Check whether an entered number is negative, positive or zero.







#include<stdio.h>
int main()
{
int num;
printf("enter the number : ");
scanf("%d", &num);
if ( num == 0 )
printf("Entered number is zero");
else if (num < 0)
printf("Entered number is a negative number");
else 
printf ("entered number is a positive number");
return 0;
}

Post a Comment

0 Comments