I am getting different results while using different compilers - c++

While trying out a question related to armstrong number i am getting different answers in VS-Code and Programiz online compiler https://www.programiz.com/cpp-programming/online-compiler/ ..
#include<iostream>
#include<math.h>
using namespace std ;
int main(){
int number ;
cin >> number ;
int sum = 0;
int original = number;
while (number > 0)
{
int element ;
element = number%10 ;
sum = sum + pow(element,3) ;
number = number/10 ;
}
cout << sum << endl ;
cout << number << endl ;
cout << original << endl ;
if (sum == original)
{
cout << "It is an Armstrong number " << endl ;
}
else cout << "It is not an Armstrong number " << endl ;
}
This was the output on vs code .
This was the output on programiz compiler ...
Pls do enlighten as to why is this happening and how can this be corrected ..

Write your own integer power function e.g.
(the constexpr allows the static_assert tests at compile time)
#include <cmath>
#include <iostream>
#include <type_traits>
static constexpr unsigned int int_pow(unsigned int value, unsigned int power)
{
int result{ 1 };
for (int i = power; i > 0; --i)
{
result *= value;
}
return result;
}
int main()
{
static_assert(int_pow(10, 3) == 1000);
static_assert(int_pow(2, 3) == 8);
return 0;
}

Related

C++ if statement not printing desired output

Problem is with the if statment inside the while loop. It is not printing the desired output. The else if statement and the else statement seem to work fine
Any help is appreciated
#include <iostream>
using namespace std;
/*
Write a C++ program that asks the user for an integer.
The program finds and displays the first power of 3
larger than the input number using while
*/
int main() {
int input = 0;
int base = 3;
int exponent = 0;
int sum = 1;
cout << "Enter a number: ";
cin >> input;
while (sum < input) {
// This is the if statement giving me problems
if (input == 1) {
exponent += 1;
sum = 3;
}
// This else if statement seems to work fine
else if (input == 3) {
exponent += 2;
sum = 9;
}
else {
exponent++;
sum *= base;
}
}
// Print output
cout << "3 to the power of " << exponent << " is equal to " << sum;
cout << endl << "It is the first power of 3 larger than " << input;
return 0;
}
Your logic is wrong (and I have to say a bit bizarre).
If the input is 1 then while (sum < input) is not true and so you never reach your if (input == 1) statement.
REALIZED my mistake. i just moved the if and else if statement to outside the loop
#include <iostream>
using namespace std;
/*
Write a C++ program that asks the user for an integer.
The program finds and displays the first power of 3
larger than the input number using while
*/
int main() {
int input = 0;
int base = 3;
int exponent = 0;
int sum = 1;
cout << "Enter a number: ";
cin >> input;
if (input == 1) {
exponent += 1;
sum = 3;
}
else if (input == 3) {
exponent += 2;
sum = 9;
}
while (sum < input) {
exponent++;
sum *= base;
}
cout << "3 to the power of " << exponent << " is equal to " << sum;
cout << endl << "It is the first power of 3 larger than " << input;
return 0;
}
If I understood the objective right from the comments, if conditions are not required. Just replace the condition and simplify the while loop as follows:
while (sum <= input) {
exponent++;
sum *= base;
}
Write a C++ program that asks the user for an integer. The program
finds and displays the first power of 3 larger than the input number
using while
You should probably calculate the answer instead of looping.
#include <iostream>
#include <cmath>
int main() {
int input;
std::cout << "input: ";
std::cin >> input;
int x = 0;
/*
3^x == input
ln(3^x) == ln(input)
x*ln(3) == ln(input)
x == ln(input)/ln(3)
*/
// calculate x = ln(input)/ln(3), round down and add 1
if(input > 0) x = std::floor(std::log(input) / std::log(3.)) + 1.;
std::cout << "answer: 3^" << x << " == " << std::pow(3, x) << "\n";
}

Not taking the input

