To create a program that reads a floating-point number from the user and prints it with two decimal places.
- Begin the program.
- Declare a variable
a
of type float to store the input floating-point number. - Prompt the user to input a floating-point number.
- Read the floating-point number input using
scanf()
function and store it in the variablea
. - Print the value of
a
usingprintf()
function with formatting specifier "%.2f" to display it with two decimal places. - End the program.
#include <stdio.h>
int main()
{
float a;
scanf("%f",&a);
printf("%.2f",a);
}
Thus the required program is written and executed successfully.
To create a program that reads a floating-point number from the user and prints it with two decimal places.
- Begin the program.
- Declare a variable
a
of type float to store the input floating-point number. - Prompt the user to input a floating-point number.
- Read the floating-point number input using
scanf()
function and store it in the variablea
. - Print the value of
a
usingprintf()
function with formatting specifier "%.2f" to display it with two decimal places. - End the program.
#include <stdio.h>
int main()
{
float a;
scanf("%f",&a);
printf("%.2f",a);
}
Thus the required program is written and executed successfully.
To develop a program that compares two integer inputs and prints the greatest one.
- Start the program.
- Declare two variables
a
andb
of type int to store the integer inputs. - Prompt the user to input two integers.
- Read the integer inputs using
scanf()
function and store them in variablesa
andb
. - Use an if-else statement to compare
a
andb
. - If
a
is greater thanb
, print "A is greatest." usingprintf()
function. - If
b
is greater thana
, print "B is greatest." usingprintf()
function. - End the program.
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if (a>b)
printf("A is greatest.");
else
printf("B is greatest.");
}
Thus the required program is written and executed successfully.
To develop a program that compares two integer inputs and informs whether they are equal or not, and if not, which one is greater.
-
Begin the program.
-
Declare two variables
a
andb
of type int to store the integer inputs. -
Prompt the user to input two integers.
-
Read the integer inputs using
scanf()
function and store them in variablesa
andb
. -
Use an if-else statement to check if
a
is not equal tob
.i) If
a
is not equal tob
, execute the following steps:Print "A is not equal to B".
Use nested if-else statements to determine which integer is larger:
If `a` is greater than `b`, print "A is larger than B". If `b` is greater than `a`, print "B is larger than A".
ii) If
a
is equal tob
, print "A is equal to B". -
End the program.
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if(a!=b)
{
printf("A is not equal to B");
if(a>b)
printf("\nA is larger than B");
else
printf("\nB is larger than A");
}
else
printf("A is equal to B");
}
Thus the required program is written and executed successfully.
To create a program that calculates the rate of interest based on the principal amount, rate, and simple interest provided by the user.
- Begin the program.
- Declare variables
p
(principal),r
(rate),si
(simple interest), andy
(rate of interest in percentage) of type float. - Prompt the user to input the principal amount, rate, and simple interest.
- Read the input values for
p
,r
, andsi
usingscanf()
function. - Calculate the rate of interest (
y
) using the formula: [ y = \frac{si \times 100}{r \times p} ] - Print the rate of interest (
y
) with two decimal places usingprintf()
function. - End the program.
#include<stdio.h>
int main(){
float p,r,si;
scanf("%f%f%f",&p,&r,&si);
float y;
y=(si*100)/(r*p);
printf("Rate of Interest is = %.2f percentage",y);
}
Thus the required program is written and executed successfully.
To develop a program that calculates the minimum value between two floating-point numbers provided by the user.
-
Begin the program.
-
Declare variables
a
andb
of type float to store the input floating-point numbers. -
Prompt the user to input two floating-point numbers separated by space.
-
Read the input values for
a
andb
usingscanf()
function. -
Use the ternary conditional operator
(a < b) ? ... : ...
to comparea
andb
:i) If
a
is less thanb
, print "Minimum between <a> and <b> is <a>" usingprintf()
function with formatting specifier "%.3f" to display the minimum valuea
with three decimal places.ii) If
b
is less thana
, print "Minimum between <a> and <b> is <b>" usingprintf()
function with formatting specifier "%.3f" to display the minimum valueb
with three decimal places. -
End the program.
#include <stdio.h>
int main()
{
float a,b;
scanf("%f %f",&a,&b);
(a < b) ? printf("Minimum between %.3f and %.3f is %.3f",a,b,a):
printf("Minimum between %.3f and %.3f is %.3f",a,b,b);
}
Thus the required program is written and executed successfully.
To create a program that identifies the maximum number among three integers provided by the user.
1 Begin the program.
-
Declare variables
r
,p
, andk
of type int to store the input integers. -
Prompt the user to input three integers.
-
Read the input values for
r
,p
, andk
usingscanf()
function. -
Use nested if-else statements to compare the three integers:
i) If
r
is greater than bothp
andk
, print "Maximum Number is <r>" usingprintf()
function.ii) Else, if
p
is greater than bothr
andk
, print "Maximum Number is <p>" usingprintf()
function.iii) Otherwise, print "Maximum Number is <k>" using
printf()
function. -
End the program.
#include <stdio.h>
int main()
{
int r,p,k;
scanf("%d%d%d",&r,&p,&k);
if (r>p && r>k)
printf("Maximum Number is %d",r);
else if(p>r && p>k)
printf("Maximum Number is %d",p);
else
printf("Maximum Number is %d",k);
return 0;
}
Thus the required program is written and executed successfully.
AIM:
To determine if a person is eligible for marriage based on their age.
ALGORITHM:
- Begin the program.
- Declare a variable
age
of type int to store the age of the person. - Prompt the user to input their age.
- Read the input value for
age
usingscanf()
function. - Use an if statement to check if the
age
is greater than or equal to 18: i) If true, print "She is eligible for marriage" usingprintf()
function. - End the program.
#include <stdio.h>
int main()
{
int age;
scanf("%d",&age);
if (age>=18)
printf("she is eligible for marriage");
return 0;
}
Thus the required program is written and executed successfully.