Write a program to convert a decimal number into a binary number.
Example:
Input: num = 10 Output: Binary number is :1010
Approach
C
#include <stdio.h>int main(){int num;printf("Enter a number: ");scanf("%d", &num);int binary[100];int i = 0;while (num > 0){int temp = num % 2;binary[i] = temp;num = num / 2;i++;}printf("Binary number is :");for (int j = i - 1; j >= 0; j--)printf("%d", binary[j]);return 0;}
Java
import java.util.Scanner;public class DecimalToBinary {public static void main(String[] args) {System.out.println("Enter a number");Scanner sc = new Scanner(System.in);int num = sc.nextInt();int binary[] = new int[100];int i = 0;while (num > 0) {int temp = num % 2;binary[i] = temp;num = num / 2;i++;}System.out.println("Binary number is :");for (int j = i - 1; j >= 0; j--)System.out.printf("%d", binary[j]);sc.close();}}
Related posts
No comments:
Post a Comment