Computer Science, asked by aman7790, 1 year ago

write a function with a parameter which is a positive number in base 10 ( any positive,non decimal number,like 452 )and converts it to binary and prints the results ( please research your self on how to convert a number in base 10 to a binary number)the fucntion does not return any thing ​

Answers

Answered by sswaraj04
1

Answer:

Explanation:

function binary(long n)

{

int rem;

long binary=0,i=1;

while (n!=0)

   {

       rem = n%2;

       n /= 2;

       binary+= rem*i;

       i=i*10;

   }

cout<<"binary value is"<<binary;

}

Logic is

find remainder every step and

now we take a variable with value 1 multiplying by 10 every step and at instance remainder is 1 we multiply it by value and add it to binary so it helps to store no. of zeroes we got in btwn

directly adding to 'binary' won't help bcoz 0 will be multiplied to it

Function segment is in C++ language

Hope it helps


aman7790: sry bt it make in python
Answered by rakeshchennupati143
1

Program:

import java.util.*;

public class Main{

     public static void main(String args[]){

           Scanner sc = new Scanner(System.in);

           System.out.println("Enter a positive non-decimal number : ");

           long num = sc.nextInt();

           String bi = DecToBin(num);

           System.out.println("Binary of "+num+" is "+bi);

     }

     public static String DecToBin(long dec){

           int rem = 0;

           String binary="";

           while(dec>0){

                 rem = dec%2;

                 binary = rem + binary;

                 dec = dec / 2;

           }

           return binary;

     }

}

Output:

Enter a positive non-decimal number : 452

Binary of 452 is 111000100

Explanation:

  • i used function parameter to take number into the function and calculate the binary for it
  • first i initialized num as "long" it can take input up to -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • if int type's range is not enough we will take long it will hold high numbers
  • so i passed taken num into a method called DecToBin()
  • in that i started loop with a condition that num should be greater than 0 to perform loop
  • in loop i took reminder of num%2 so that it will be either 1 or 0
  • and then i assigned that rem to a string variable so that it can handle big number which are bigger than long
  • and after that i divided the num /2 and the loop iterates by checking condition and adding 1's and 0's to the string at last the loop will break when the num gets to 0
  • then i returned the string that i used to store the binary of the num
  • see clearly i used return type as String in  public static String DecToBin(),
  • so that it will return string to the main function
  • in main i initialized a String "bi" and took the returned value into it and i printed it using print() method

-----Hope you got what you wanted, if you liked my program,mark it as brainliest, it would really help me .    :)


rakeshchennupati143: if you understood this mark my answer as brainliest, no offence sswaraj bro
sswaraj04: ok
rakeshchennupati143: bro have you seen my latest question on python program?
rakeshchennupati143: try to solve it, it's kind of tricky
sswaraj04: i tried it ,see if it's correct!!!
rakeshchennupati143: yea checkin it
aman7790: it wrong or ✔
rakeshchennupati143: the python program isent you in the comments is 100% correct
rakeshchennupati143: it will work
aman7790: ok thanx
Similar questions