C Program to Convert KB to MB

In this post, we will learn how to convert KB to MB using C Programming language.

As we know, 1 Megabyte = 1024 Kilobyte, so we will use this conversion factor in this program to convert the size from KB to MB.

So, without any delay, let’s begin this tutorial.

C Program to Convert KB to MB

// C Program to Convert KB to Mb
#include <stdio.h>
int main(){
    long kb, mb;
    
    // Asking for input
    printf("Enter the size in kilobytes: ");
    scanf("%ld", &kb);
    
    // KB to MB
    mb = kb / 1024;
    
    // Displaying output
    printf("%ld Kilobytes = %ld Megabytes", kb, mb);
    
    return 0;
}

Output

Enter the size in kilobytes: 2048
2048 Kilobytes = 2 Megabytes

How Does This Program Work ?

    long kb, mb;

In this program, we have declared two long data type variables named kb and mb.

    // Asking for input
    printf("Enter the size in kilobytes: ");
    scanf("%ld", &kb);

Then, the user is asked to enter the size in kilobytes.

    // KB to MB
    mb = kb / 1024;

We convert the size from KB to MB using the conversion factor:

1024 KiloBytes = 1 MegaByte

The size in megabytes gets stored in the mb named variable.

    // Displaying output
    printf("%ld Kilobytes = %ld Megabytes", kb, mb);

At the end, the converted size is displayed on the screen using printf() function.

Conclusion

I hope after going through this post, you understand how to convert KB to Mb 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 assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *