General code 3
CODE NO:- 18
Q) C program to convert a binary number to octal number.
Input:
101
Output:
5
Input:
11010
Output:
32
Test Case:
-------------
(1) Valid Input:
(a) Only number consisting of 0s and 1s will be given as input
(2) Invalid inputs:
(a) Decimal (b) Fraction (c) String (d) Two or more command-line arguments (e) Negative number
(3)You should generate output as follows:
(a) For right output print just the actual Octal Value to STDOUT without any other text.
(b) If any error: print 'ERROR' to the STDOUT without any other text.
#LOGIC
First, we need to convert the Binary no. into in Decimal no. then convert the decimal no. to octal no. .
#Program( In C):
In C language we use ' // ' for Single line comment and /*---*/ for multi line comment
/* Binary TO octal */
#include<stdio.h> // for standard i/p and o/p
#include<math.h> //for pow() function
#include<stdlib.h> //for atoi() and exit() function
int main(int ar,char *argv[])
{
long int n,r,dec=0,oct=0,i=0,j;
/* argv[0] is the execution file name */
n=atoi(argv[1]); //atoi stands or ascii to integer
printf("\nNo.of command line i/p are :%d",ar);
if(ar>=3)
{
printf("\nmore than one command line input");
exit(0);
}
/* printf("\n value of argv[1]:%s",argv[1]); // to print whole value of argv[1]
printf("\n value of argv[1][0]:%c",argv[1][0]); // to print first character of argv[1]
printf("\n ascii value of argv[1][0]:%d",(int)argv[1][0]); //to print ascii value of first character*/
for(j=0;argv[1][j]!='\0';j++) // As every string terminates by \0 (backslash zero)
{
if(argv[1][j]=='.')
{
printf("\ndecimal value are not accepted");
exit(0);
}
if(argv[1][j]=='-')
{
printf("\nnegative value are not accepted");
exit(0);
}
if(argv[1][j]=='/')
{
printf("\nfraction value are not accepted");
exit(0);
}
if(50<=argv[1][j] && argv[1][j] <=57)
{
printf("\nInteger value are not accepted");
exit(0);
}
if((argv[1][j]>=65 && argv[1][j]<=90) || (argv[1][j]>=97 && argv[1][j]<=122))
{
printf("\nstring values are not accepted");
exit(0);
}
if(argv[1][j]<48||argv[1][j]>49)
{
printf("\nonly 0's and 1's are accepted");
exit(0);
}
}
printf("\nEntered binary no. is:%ld",n);
while(n!=0)
{
r=n%10;
dec=dec+r*pow(2,i);
i++;
n=n/10;
}
printf("\ndecimal equivalent no. is:%ld",dec);
i=1;
while(dec!=0)
{
r=dec%8;
oct=oct+r*i;
i*=10;
dec/=8;
}
printf("\noctal equivalent no. is:%ld",oct);
return 0;
}
We got this output by using DEV C++ (IDE)
Link: For how to take command line i/p in dev c++
Link: For other General Problems
Q) C program to convert a binary number to octal number.
Input:
101
Output:
5
Input:
11010
Output:
32
Test Case:
-------------
(1) Valid Input:
(a) Only number consisting of 0s and 1s will be given as input
(2) Invalid inputs:
(a) Decimal (b) Fraction (c) String (d) Two or more command-line arguments (e) Negative number
(3)You should generate output as follows:
(a) For right output print just the actual Octal Value to STDOUT without any other text.
(b) If any error: print 'ERROR' to the STDOUT without any other text.
#LOGIC
First, we need to convert the Binary no. into in Decimal no. then convert the decimal no. to octal no. .
#Program( In C):
In C language we use ' // ' for Single line comment and /*---*/ for multi line comment
/* Binary TO octal */
#include<stdio.h> // for standard i/p and o/p
#include<math.h> //for pow() function
#include<stdlib.h> //for atoi() and exit() function
int main(int ar,char *argv[])
{
long int n,r,dec=0,oct=0,i=0,j;
/* argv[0] is the execution file name */
n=atoi(argv[1]); //atoi stands or ascii to integer
printf("\nNo.of command line i/p are :%d",ar);
if(ar>=3)
{
printf("\nmore than one command line input");
exit(0);
}
/* printf("\n value of argv[1]:%s",argv[1]); // to print whole value of argv[1]
printf("\n value of argv[1][0]:%c",argv[1][0]); // to print first character of argv[1]
printf("\n ascii value of argv[1][0]:%d",(int)argv[1][0]); //to print ascii value of first character*/
for(j=0;argv[1][j]!='\0';j++) // As every string terminates by \0 (backslash zero)
{
if(argv[1][j]=='.')
{
printf("\ndecimal value are not accepted");
exit(0);
}
if(argv[1][j]=='-')
{
printf("\nnegative value are not accepted");
exit(0);
}
if(argv[1][j]=='/')
{
printf("\nfraction value are not accepted");
exit(0);
}
if(50<=argv[1][j] && argv[1][j] <=57)
{
printf("\nInteger value are not accepted");
exit(0);
}
if((argv[1][j]>=65 && argv[1][j]<=90) || (argv[1][j]>=97 && argv[1][j]<=122))
{
printf("\nstring values are not accepted");
exit(0);
}
if(argv[1][j]<48||argv[1][j]>49)
{
printf("\nonly 0's and 1's are accepted");
exit(0);
}
}
printf("\nEntered binary no. is:%ld",n);
while(n!=0)
{
r=n%10;
dec=dec+r*pow(2,i);
i++;
n=n/10;
}
printf("\ndecimal equivalent no. is:%ld",dec);
i=1;
while(dec!=0)
{
r=dec%8;
oct=oct+r*i;
i*=10;
dec/=8;
}
printf("\noctal equivalent no. is:%ld",oct);
return 0;
}
We got this output by using DEV C++ (IDE)
Link: For other General Problems
Good
ReplyDelete