Decimal to any Base | Solution

preview_player
Показать описание

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the solution where we are required to change a decimal number to a number of any other base.

Рекомендации по теме
Комментарии
Автор

Saara jahan k coding channels ek taraf aur pepcoding ek

csiddharth
Автор

Thank you sir for providing such short and precise videos

dishantyadav
Автор

Accepted Answer:

public static int getValueInBase(int num, int base){
String ans = "";
while (num > 0) {
ans += num % base;
num /= base;
}
return Integer.parseInt(new
}

kamalnayan
Автор

A simple trick for everyone after you put the code in your own ide either use Pieces OS or just copy it and put it in any AI and ask to explain it properly that really gives you insight like no one will be able to.

biswajeet
Автор

would this soultion work for hexadecimal conversion?

vedanshbisht
Автор

code is not working for base greater than 11 or it does not work at all?

rohitraj
Автор

Sir can we use recursion for this question

#include<iostream>
using namespace std;
void DecToAny(int n, int b) {
//write your code here
if(n==0)
{
return;
}

DecToAny(n, b);
cout << n%b << endl;
n = n/b;
}
int main() {
int n;
int b;
cin >> n;
cin >> b;
DecToAny(n, b);
}

Sir my code is not working but still this is the first approach that come in my mind.

apurbakumarmajumder
Автор

Sir how to handle negative base e.g. Convert to Base -2

sachintiwari-yesq
Автор

no need to take pow, we can intiate rv with 0 and while storing no rv=rv*10+dig;

abdullas
Автор

Not working for hexadecimal it returns 10 not A

rohitmore
Автор

sir what would be the time complexities of these conversions

palakgupta
Автор

Why my compiler is giving 111000? But when I submitted the solution, It got submitted successfully. Below is the code:

#include <bits/stdc++.h>
using namespace std;


int DecToAny(int n, int b) {

int sum = 0;
int raiseToPower = 0;

while (n) {
int val = n % b;
sum = sum + (val * pow(10, raiseToPower));
n = n / b;
raiseToPower++;

}

return sum;

}
int main() {
int n;
int b;
cin >> n;
cin >> b;
int res = DecToAny(n, b);
cout << res << endl;
}

ajaykmr
Автор

is this foundation course is enough or should we go for level up after completing this course

vaishalisharma
Автор

Sir maine pow use kr k kiya hai ye question kya ye approach shi hai??
#include <iostream>
#include <math.h>
using namespace std;

int decimalToAnyBase(int n, int b){
int sum = 0, count = 0;
while(n > 0){
int rem = n % b;
n /= b;
sum = sum + rem * pow(10, count);
count++;
}
return sum;
}
int main()
{
int n, b;
cin>>n>>b;
cout<<decimalToAnyBase(n, b);
return 0;
}

vaibhavmishra
Автор

Has anyone tried with 4658(decimal to binary)?

Mr.TechSpec
Автор

//same problem different way
import java.util.Scanner;

public class Base{

public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int b=sc.nextInt();
//int f=base(n, b);
System.out.println(base(n, b));
}
public static int base(int n, int b){
int rem, div, rev=0, i=1;
while(n>0){
rem=n%b;
div=n/b;
n=div;
rev+=rem*i;
i=i*10;

}
return rev;
}

}

mayankdhiman