C++ :: Can't Output Decimal Number From A Function Result
Mar 28, 2013
I have this simple code here:
Code:
#include <iostream>
int multiply (double x, double y) {
double result = x*y;
return (result);
[Code] ....
I get the answer 5.94 (which is what I'm looking for). I can't work out why the first example is not outputting a decimal number. I have set the variables as a double so I just can't see why this is not working for me.
I just want to practice in the language so i wrote this simple function that computes the factorial result of a certain number. The problem is that for all numbers > 20, the results are wrong ( < 20 all good).
I already learned that normal "long" type in c is more like 32 bit int and not 64 bit like a long type in java. so I used here a "long long" type.
why am I getting strange results above the number 20? isn't 64 bit enough to hold those numbers?
Code: long long factorial(int n); int main() { long long result = factorial(20); printf("%lld",result);
[code] ...
for 21 i get: -4249290049419214848
where the right result shoud be: 51,090,942,171,709,440,000
I'm trying to pass a decimal number to a function and convert it to binary and return it and print it out in main. But it prints out 1011 and then seg faults...not sure where it's tripping up
Code: int main(){ char* binNum = decToBin(25); int i = 0; while(binNum != NULL){
suppose I want to find the value of e ,e^x or sin(x) or any such function in general ,through their infinite series (maclaurin or taylor) how many iteration should I have to make to get the result correct uptu 'n' decimal places?
Code: Complete the program below which converts a binary number into a decimal number. Sample outputs are shown belowComplete the program below which converts a binary number into a decimal number. Sample outputs are shown below.
Sample Output 1:
8-bit Binary Number => 11111111 Decimal Number = 255
Sample Output 2:
8-bit Binary Number => 10101010 Decimal Number = 170
Sample Output 3:
8-bit Binary Number => 101010102 Number entered is not a binary number
#include <iostream> using namespace std; int main() { int num;
I'm working on a CGI application. I'm trying to test my input with a switch statement and output the result with html tags to populate a web page. From within the switch, I've coded as follows:
HTML Code: switch(mFunc) { case 0: cout << "<p><b>YOU ENTERED THE FOLLOWING TO BE CALCULATED:</b></p>" "<h2>"<< number1 <<"+" << number2 << "</h2>" << endl; break; case 1: cout << "You've entered" << number1 <<"-" << number2 << "to be evaluated" << endl; break;
I know that I'll need to put this in an html body with a content type as such:
I am learning c because I want to get back into programming microcontrollers, which I previously did in assembly. I wanted to make something fairly tight in terms of program memory and RAM to show me an output in binary form. When you are troubleshooting a file register or serial interface when you can see the actual bit values (on a small LCD for a micro-controller) and compare it to a datasheet.
Code: #include <stdio.h> #include <math.h> int main() { int i; int decimaltoconvert; int convertingarray[7]; int convertingarray2[7];
[Code] .....
Also, how might I go about putting that into a function that I could call?
which worked well at first because on my command prompt screen the numbers had two decimal places and looked liked this:
Premium for Pol1 is $14101.62 Premium for Pol2 is $14221.16 Premium for Pol3 is $582390.50 Premium for Pol4 is $220384.49
However, I also have the program outputting to a textfile called "output.txt" and when I opened this text file the numbers only had one or no decimal places.
Premium for Pol1 is $14101.6 Premium for Pol2 is $14221.2 Premium for Pol3 is $582391 Premium for Pol4 is $220384
How come the code for two decimal places is working for my output to command prompt but not my output to the text file?
I need to display 0-15 hex numbers[0X00-0x0F] in decimal value...& I'm getting the output but it's not exactly what it should be,below is my code.. [This not the complete code,but main part where the changes are done]
Actual output i should get is for 1v it should generate 0001,for 2v it should generate 0010 and simultaneously till [15v-1111]... But what i'm getting is exactly different to this for eg for 7v,8v&9v the bits generated are 1101,1011,1011 respectively...
This program has to convert an unsigned binary number into a decimal number. No matter what binary number I enter, however, it always outputs that the decimal number is 0.
My code is as follows:
#include <iostream> #include <cmath> #include <algorithm> using namespace std; int main() { string binarynumber; cout << "Enter an unsigned binary number up to 32 bits." << endl;
[Code] ....
And my output:
Enter an unsigned binary number up to 32 bits. 00001111 That number in decimal is 0
The output should have shown the binary number in decimal to be 15, and I cannot find my error.
event................. red house.............. blue house........... yellow house Chess ......................12.................... 5....................................8 TableTennis .................. 4.......................... 11.....................17 Basket Ball .................... 6......................... 5......................14 karathe ..........................5........................... 8.........................10
Write a program to input the house name, and the score (as shown in the table) of the sport event obtained by the corresponding house. Then find the total score . You should use nested selection statements.
i) If the user inputs a wrong house name, display the error message "In valid House Name".
ii) Modify the program to handle many data. After calculate and disp ay the total score of the house, the program should display a prompt "Do you need to enter nother house name?".
If the user inputs "y" or "Y", program should ask for the next house name and the scores. If the user inputs "n" or "N" after entering all three houses, program should terminate by printing the score of the winner. If the user inputs "n" or "N" and without entering all three houses, program should display an error message "Error in finding the winner" and terminate without printing the ranks.
Note: format the output into two decimal places.
Sample output: Enter the house name: red house Enter the score of Chess: 12 Enter the score of Table Tennis:4 Enter the score of Basket Ball:6 Enter the score of karathe 5 Score is: 27 Points Do you enter another house name? y
Enter the house name: blue house Enter the score of Chess:5 Enter the score of Table Tennis:ll Enter the score of Basket Ball:5 Enter the score of karathe 8 Score is: 29 Points Do you enter another house name? y
Enter the house name: yellow house Enter the score of Chess:8 Enter the score of Table Tennis: 17 Enter the score of Basket Ball: 14 Enter the score of karathe 10 Score is: 49 Points Do you enter another house name? n The winner scored: 49 Points
Write a C++ application program to accept a signed decimal integer as input and output the equivalent 2s complement version in 16-bit binary. Include a space between every four bits in the output string. The input will only be processed by the application if it falls in the valid range that can be represented in 2s complement format with 16 bits. The range of a decimal number from - to + is -32768 to 32767.
How would I by any means print out the result of one number being added by two numbers 999999 times, the numbers both happen to be about 16500 digits long. It must display ALL digits in the number (all the digits the result ends up being). I know I can't use normal data-types, so what do I do?
Looks kinda like this:
for (int i = 0; i < 999999; ++i) { total += a; total += b; }
a and b being 2 different numbers with about 16500 digits.
I am very new to programming and have been working on a program that can receive decimals or binary numbers and convert them. The decimal --> binary works fine. For some reason I cannot figure out I cannot get the "BinaryToDecimal" function to perform. By putting a "printf" into the for-loop.
Code:
#include <stdio.h>#include <string.h> #include <math.h> char* ReverseString (char _result[]) { int start, end, length = strlen(_result); char swap; for (start = 0, end = length-1; start < end; start++, end--)
I have a code and am asked to modify it so that it will take as input as unsigned binary number up to 16 digits in length and convert it into its equivalent decimal number and output that decimal number.
All I know is that I use library function strlen() in <cstring> to calculate the length of the input string.
I also know I have to use something called pow(2,4);
//pow (); is found in cmath
I was told to use sum = sum >>16-l; (l is the length of />/>
#include <iostream> using namespace std; int main() {
I'm writting an algorithm which equals to std::to_string. The inttostring part is fine, and the decimal_part too. But when the number digits > 5, the program loops infinitely.
#include "stdio.h" #include <stdarg.h> #include <math.h> // Main Function int main(void){ int number; printf(" Please enter a number from 1-10? "); scanf("%d", &number);
[Code] ....
I took the while statement out didn't want that in there.
I need the user to be able to input the number of decimal places they wish to have displayed in the output. Everything works fine as is, I just don't know how to allow for the user to input the number of decimal places they want the output to have.
Code:
#include<stdio.h> #include<math.h> #define PI 3.141592654 int main(void) { //Local Declarations int x; //desired number of decimal places float radius; //radius of circle float circumference; //circumference of circle