06/02/2023

C Program To Generate IP Addresses(IPv4 & IPv6) Using For Loop

Write a C Program To Generate IP Addresses (Internet Protocol IPv4 & IPv6 ) Using For Loop. Generate IP Address in C. Logic is very simple first print a random Number so I use the rand() Function But always shows the same value if you run your code again and again it shows the same or repeat Output that's why I use stand(). So to print IPv4 we need to print multiple of 4 random numbers cause we need output in this format.

C Program To Generate IP Addresses

IPv4: What is IPv4 Format?

64.92.8.122 This is an IPv4 Format or Number from 0 to 255 or you can say 0.0.0.0 to 255.255.255.255 Below Code is Printing an IPv4.


srand(time(NULL));
for (i = 0; i < no; i++) {
    for (j = 0; j < 4; j++) {
        cnt = rand() % 255;
        printf("%d", cnt);
        if (j < 3)
            printf(".");
    }
    printf("\n");
}

IPv6: What is IPv6 Format?

5677.a66c.4410.cc07.8487.8a9a.bd83.7e05 This is an IPv6 Format or we also use hexadecimal Numbers IPv6 that's a reason IPv6 is More Secure Than IPv4. Below Code For IPv6 But we also Use A Character array See the Full Code.


srand(time(NULL));
for (i = 0; i < no; i++) {
    for (j = 0; j < 8; j++) {
        for (k = 0; k < 4; k++) {
            ch = str[rand() % 16];
            printf("%c", ch);
        }
        if (j < 7)
            printf(".");
    }
    printf("\n");
}

Read: C Program To Print A Calendar By Taking Input From User Using Loop

C Program To Generate IP Addresses


#include<stdio.h>

#include<string.h>

#include<math.h>

void main() {
    /*C Program To Generate IP Addresses*/
    int i, j, k, dig, no, nu, cnt = 0;
    char str[] = {
        '0',
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        'a',
        'b',
        'c',
        'd',
        'e',
        'f'
    };
    char ch;

    printf("\nEnter How Many IP Addresses You Want To Print: ");
    scanf("%d", & no);

    printf("\n\nEnter 4 for IPv4 and 6 for IPv6: \n");
    scanf("%d", & nu);

    switch (nu) {
    case 4:
        srand(time(NULL));
        for (i = 0; i < no; i++) {
            for (j = 0; j < 4; j++) {
                cnt = rand() % 255;
                printf("%d", cnt);
                if (j < 3)
                    printf(".");
            }
            printf("\n");
        }
        break;
    case 6:
        srand(time(NULL));
        for (i = 0; i < no; i++) {
            for (j = 0; j < 8; j++) {
                for (k = 0; k < 4; k++) {
                    ch = str[rand() % 16];
                    printf("%c", ch);
                }
                if (j < 7)
                    printf(".");
            }
            printf("\n");
        }
        break;
    default:
        printf("\nEnter IPv Either 4 or 6\n\n");
    }
}

IPv4- IP Address


IPv4 Address Program in C

IPv6- IP Address


IPv6 Address Program in C
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: