C program to display the float number on the defined decimal places.
#include <stdio.h>
int main() {
float number;
int decimalPlaces;
// Get the float input from the user.
printf("Enter a floating-point number: ");
scanf("%f", &number);
// Get the desired number of decimal places from the user.
printf("Enter the number of decimal places to display: ");
scanf("%d", &decimalPlaces);
// Input validation: Check if the number of decimal places is valid.
if (decimalPlaces < 0) {
printf("Number of decimal places must be non-negative.\n");
return 1; // Return an error code
}
// Print the float with the specified precision.
// Use the format specifier "%.*f" to dynamically set the precision.
printf("Formatted number: %.*f\n", decimalPlaces, number);
return 0; // Return success code.
}
Program Description:
Include Header: The line #include <stdio.h> includes the standard input/output library, which provides functions like printf for printing to the console and scanf for reading input from the user.
Declare Variables:
float number: This variable will hold the floating-point number entered by the user.
int decimalPlaces: This variable will be used to store the decimal places that the user wishes to display.
Take Input:
The program asks the user to enter a floating-point number by using printf("Enter a floating-point number: ").
The scanf("%f", &number); function takes input in the form of a floating point number and saves it on the number variable. The %f is to get a float, while the & is the address of operator which gives memory location for variable number so scanf can store the input there.
It then asks the user to enter the desired number of decimal places using printf("Enter the number of decimal places to display: ");.
The scanf("%d", &decimalPlaces); function reads the integer entered by the user and stores it in the decimalPlaces variable. The %d format specifier is used to read an integer.
Validate Input:
The program checks if the entered decimalPlaces is negative by if(decimalPlaces < 0).
If decimalPlaces is negative, this program prints an error message using printf("Number of decimal places must be non-negative.\n"); and returns 1 for an error condition.
Print Output:
The program prints out the formatted floating-point number using printf("Formatted number: %.*f\n", decimalPlaces, number).
%.*f is a format specifier; at runtime, the * is replaced by the value of decimalPlaces, allowing precision to be specified dynamically. If, for example, decimalPlaces equals 2 and number equals 3.14159, the output will be "Formatted number: 3.14".
\n is a newline character, moving the cursor to the next line in the console.
Return 0:
The program returns 0, thus indicating that it has executed successfully.
Basic and Operators practice program assignments in C Language :
Practice/Assignment set 1:
1.Write a program in C to Input integer, float and character values using one scanf().
2.Write a program in C language to find the Area of Circle.
3.Write a program in C language to Input an integer value and print with padding by Zeros in C
4. Write a program in C language to Input float value and print it with specified digit after decimal point in C
5.Write a program in C language to find sum of the digits of any three digit number.
6. Write a program in C language to Input an unsigned integer value using scanf().
7.Write a program in C language to Find area of Triangle
8.Write a program in C language to Input a hexadecimal value using scanf() in C
9.Write a program in C language to Input octal value using scanf()
10.Write a program in C language to Convert temperature given in farenheight to degree celcius.
11.Write a program in C language to Input octal value using scanf()
12.Write a program in C language to Input decimal, octal and hexadecimal values in character variables using scanf() in C
13.Write a program in C language to Input an integer value in any format (decimal, octal or hexadecimal) using '%i' in C
14.Write a program in C language to Input side value and find area of Square.
15.Write a program in C language to Input individual characters using scanf() in C
16.Write a program in C language to Read a memory address using scanf() and print its value in C
17.Write a program in C language to Skip characters while reading integers using scanf() in C
18.Print your name, date of birth, and mobile number
19. Write a C program to convert specified days into years, weeks and days.
Note: Ignore leap year.
21.Write a C program that accepts two item's weight and number of purchases (floating point values) and calculates their average value.
22.Write a C program that accepts an employee's ID, total worked hours in a month and the amount he received per hour. Print the ID and salary (with two decimal places) of the employee for a particular month.
Test Data :
Input the Employees ID(Max. 10 chars): 0342
Input the working hrs: 8
Salary amount/hr: 15000
Expected Output:
Employees ID = 0342
Salary = U$ 120000.00
23.Write a C program that accept an students roll number and six subject markes, find total and average marks.
24.Write a C program that accepts an employee's ID, basic salary in a month and calculate total salary.
The formulat for
Total salary=(basic salary-tax)+House Rent allowance+Bonus+Traveling allowance+other allowance
tax 10% of basic salary
House Rent allowance 17% of basic salary
Bonus 7% of basic salary
Traveling allowance 8% of basic salary
other allowance 10% of basic salary
25.Write a C program to convert a given integer (in days) to years, months and days, assuming that all months have 30 days and all years have 365 days.
Test Data :
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s)
Practice/Assignment set 2:
1. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2
2. C program to find sum of the all natural numbers from 1 to N.
Series: 1+2+3+4+..N
3.C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms