I am a beginner in c++. I was solving problems in CodeChef and came across this prooblem: http://www.codechef.com/problems/FCTRL2
The problem asks you to find the factorial of large numbers. To achieve this I am trying to perform digit by digit multiplication. However, in my code in line 39 the sum of "b[i] * c[j] * pow(10,(i+j)) + x" is being reduced by 1 from 3rd iteration of the loop. I am not able to figure out the reason for sum being reduced by 1.
Kingly help me out with this problem.
Here is the code:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
// to find the number of digits
int num_digits(int val) {
int digits = 0;
while(val) {
val /= 10;
digits++;
}
return digits;
}
int main() {
std::ios_base::sync_with_stdio(false);
int b[10], c[10], num1, num2, x=0, num_inputs;
std::cin >> num_inputs;
while(num_inputs) {
std::cin >> num1;
num2 = num1 - 1;
int num1_digits = num_digits(num1);
int num2_digits = num_digits(num2);
int temp1 = num1;
int temp2 = num2;
while(num2>0) {
std::cout << temp2 << " " << temp1 << " " << num1_digits << " " << num2_digits<< endl;
for(int i=0; i<num2_digits; i++) {
b[i] = temp2 % 10;
temp2 /= 10;
for(int j=0; j<num1_digits; j++) {
c[j] = temp1 % 10;
temp1 /= 10;
std::cout << b[i] << " " << c[j] << " " << pow(10, (i+j)) << " ";
x = b[i] * c[j] * pow(10,(i+j)) + x; // the sum is getting reduced by 1
std::cout << x << endl;
}
temp1 = num1;
}
num2--;
temp2 = num2;
temp1 = x;
x = 0;
num1_digits = num_digits(temp1);
num2_digits = num_digits(temp2);
}
std::cout << temp1;
num_inputs--;
}
return 0;
}
pow is floating-point exponentiation, not integer exponentiation. You should only use it when you want approximate, floating point results, not exact, integer results.
Related
Q) Write a program that defines and tests a factorial function. The factorial of a number is the product of all whole numbers from 1 to N.
For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120
Problem: I am able to print the result,but not able to print like this :
let n = 5
Output : 1 * 2 * 3 * 4 * 5 = 120;
My Code:
# include <bits/stdc++.h>
using namespace std;
int Factorial (int N)
{
int i = 0;int fact = 1;
while (i < N && N > 0) // Time Complexity O(N)
{
fact *= ++i;
}
return fact;
}
int main()
{
int n;cin >> n;
cout << Factorial(n) << endl;
return 0;
}
I am able to print the result,but not able to print like this : let n
= 5 Output : 1 * 2 * 3 * 4 * 5 = 120;
That's indeed what your code is doing. You only print the result.
If you want to print every integer from 1 to N before you print the result you need more cout calls or another way to manipulate the output.
This should only be an idea this is far away from being a good example but it should do the job.
int main()
{
int n;cin >> n;
std::cout << "Factorial of " << n << "!\n";
for (int i =1; i<=n; i++)
{
if(i != n)
std::cout << i << " * ";
else
std::cout << n << " = ";
}
cout << Factorial(n) << endl;
return 0;
}
Better approach using std::string and std::stringstream
#include <string>
#include <sstream>
using namespace std;
int main()
{
int n;
cin >> n;
stringstream sStr;
sStr << "Factorial of " << n << " = ";
for (int i = 1; i <= n; i++)
{
if (i != n)
sStr << i << " * ";
else
sStr << i << " = ";
}
sStr << Factorial(n) << endl;
cout << sStr.str();
return 0;
}
I am writing a program where the input is a number and it is converted to binary string and the binary bits are stored in an array. It is done for two/(or more) numbers and after the conversion I want to perform the bitwise xor operation between the two bit strings. For this I want to define a function and get outputs for different numbers. How to do that ?
My try
#include <bits/stdc++.h>
using namespace std;
int decTobin1(int decimal, int *binary1[6])
{
int binary1[6]{};
int round{};
int rem;
while (decimal != 0)
{
*binary1[round] = decimal % 2;
decimal /= 2;
++round;
}
}
int decTobin2(int decimal, int *binary2[6])
{
int binary2[6]{};
int round{};
int rem;
while (decimal != 0)
{
*binary2[round] = decimal % 2;
decimal /= 2;
++round;
}
}
/*int decTobin3(int decimal, int* binary3[6])
{
int binary3[6]{};
int round{};
int rem;
while (decimal != 0)
{
*binary3[round] = decimal % 2;
decimal /= 2;
++round;
}
}*/
int main()
{
int binary1[6], binary2[6]; //binary3[6];
cout << "Enter a number :";
cout << "Enter a number :";
int num1, num2;
cin >> num1 >> num2;
int num1 = decTobin1(num1, &binary1);
int num2 = decTobin2(num2, &binary2);
//int newkey2 = decTobin3(num3,&binary3);
cout << "Print :";
for (int k{}; k < 6; ++k)
{
cout << binary1[k] << endl;
}
for (int k{}; k < 6; ++k)
{
cout << binary2[k] << endl;
}
/*for(int k{};k<6;++k){
cout << binary3[k] << endl;
}*/
}
second try ...
using namespace std;
struct binary
{
int binary1[6]{};
};
binary decTobin()
{
int round{};
int decimal;
int binary1[6]{};
int rem;
while (decimal != 0)
{
binary1[round] = decimal % 2;
decimal /= 2;
++round;
}
binary temp{binary1[0], binary1[1], binary1[2], binary1[3], binary1[4], binary1[5]};
return temp;
}
int main()
{
int binary1[6], binary2[6]; //binary3[6];
cout << "Enter a number :";
//cout << "Enter a number :";
int num1, num2;
cin >> num1; // >> num2 ;
binary zero{decTobin()};
//int num1 = decTobin(num1,&binary1);
//int num2 = decTobin2(num2,&binary2);
//int newkey2 = decTobin3(num3,&binary3);
cout << "Print :";
for (int k{}; k < 6; ++k)
{
cout << zero.binary1[k] << endl;
}
/*for(int k{};k<6;++k){
cout << binary3[k] << endl;
}*/
}
I tried to run this but it crashed. Can anyone help me out. I basically want to learn the way to return array values. Also I am new to programming and learning so little bit explanation will be helpful. Thank You.
#include <iostream>
using namespace std;
// on input n returns the value a_n as a double
double term(int n) {
double val = 1.0 / (n+1.0) / (n+1.0);
if (n%2 != 0) val = -val;
return val;
}
/* computes the sum for i from k to k+n-1 of term(i) by the
* direct upwards method */
double direct_up(int k,int n) {
double sum = 0.0;
for (int i=0; i<n; i++) {
sum += term(k+i);
}
return sum;
}
int main() {
cout.precision(16);
int nterms = 0;
int ft = 0;
cout << "Enter first term, number of terms" << endl;
cin >> ft >> nterms;
cout << "The sum of the " << nterms << " terms starting at " << ft << endl;
cout << "direct_up: " << direct_up(ft, nterms) << endl;
return 0;
}
I have created a program which takes a formula and adds term by term starting from the kth term to the (n-1)th term. However I am unable to work out how to calculate the roundoff error after each term?
Would you be able help me with this please?
I'm making a program that prints all digits from an array (entered as an integer) and it works, but the digits are printed backwards and I don't know how to reverse them. Can someone help?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
cout << digit << '\n';
number /= 10;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
// numdigits = explode(n,digits);
cout << "[";
while (n > 0) {
int digit = n % 10;
n /= 10;
digits[digit] = digit;
cout << digits[digit];
}
cout << "]" << endl;
}
You just have to reverse the array using reverse() from <algorithm>.
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int array_c = 0;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
number /= 10;
array[array_c++] = digit;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
explode(n,digits);
reverse(digits,digits+array_c);
cout << "[";
for(int i = 0; i < array_c; ++i)
cout<<digits[i];
cout << "]" << endl;
}
Your use of
digits[digit] = digit;
is not right. You probably meant to use
digits[numdigits] = digit;
You can fix your problem by dividing the work into two steps. In the first step, you store the digits. In the second step, you print the digits.
int numdigits = 0;
while (n > 0) {
cout << "n: " << n << endl;
int digit = n % 10;
n /= 10;
digits[numdigits++] = digit;
}
// Make sure to print them in reverse order.
cout << "[";
for ( ; numdigits > 0; )
{
cout << digits[--numdigits];
}
cout << "]" << endl;
I am currently doing a task in a book which asks me to calculate the mathematical constant e using the while loop. I managed that fairly easily, however I am having troubles calculating e^x, whereas the user inputs x and the degree of accuracy. The code I used for computing e is:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
while (x <= degreeOfAccuracy)
{
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
e += (1/number);
x++;
}
cout << endl << "The mathematical constantr e is: "
<< setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
However, when I tried e^x the following code returned a completely wrong value:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1, exponent;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
cout << "Enter the number of which you wish to raise to e: ";
cin >> exponent;
int temp = exponent;
while (x <= degreeOfAccuracy)
{
exponent = temp;
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
int counterr = 1;
while (counterr < x)
{
exponent *= exponent;
counterr++;
}
e += (exponent/number);
x++;
}
cout << endl << "The mathematical constantr e is: " << setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
Any ideas where the calculations went wrong?
This line:
exponent *= exponent;
is wrong. It should be:
exponent *= temp;