Write a program to print Binary number using for loop | electro4u

28 Dec 2022 Balmiki Mandal 0 C Programming

Print Binary of any number using for loop in C

step-by-step guide on how to write a C program to print the binary representation of any given number using a for loop.

Introduction

function that takes an integer as an input and returns its binary representation as an output  using a for loop

Steps to Print Binary of a Number

Step 1: Include Necessary Headers

#include 

Step 2: Define the Main Function

int main() {
    // Code will go here
    return 0;
}

Step 3: Declare Variables

Declare a variable to store the input number.

int num;

Step 4: Take User Input

Prompt the user to enter a number.

printf("Enter a number: ");
scanf("%d", &num);

Step 5: Initialize a for Loop

for (i = 31; i >= 0; i--) {
        if (num & (1 << i))
            printf("1");
        else
            printf("0");
    }

Step 6: Extract Each Bit

Inside the loop, use bitwise operators to extract each bit of the number.

num>>pos&1

Step 7: Print the Binary Representation

printf("%d", bit);

Step 8: Complete the Program

Complete the program by putting it all together.

Program 01: Printing Binary of Any Number Using a For Loop in C without using function 

#include
 int main()
 {
 int pos, num;
 printf("Enter the number:");
 scanf("%d",&num); 
for(pos=31;pos>=0;pos--)
 {
 printf("%d",num>>pos&1);
 } 
printf("\n");
 }
Output:
Enter the number:5
0000 0000 0000 0000 0000 0000 0000 0101
  1. The program starts by declaring two integer variables pos and num.
  2. The user is prompted to enter a number using printf() and scanf().
  3. The program then enters a for loop that iterates from 31 to 0, representing the 32 bits in an integer.
  4. Inside the for loop, the program uses bitwise operators to extract the value of each bit in the input number. Specifically, it uses the right shift operator >> to shift the bits of the input number to the right by pos positions, and then uses the bitwise AND operator & with 1 to extract the value of the rightmost bit.
  5. The program then prints the value of the bit using printf().
  6. After the for loop completes, the program prints a newline character using printf() to separate the binary representation from any subsequent output.

Program 02: Printing Binary of Any Number Using a For Loop in C using function 

#include
#include
void bin(int);
int main(void) 
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    printf("Binary representation of %d is:\n", num);
    bin(num);
    return 0;
}
void bin(int num) {
    int i;
    for (i = 31; i >= 0; i--) {
        if (num & (1 << i))
            printf("1");
        else
            printf("0");
    }
    printf("\n");
}
Enter an integer: 10
Binary representation of 10 is:0000 0000 0000 0000 0000 0000 0000 1010

Attention: In this function, we use a for loop that iterates over 32 bits (the maximum number of bits required to represent an integer in C) and checks the value of each bit using the bitwise AND operator & and the left shift operator <<. If the result of the bitwise AND operation is non-zero, it means that the bit is set and the character '1' is printed, otherwise the character '0' is printed. The final binary representation of the input number is obtained by printing the bits in reverse order, from the most significant bit to the least significant bit.

Overall, this program provides a simple way to convert an integer to its binary representation using a for loop and bitwise operators.

Conclusion

You have now learned how to write a C program to print the binary representation of any given number using a for loop. This program can be a useful tool in understanding binary representation in computer science and programming.

Further Reading:


[Design a function to print the number into binary formate in c]

[Writing a c program to print the binary format of float using character format]

[Write a program to print the binary formate of float using integer pointer in c]

[Write a program to print Binary number using for loop]

[printing the Binary Format of any Floating number using union]

[printing the binary of any integer number using union in c | electro4u]

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

Top Resources


Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.