In this post, we will learn how to convert bytes to kilobytes using the C programming language.
A Byte is a unit of computer information or data storage capacity that consists of a group of eight bits and that is used especially to represent an alphanumeric character.
- 1 Byte = 8 Bits
- 1 Kilobyte = 1024 Bytes
We will be using the above values for the conversion.
So, without further ado, let’s begin this tutorial.
C Program to Convert Bytes to Kilobytes
// C Program to Convert Bytes to Kilobytes #include <stdio.h> int main(){ int bytes; double kilobytes; // Asking for input printf("Enter the no. of bytes: "); scanf("%d", &bytes); // Bytes to Kilobytes kilobytes = bytes / 1024; // Displaying output printf("%d Bytes = %.2lf Kilobytes", bytes, kilobytes); return 0; }
Output
Enter the no. of bytes: 2048
2048 Bytes = 2.00 Kilobytes
How Does This Program Work ?
int bytes; double kilobytes;
In this program, we have declared one int data type and one double data type variable.
// Asking for input printf("Enter the no. of bytes: "); scanf("%d", &bytes);
Then, the user is asked to enter the no. of bytes.
// Bytes to Kilobytes kilobytes = bytes / 1024;
Now, we convert the bytes to kilobytes by dividing total no. of bytes by 1024. This gives us the value of kilobyte.
// Displaying output printf("%d Bytes = %.2lf Kilobytes", bytes, kilobytes);
Finally, the result is printed using printf() function.
Conclusion
I hope after going through this post, you understand how to convert bytes to kilobytes using C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: