Question Bank (5 marks) · Q11
Q.Write a C program to accept two floating point numbers and compute their ratio. If the ratio is greater than zero then it should exchange the contents of the numbers.
Karnataka PUCTextbookLong· 5mImportance★★★★★est
38% · 58/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
Compute ratio = x/y; if ratio > 0, swap x and y using a temporary variable.
The two numbers are read into float variables, their ratio x/y is computed, and the sign of the ratio is tested. A ratio greater than zero means both numbers have the same sign; in that case the contents are exchanged with the help of a temporary variable temp.
#include <stdio.h>
int main()
{
float x, y, ratio, temp;
printf("Enter two floating point numbers: ");
scanf("%f %f", &x, &y);
ratio = x / y;
printf("Ratio = %.2f\n", ratio);
if (ratio > 0)
{
temp = x;
x = y;
y = temp;
printf("Ratio > 0, so contents are exchanged\n");
printf("After exchange: x = %.2f, y = %.2f\n", x, y);
}
else
printf("Ratio is not greater than zero, no exchange\n"); …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.