C Program to Convert GB to MB

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

This program asks the user to enter the size in Gigabytes, then it converts the size into Megabytes using the following conversion factor:

1 Gigabyte = 1024 Megabytes

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

C Program to Convert GB to MB

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

Output

Enter the size in GB: 5
5 Gigabytes = 5120 Megabytes

How Does This Program Work ?

    long mb, gb;

We have declared two long data type variables named mb and gb.

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

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

    // GB to MB Conversion
    mb = gb * 1024;

We convert the size from GB to MB using the conversion factor, 1 Gigabyte = 1024 Megabytes.

    // Displaying output
    printf("%ld Gigabytes = %ld Megabytes", gb, mb);

We convert the size after conversion with the help of printf() function.

Conclusion

I hope after going through this post, you understand how to convert GB to MB using C Programming language.

If you have any doubt regarding the program, then 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 *