Thursday, March 19, 2026

Write a program that shows the binary equivalent of a given positive number between 0 to 255 || C Lab Program

 WAP_A04: Write a program that shows the binary equivalent of a given positive number between 0 to 255 || Simple Numeric Problems


Algorithm

start
step1: Read number
step2: if (number < 0 OR number > 255) then
step3: Print "Number is out of range." stop
step4: for i:=0 to number ≠ 0 step 1 do
step5:  bit[i] := number % 2
step6: number := number / 2
step7: Print "Binary equivalent"
step8: for j := i-1 to j ≥ 0 step -1 do
step9: print bit[j]
stop


WAP_A04: C Lab Program


//Binary equivalent of a given positive number between 0 to 255.


#include <stdio.h>

int main() {
    static int number,bit[8],i,j;

    // Prompt the user for input
    printf("Enter a number: ");
    scanf("%d", &number);

    // Validate the input
    if (number < 0 || number > 255) {
        printf("Number is out of range.\n");
        return 1; // Exit with error code
    }

  for(i=0;number!=0;i++)
  {
      bit[i]=number%2;
      number=number/2;
  }
  printf("Binary equivalent: ");
  for(j=i-1;j>=0;j--)
    printf("%d",bit[j]);
printf("\n");
    return 0; // Successful execution
}


OUTPUT

Enter a number: 56
Binary equivalent: 111000

For all 2026 published articles list: click here

...till the next post, bye-bye & take care.

No comments:

Post a Comment