Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Base 7 problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Base 7 problem solution Given an integer num, return a string of its base 7 representation.

Leetcode Base 7 problem solution

Leetcode Base 7 problem solution in Python.

class Solution:
    def convertToBase7(self, num: int) -> str:
        if num==0:
            return "0"
        s='';ms=0
        if(num<0):
            num=-num
            ms+=1
        while(num>0):
            s+=str(num%7)
            num//=7
        s=s[::-1]
        return s if ms==0 else '-'+s

Base 7 problem solution in Java.

class Solution {
    public String convertToBase7(int num) {
        if (num == 0) {
            return "0";
        }
        String res = "";
        boolean isNegative = num < 0;
        long abs = Math.abs(num);
        while (abs > 0) {
            long rem = abs % 7;
            res = rem + res;
            abs /= 7;
        }
        if (isNegative) {
            res = "-" + res;
        }
        return res;
    }
}

Problem solution in C++.

class Solution {
public:
    string convertToBase7(int num) {
        string result ="";
        if(num==0) return to_string(num);
        string sign = "";
        if(num<0){
            sign = "-";
            num = -num;
        }
        while(num){
            result= to_string(num%7) + result;
            num /=7;
        }
        return sign + result;
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes