Q:

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

0

Write a C Program To Generate IP Addresses (Internet Protocol IPv4 & IPv6 ) Using For Loop .

 

Logic : 

Logic is very simple first print random Number so i use rand() Function But always shows same value if you run your code again and again it shows same or repeat Output that's why i use srand() .

so for print IPv4 we need to print multiple of 4 random number cause we need output in this format 
 
IPv4 :- What is IPv4 Format ?
 
64.92.8.122 This is a 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 a IPv4 .
 
 
Pv6 :- What is IPv6 Format ?
 
5677.a66c.4410.cc07.8487.8a9a.bd83.7e05 This is a IPv6 Format or we also use hexadecimal Number IPv6 thats a reason IPv6 is More Secure Than IPv4 .Below Code For IPv6 But we also Use A Character array See the Full Code .

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
    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\nWhat You Want ipv4 Or Ipv6 Enter (4 Or 6) :");
    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"); 
 }
    
    getch();
}

 

 

Output:

IPv4:

Enter How Many IP Addresses you Want To Print :10

What you Want ipv4 Or ipv6 Enter (4 or 6):4

77.99.252.203

64.92.8.122

161.197.253.87

200.68.72.231

145.162.7.252

51.2.221.222

13.226.35.192

20.253.34.157

139.43.189.206

146.59.174.112

IPv6:

Enter How Many IP Addresses You Want To Print :10

What You Want ipv4 Or Ipv6 Enter (4 Or 6) :6

7c8c.d103.bf59.3940.c1c2.b70b.ba2b.a601

28e0.9e35.e8e1.231e.4d1f.41af.bda6.3b76

356d.4922.1033.3427.1366.4150.e061.bd7e

3db7.6d98.ecb1.0d91.0f75.0d5e.db08.876b

522b.fb3d.7ff7.c89d.7028.d76a.262a.e963

b8ea.428b.173d.fca6.ccea.3545.b709.06cb

eb52.ddee.41b3.d6a9.2836.d8b9.fb2f.1fa0

af27.d051.105e.6f89.7bf5.3ae2.6017.fb79

ba08.a59c.6eac.d255.e4a1.f845.85c8.141c

e248.7d4d.cfa9.1fef.4813.0589.a41b.8276

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now