Accepted Answer: public static int getValueInBase(int num, int base){ String ans = ""; while (num > 0) { ans += num % base; num /= base; } return Integer.parseInt(new StringBuilder(ans).reverse().toString()); }
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.
Sir can we use recursion for this question #include using namespace std; void DecToAny(int n, int b) { //write your code here if(n==0) { return; } DecToAny(n, b); cout n; cin >> b; DecToAny(n, b); } Sir my code is not working but still this is the first approach that come in my mind.
Why my compiler is giving 111000? But when I submitted the solution, It got submitted successfully. Below is the code: #include 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
//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;
Sir maine pow use kr k kiya hai ye question kya ye approach shi hai?? #include #include 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
Thankyou beta! If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )
Thank you sir for providing such short and precise videos
Glad you liked it!! pdhtee rhiee or bdtte rhiee..
@@Pepcoding yes sir
@@Pepcoding sir wat abt decimal to hexa?
Saara jahan k coding channels ek taraf aur pepcoding ek taraf......
Accepted Answer:
public static int getValueInBase(int num, int base){
String ans = "";
while (num > 0) {
ans += num % base;
num /= base;
}
return Integer.parseInt(new StringBuilder(ans).reverse().toString());
}
would this soultion work for hexadecimal conversion?
Thank you so much sir
So nice of you
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.
code is not working for base greater than 11 or it does not work at all?
Not working for hexadecimal it returns 10 not A
Post your queries on nados.pepcoding.com our community will help you out.
Bht sahi pal
Thank you so much
Sir can we use recursion for this question
#include
using namespace std;
void DecToAny(int n, int b) {
//write your code here
if(n==0)
{
return;
}
DecToAny(n, b);
cout n;
cin >> b;
DecToAny(n, b);
}
Sir my code is not working but still this is the first approach that come in my mind.
sir what would be the time complexities of these conversions
o(n) i think?
O(no. of digits in the number)
Thank you sir
Sir how to handle negative base e.g. Convert to Base -2
no need to take pow, we can intiate rv with 0 and while storing no rv=rv*10+dig;
Has anyone tried with 4658(decimal to binary)?
3:23
is this foundation course is enough or should we go for level up after completing this course
Level up is important, this only won’t suffice
Why my compiler is giving 111000? But when I submitted the solution, It got submitted successfully. Below is the code:
#include
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
Why mein maza aaya
//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;
}
}
Sir maine pow use kr k kiya hai ye question kya ye approach shi hai??
#include
#include
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
Yes
Present sir
Thank you sir
Thankyou beta!
If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )