Question

I need help writing a C programming code. I am trying to create a program that...

I need help writing a C programming code.

I am trying to create a program that returns ANY given number's decimal value as an Integer (Whole number).

Examples:

User input: 0.35 Output: 35

User input: 1.465 Output: 465

User input: 10.6054 Output: 6054

0 0
Add a comment Improve this question Transcribed image text
Answer #1

float.c 1 #include<stdio.h> #include<math.h> //math header file for pow() function 2 3 4. 5 6 7 8. 9 10 11 12 int main() { //22 23 24 25 26 while((int)diff){ // incrementing iter by 1 iter += 1; // storing decimal value in diff diff = diff - (int)difC:\Users\krishna\Downloads>gcc -o float float.c C:\Users\krishna\Downloads>float 0.35 35 C:\Users\krishna\Downloads>float 1.4

Raw_code:

#include<stdio.h>
#include<math.h> //math header file for pow() function

int main(){
// declaring temporary variables
double number, diff, temp_diff;
int num, result = 0;
// taking input from user and stroring it in number variable
scanf("%lf", &number);
// converting number into integer type and storing in num variable
num = (int)number;
// storing decimal value by substracting integer from input number
diff = number - num;
// stroring decimal value in another temporary variable
temp_diff = diff;
// making diff greater than zero by multiply it with 10(base)
diff *= 10;
// iter variable for storing count of numbers after decimal point
int iter = 0;
// while loop for counting numbers after decimal point
// condition is converting diff into integer type , which is true until number is not zero
while((int)diff){
// incrementing iter by 1
iter += 1;
// storing decimal value in diff
diff = diff - (int)diff;
diff *= 10;
}
// stroring resultant integer in by multiplying decimal value with 10 power iters
result = (int)( temp_diff * pow(10, iter));
printf("%d", result);
}

sorry: it doesn't work for last one

Add a comment
Know the answer?
Add Answer to:
I need help writing a C programming code. I am trying to create a program that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ programming language , I need help writing the code . Please explain if you can...

    C++ programming language , I need help writing the code . Please explain if you can . Exercise 10: Unit conversion (10 points) Write a console program that converts meters into feet and inches. The program starts by asking the user for a measurement in meters. The program should accept a floating point number. After getting the number, the program computes the number of feet and inches equal to the specified number of meters. The program outputs feet and inches...

  • I need help with this C++ assignment. Create a program which will implement the sum of...

    I need help with this C++ assignment. Create a program which will implement the sum of the numbers between 1 and n where n is an integer. When completed, the program will allow the user to input a positive integer (n) then the program will add all numbers between 1 and n (including n) and output the sum. For example, if the user enters 8 then the program will sum the numbers 1 through 8 and will output the sum....

  • I need help with this python programming exercise, please! thanks in advance Create a Python script...

    I need help with this python programming exercise, please! thanks in advance Create a Python script file called hw4.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Ex. 1. Write a program that inputs an integer number from the user, then prints a letter "O" in ASCII art using a width of 5 and the...

  • Please help me with the following C Programming project. I am providing the code I used...

    Please help me with the following C Programming project. I am providing the code I used which needs to be reworked to add the following requirements. Here is my code #include<stdio.h> char input; int main() { int i = 0; while (true){ scanf_s("%c", &input); if (input == 'X') break; printf("%c", input); } return 0; } Here are the requirements for the code plus and additional note what the code should have. Goals Understand the ASCII representation of character values. Understand...

  • I need help writing these functions in C programming. Write a C function that checks if...

    I need help writing these functions in C programming. Write a C function that checks if an array is sorted or not using an iterative approach. Write a C function that checks if an array is sorted or not using a recursive approach Write a C function to reverse the elements of an array. Note you are not allowed to use an 1. additional array to do that Write a C function to find the sum of the elements of...

  • Hello. I just need help with my c++ code. Write a program that: Creates an integer...

    Hello. I just need help with my c++ code. Write a program that: Creates an integer variable AND an integer pointer variable Ask the user to input an integer value Store the value in the integer variable Print the address of the variable. Make the pointer variable point at the integer value Print the value pointed to by the pointer Print the address of the pointer

  • I am trying to create a program called Count.java that contains a single method called compute...

    I am trying to create a program called Count.java that contains a single method called compute with a string parameter called value. The method should count the number of newlines, words (any sequence of characters separated with whitespace), and characters in a given string and return an array of the three integer values. Use a Scanner to count the number of words in a string. The Scanner.next() method returns the next word, ignoring whitespace.

  • Hello I am confused about this C++ program. I need to create a payroll C++ program...

    Hello I am confused about this C++ program. I need to create a payroll C++ program following these steps. Source file structure Your program will consist of three source files: main.cpp the main program Payroll.hppclass declaration for the Payroll class. Make sure you have an include guard. Payroll.cpp Payroll's member functions Class definition Create a Payroll class definition. The class has private members which originate from user input: number of hours worked hourly rate float float - and private members...

  • Hi there I need help writing an algorithm to solve Sudoku puzzles through integer linear programming...

    Hi there I need help writing an algorithm to solve Sudoku puzzles through integer linear programming using MATLAB. If you could please provide the code and an explanation of how it works I'd be really grateful. Any assistance would be much appreciated, many thanks in advance!

  • I'm using code blocks to perform this. These problems are from my C Programming Beginners book....

    I'm using code blocks to perform this. These problems are from my C Programming Beginners book. Not too familiar with C programming so any help would be great. I would like to see how it is formatted. Also, I don't understand Problem 4 at all. Thanks. 1- Create three integer variables and initialize to zero. 2- Create three float variables and initialize to zero. 3- Create three double variables and initialize to zero. 4- Create one character variables and initialize...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT