C++ Program to convert any base(2-9) to decimal - c++

This is the simple program to convert any base(2-9) to decimal.
It is completely compiled, but the output is not what I expected.
Please let me know what's the problem.
#include <iostream>
#include <cmath>
using namespace std;
int size_int(int num) //the number of num
{
int n = 0;
while(num > 0)
{
num = num/10;
n = n+1;
}
return n;
}
int convert(int num, int b) //conver num base b to base 10
{
int sum = 0;
int n = size_int(num);
while(n>0)
{
sum += (num/pow(10,n-1)) * (pow(b,n-1));
n -= 1;
}
return sum;
}
int main()
{
int num;
cout << "Enter a number to convert: ";
cin >> num;
int b;
cout << "Enter the base of number: ";
cin >> b;
int sum;
sum = convert(num, b);
cout << "Its decimal value is: "<< sum << endl;
return 0;
}
Enter a number to convert: 10110
Enter the base of number: 2
Its decimal value is: 12632
This is the result when I executed.

You calculated your current digit seems not right num/pow(10,n-1).
This is my suggestion:
#include <iostream>
#include <cmath>
using namespace std;
int size_int(int num) //the number of num
{
int n = 0;
while (num > 0)
{
num = num / 10;
n = n + 1;
}
return n;
}
int convert(int num, int b) //conver num base b to base 10
{
int sum = 0;
int n = size_int(num);
int counter = 0;
while (num > 0) {
int temp = num % 10;
num = num / 10;
sum += temp * (pow(b, counter));
counter++;
}
return sum;
}
int main()
{
int num;
cout << "Enter a number to convert: ";
cin >> num;
int b;
cout << "Enter the base of number: ";
cin >> b;
int sum;
sum = convert(num, b);
cout << "Its decimal value is: " << sum << endl;
return 0;
}

Related

How to show only the last value of a for loop?

I'm working on building a loop right now that only shows the end value. However, the code is showing all the integers though.
Here's my code:
#include <iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
cout << "Sum: "<<(sum =sum+i)<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
You are using for then if then showing output. The for and if scope area is one line without { }, so you are printing and summing at the same time and it is each time in the scope of if statement.
#include<iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
sum =sum+i;
cout << "Sum: "<<sum<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
Your cout statements are in the wrong places. Try this instead:
#include <iostream>
using namespace std;
int findMultiples(int n){
int sum = 0;
for(int i = 0; i <= n; i++){
if (i % 3 == 0)
sum += i;
}
return sum;
}
int main() {
cout << "Enter a positive integer:" << endl;
int num;
cin >> num;
cout << "Sum: " << findMultiples(num) << endl;
return 0;
}

Returning stored array values from functions in C++

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.

How to correct this program?

I tried to inverse the number. Ex: 243 > 342
its my quiz from school (Not to be graded)
#include <iostream>
using namespace std;
int main()
{
int n, reverse, rem;
cout << "Enter a number: ";
cin >> n;
while (n != 0)
{
rem = n % 10;
reverse = reverse / 10 + rem;
n /= 10;
}
cout << "Reversed Number: " << reverse << endl;
return 0;
}
You should do reverse = reverse* 10 + rem; instead of division:
#include <iostream>
using namespace std;
int main()
{
int n = 0, reverse = 0, rem = 0;
cout << "Enter a number: ";
cin >> n;
while (n != 0)
{
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
cout << "Reversed Number: " << reverse << endl;
return 0;
}
you can try this
#include <iostream>
using namespace std;
int main()
{
int n, reverse=0, rem;
cout<<"Enter a number: ";
cin>>n;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
cout<<"Reversed Number: "<<reverse<<endl;
return 0;
}

Program prints all digits from array, prints backwards

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;

LCM program in C++

#include <iostream>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put
a space between them):\n";
cin >> num1 >> num2;
const int num_limit = num1 * num2;
for (int i = 1; i <= num_limit; i++)
{
int product = num1 * i;
int product2 = num2 * i;
// test for when multiples are equal
if (product == product2)
{
cout << "The LCM of " << num1 << " and " <<
num2 << " is: ";
}
}
}
I'm trying to get the LCM from two integers which the user enters. The if statement within the for loop isn't doing what I intended it to do, as nothing prints when product and product2 are equal. What is the solution to this little problem?
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int lcm(int a,int b)
{
//Efficient Solution
// a*b=gcd(a,b)*lcm(a,b)
return (a*b)/gcd(a,b);
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int a,b;
cin>>a>>b;
cout<<lcm(a,b);
return 0;
}
Output
15 12
60
Try this out
//#include "stdafx.h"
#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put a space between them):\n";
cin >> num1 >> num2;
int num_limit = num1 * num2,LCM;
for (int i = 1; i <= num_limit; i++)
{
LCM = num1 * i;
// test for when multiples are equal
if (LCM%num2==0) break;
}
cout << "The LCM of " << num1 << " and " << num2 << " is: " << LCM;
_getch();// replace with getch(); if didn't work
}