I want to write a program that only takes odd numbers, and if you input 0 it will output the addition and average, without taking any even number values to the average and the addition. I'm stuck with not letting it take the even values..
Heres my code so far:
int num = 0;
int addition = 0;
int numberOfInputs = 0;
cout << "Enter your numbers (only odd numbers), the program will continue asking for numbers until you input 0.." << endl;
for (; ;) {
cin >> num;
numberOfInputs++;
addition = addition + num;
if (num % 2 != 0) {
//my issue is with this part
cout << "ignored" << endl;
}
if (num == 0) {
cout << "Addition: " << addition << endl;
cout << "Average: " << addition / numberOfInputs << endl;
}
}
Solution of your code:
Your code doesn't working because of following reasons:
Issue 1: You adding inputs number without checking whether it's even or not
Issue 2: If would like skip even then your condition should be as follow inside of the loop:
if (num%2==0) {
cout << "ignored:" <<num << endl;
continue;
}
Solving your issues, I have update your program as following :
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num = 0;
int addition = 0;
int numberOfInputs = 0;
cout << "Enter your numbers (only odd numbers), the program will continue asking for numbers until you input 0.." << endl;
for (; ;) {
cin>> num;
if (num%2==0) {
cout << "ignored:" <<num << endl;
continue;
}
numberOfInputs++;
addition = addition + num;
if (num == 0) {
cout << "Addition: " << addition << endl;
cout << "Average: " << addition / numberOfInputs << endl;
break;
}
}
}
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int number;
int sum=0;
int average=0;
int inputArray[20]; // will take only 20 inputs at a time
int i,index = 0;
int size;
do{
cout<<"Enter number\n";
cin>>number;
if(number==0){
for(i=0;i<index;i++){
sum = sum + inputArray[i];
}
cout << sum;
average = sum / index;
cout << average;
} else if(number % 2 != 0){
inputArray[index++] = number;
} else
cout<<"skip";
}
while(number!=0);
return 0;
}
You can run and check this code here https://www.codechef.com/ide
by providing custom input

Reversing a number C++

Looking for some advice here on what I'm getting wrong. Everything in my main should be fine and left unchanged. My problem is in my reverse function. It's printing the reversed number right before the cout statement of "The number is" instead down below where it should be. I spent awhile trying to fix but can't come up with a solution.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NUM_VALS = 10; //the maximum number of values to use
int reverse(int num);
bool isPrime(int num);
int main()
{
int number, //Holds the random number that is manipulated and tested
loopCnt; //Controls the loop
//set the seed value for the random number generator
//Note: a value of 1 will generate the same sequence of "random" numbers every
// time the program is executed
srand(1);
//Generate 10 random numbers to be manipulated and tested
for( loopCnt = 1; loopCnt <= NUM_VALS; loopCnt++ )
{
//Get a random number
number = rand();
//Display the sum of adding up the digits in the random number, the reversed
//random number, and whether or not the number is palindromic or a prime number
cout << "The number is " << number << endl
<< "----------------------------------------" << endl
// << "Adding the digits result" << setw(16) << sumDigits( number ) << endl
<< "Reversing the digits result" << setw(13) << reverse(number) << endl
// << "Is the number a palindrome?" << setw(13) << (isPalindrome(number)? "Yes" : "No") << endl
// << "Is the number prime?" << setw(20) << (isPrime(number)? "Yes" : "No") << endl
<< endl << endl;
}
return 0;
}
int reverse(int num)
{
int quo, rem;
quo = num;
while (quo != 0)
{
rem = quo % 10;
cout << rem;
quo /= 10;
}
}
bool isPrime(int num)
{
int i;
if (num % 2 == 0)
return false;
for (i = 3; i*i <= num; i+=2)
{
if (num % i == 0)
return false;
}
return true;
}
You need to have your reverse function return the number as reversed, because the return value is used in main.
You can build the reversed number by multiplying a "reversed" value by 10, then adding in the remainder:
int reverse(int num)
{
int reversed = 0;
int quo, rem;
quo = num;
while (quo != 0)
{
rem = quo % 10;
reversed = reversed * 10 + rem;
quo /= 10;
}
return reversed;
}
You can also use this method to reverse a number by taking string input and then reverse it and convert it to int.
#include <iostream>
#include<string>
using namespace std;
int reverse_num(string a)
{
string s;
for(int i=a.length()-1;i>=0;i--)
{
s+=a[i];
}
int n;
n=stoi(s);
return n;
}
int main()
{
string a;
cin>> a;
cout<<reverse_num(a);
return 0;
}

fibonacci c++ gmp generator

Hello I am attempting to implement gmp to an existing fibonacci generator I wrote. I have been reading over the gmp documentation but there is still much I do not understand. The original fibonacci generator is here:
#include <iostream>
using namespace std;
class Fib {
int n;
long unsigned int first, second;
public:
Fib() {
first = 0;
second = 1;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are:" << endl;
}
int solve() {
int i;
long unsigned int next;
for(i = 0 ; i < n + 1 ; i++) {
if(i <= 1) {
next = i;
}
else {
next = first + second;
first = second;
second = next;
}
}
return next;
}
};
int main() {
Fib fib;
cout << fib.solve() << endl;
return 0;
}
I installed gmp using:
sudo apt-get install libgmp3-dev
When I try to implement gmp I did this:
#include <iostream>
#include <gmpxx.h>
using namespace std;
class Fib {
int n;
mpz_class first, second;
public:
Fib() {
first = 0;
second = 1;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are:" << endl;
}
int solve() {
int i;
mpz_class next;
for(i = 0 ; i < n + 1 ; i++) {
if(i <= 1) {
next = i;
}
else {
next = first + second;
first = second;
second = next;
}
}
return next;
}
};
int main() {
Fib fib;
cout << fib.solve() << endl;
return 0;
}
I know at some point I need to convert from a int to a string, then clear the output variable or something like that. when I attempt to compile I run:
g++ -lgmpxx -lgmp fib.cpp -o fib
my output:
fib.cpp: In member function ‘int Fib::solve()’:
fib.cpp:30:12: error: cannot convert ‘mpz_class {aka __gmp_expr<__mpz_struct [1], __mpz_struct [1]>}’ to ‘int’ in return
return next;
^
I am a complete noob to bignum libraries, any help would be great. I am reading the documentation but I am struggling with implementing it.
Solved, thank you Marc Glisse for pointing me in the right direction!
I simply removed the function return and allowed the function to just return the ouput.
#include <iostream>
#include <gmpxx.h>
using namespace std;
class Fib {
int n;
public:
Fib() {
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "The " << n << "'st Fibonacci number is:" << endl;
}
void solve() {
int i;
mpz_class first, second, next;
first = 0;
second = 1;
for(i = 0 ; i < n + 1 ; i++) {
if(i <= 1) {
next = i;
}
else {
next = first + second;
first = second;
second = next;
}
}
cout << next << endl;
}
};
int main() {
Fib fib;
fib.solve();
return 0;
}
output:
Enter the number of terms of Fibonacci series you want
3301
First 3301 terms of Fibonacci series are:
330153163507162264637094778670152653434758914922281728912670042596222213549775330156165336158736310556035302724174567603559968964146698655928480718496410717009709564103992213321320869628734803460669663152332798570186240768164370808688660485835985642189726235311578136722218902035069558368032277843436948382319806290480685283349217035498351102885889468646619750569482644246863804467015344937199892515242806415403581786532923017170033416624774209919795051514102027827396052441847160310846646083321110222356075543424672128051593137886359425865994528848747739182600228659941846983982384323813903695048726976986370288741982958687841091743740983161275336114608885705665822704734020694899622487801
ubuntu#ubuntu:~/projects/c++/fibonacci_cpp$

C++ Sum of factorial -Request 2nd code

I would like to analyze the complexity of my code algorithm.Therefore,i must have 2 different programs giving the same functions to allow me to start off.
Currently this is my own code.
I'm not sure if it is allowed that i would like to have someone that could volunteer his own way code to compute summation of factorial for me as the 2nd program code.
Preferrably a nested loop.
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++)
{
c = c * i;
a = a + c;
}
cout << "The sum of the factorials is " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
static const int results[] = {
0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113,
4037913, 43954713, 522956313
};
cout << "The sum of the factorials is " << results[val < 0 ? 0 : val] << endl;
system("pause");
return 0;
}
Note that I replicated the defect in the original program which causes it to return the incorrect value if the user enters 0.
This alternate version assumes 32-bit integers because it takes advantage of overflow behavior. Extending to 64-bit integers is left as an exercise.
I do not understand what you do with another nested way but i hope this can help...
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++){
c *= i;
a += c;
}
int c2=1;
for (i = val; i > 1; i--){
c2*=i;
c2++;
}
cout << "The sum of the factorials is " << a << endl;
cout << "The sum of the factorials is " << c2 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int suma = 0;
int n = 0;
cout << "Sum of factorials\n";
cout << "-------------------------------\n";
cout << "Insert number of n: ";
cin >> n;
int i = 1;
while (i <= n)
{
int factorial = 1;
for(int j=1; j<=i; j++)
{
factorial = factorial * j;
}
suma += factorial;
i++;
}
cout << "Sum of factorials is: " << suma;
system("pause");
return 0;
}