Wednesday, 6 April 2016

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

Problem :- 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 .

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 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 .

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");
}

Try Yourself C Program To Print A Calendar By Taking Input From User Using Loop

Solution :-

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
  //Ghanendra Yadav
    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 :-
C Program To Generate IP Addresses (Internet Protocol) Using For Loop
IPv6 :-
C Program To Generate IP Addresses (Internet Protocol) Using For Loop

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment