Monday, November 24, 2014

C Binary using switch pro



C Program to Convert Hexadecimal to Binary

 
#include 
#include
#define Max 1000
int main()
{
char binarynum[Max], hexa[Max];
long int i = 0;
printf("Enter the value for hexadecimal ");
scanf("%s", hexa);
printf("\n Equivalent binary value: ");
while (hexa[i])
{
switch (hexa[i])
{
case '0':
printf("0000"); 
break;
case '1':
printf("0001");  
break;
case '2':
printf("0010");  
break;
case '3':
printf("0011"); 
break;
case '4':
printf("0100");  
break;
case '5':
printf("0101"); 
break;
case '6':
printf("0110"); 
break;
case '7':
printf("0111");  
break;
case '8':
printf("1000"); 
break;
case '9':
printf("1001"); 
break;
case 'A':
printf("1010"); 
break;
case 'B':
printf("1011");  
break;
case 'C':
printf("1100");  
break;
case 'D':
printf("1101"); 
 break;
case 'E':
printf("1110");  
break;
case 'F':
printf("1111"); 
break;
case 'a':
printf("1010");  
break;
case 'b':
printf("1011");  
break;
case 'c':
printf("1100"); 
break;
case 'd':
printf("1101");  
break;
case 'e':
printf("1110"); 
break;
case 'f':
printf("1111");  
break;
default:
printf("\n Invalid hexa digit %c ", hexa[i]);
return 0;
}
i++;
}
return 0;
}
 
Output
 
Enter the value for hexadecimal ab
Equivalent binary value: 10101011

C Program to Convert Octal to Binary

 
#include 
#include
#define Max 1000
int main()
{
char octalnum[Max];
long i = 0;
printf("Enter any octal number: ");
scanf("%s", octalnum);
printf("Equivalent binary value: ");
while (octalnum[i])
{
switch (octalnum[i])
{
case '0':
printf("000"); 
break;
case '1':
printf("001");  
break;
case '2':
printf("010"); 
break;
case '3':
printf("011");  
break;
case '4':
printf("100");  
break;
case '5':
printf("101");  
break;
case '6':
printf("110");  
break;
case '7':
printf("111");  
break;
default:
printf("\n Invalid octal digit %c ", octalnum[i]);
return 0;
}
i++;
}
return 0;
}
 
Output
 
Enter any octal number: a
Equivalent binary value:
Invalid octal digit a
Enter any octal number: 160
Equivalent binary value: 001110000

C Program to Print Diamond Pattern

 
#include 
#include   
int main()
{
int number, i, k, count = 1;
printf("Enter number of rows\n");
scanf("%d", &number);
count = number - 1;
for (k = 1; k <= number; k++)
{
for (i = 1; i <= count; i++)
printf(" ");
count--;
for (i = 1; i <= 2 * k - 1; i++)
printf("*");
printf("\n");
}
count = 1;
for (k = 1; k <= number - 1; k++)
{
for (i = 1; i <= count; i++)
printf(" ");
count++;
for (i = 1 ; i <= 2 *(number - k)-  1; i++)
printf("*");
printf("\n");
}
return 0;
}
 
Output
 
Enter number of rows
5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

C Program to Display the IP Address of the System

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main()
{
int n;
struct ifreq ifr;
char array[] = "eth0";
n = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , array , IFNAMSIZ - 1);
ioctl(n, SIOCGIFADDR, &ifr);
close(n);
printf("IP Address is %s - %s\n" , array , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
return 0;
}
 
Output
 
IP Address is eth0 - 192.168.225.135


No comments:

Post a Comment