You are on page 1of 3

6) Write a simple program that prints the result of all operators available in c.

Required operand values from the standard input.

Ans:

Code:
#include <stdio.h>

int main() {

float operand1, operand2;

printf("Enter operand1: ");

scanf("%f", &operand1);

printf("Enter operand2: ");

scanf("%f", &operand2);

printf("Sum: %.2f\n", operand1 + operand2);

printf("Difference: %.2f\n", operand1 - operand2);

printf("Product: %.2f\n", operand1 * operand2);

if (operand2 != 0) {

printf("Quotient: %.2f\n", operand1 / operand2);

} else {

printf("Division by zero is undefined.\n");

printf("Increment (operand1): %.2f\n", ++operand1);

printf("Decrement (operand2): %.2f\n", --operand2);

printf("Negation (operand1): %.2f\n", -operand1);

printf("Negation (operand2): %.2f\n", -operand2);

return 0;

Output:
7) Write a simple program that converts one given data type to another

using auto conversion and casting. Take the values from the standard input.

Ans:
Code:

#include <stdio.h>

int main() {

int integerValue;

float floatValue;

printf("Enter an integer value: ");

scanf("%d", &integerValue);

floatValue = integerValue;

printf("Automatically converted to float: %.2f\n", floatValue);

printf("Enter a float value: ");

scanf("%f", &floatValue);

integerValue = (int)floatValue;

printf("Casted to integer: %d\n", integerValue);

return 0;

Output:
8) A building has 10 floors with a floor height of 3 meters each. A ball is dropped from the top of the
building. Find the time taken by the ball to reach each floor, then the formula s=ut+1/2at^2 where u
and a are the initial velocity (m/sec) and acceleration (m/s^2) respectively.

Ans:

Code:

#include <stdio.h>

#include <math.h>

int main() {

const int numFloors = 10;

const float floorHeight = 3.0;

const float gravity = 9.8;

printf("Time taken to reach each floor:\n");

for (int floor = 1; floor <= numFloors; ++floor) {

float distance = floor * floorHeight;

float time = sqrt(2 * distance / gravity);

printf("Floor %d: %.2f seconds\n", floor, time);

return 0;

Output:

You might also like