Frequently Asked C Programs in Interview

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

Frequently asked c programs in interview

1.
//program to find whether number is a prime number
#include<stdio.h>
void main()
{
int n,flag=0;
printf("Enter a number:\n");
scanf("%d",&n);
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}

2.
//program to find whether number is odd or even number
#include<stdio.h>
void main()
{
int n;
printf("Enter a number:\n");
scanf("%d",&n);
if(n%2==0)
printf("%d is an even number.",n);
else
printf("%d is an odd number.",n);
}
3.
//program to find whether number is palindrome or not
#include<stdio.h>
void main()
{
int n,num,rev=0;
printf("Enter a number:\n");
scanf("%d",&num);
n=num;
while(n!=0)
{
rev=rev*10+(n%10);
n/=10;
}
if(rev==num)
printf("Given number is a palindrome.");
else printf("Given number is not a palindrome.");
}

4.
//program to find whether given string is palindrome or not
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
printf("Enter string-");
scanf("%s",str);
int flag=1,l=strlen(str);
for(int i=0;i<l/2;i++)
{
if(str[i]!=str[l-i-1])
{
flag=0;
break;
}
}
if(flag==0)
printf("The string isnt a palindrome.\n");
else
printf("The string is a palindrome.\n");

5.
//program to print fibonacci series of a given range
#include<stdio.h>
void main()
{
int low,up,A=0,B=1,C;

printf("Enter the lower limit: ");


scanf("%d", &low);
printf("Enter the upper limit: ");
scanf("%d", &up);
printf("Fibonacci numbers in the specified range are:\n");
while(A<up)
{
if(A>low)
printf("%d\n", A);
C = A + B;
A = B;
B = C;
}
}

6.
//program to find factorial of a given number
#include<stdio.h>
void main()
{
int n,f=1;
printf("Enter the number:");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
f*=i;
}
printf("!%d=%d",n,f);
}

7.
//program to find GCD of 2 numbers
#include<stdio.h>
int gcd(int a,int b);
void main()
{
int a,b;
printf("Enter the numbers:\n");
scanf("%d %d",&a,&b);
printf("The G.C.D. of the 2 numbers is=%d",gcd(a,b));
}
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}

Area and volume


1.
//program to find area of circle
#include<stdio.h>
void main()
{
float area,r,pi=3.14;
printf("Enter radius of the circle:");
scanf("%f",&r);
area=pi*r*r;
printf("Area=%f",area);

}
2.
//program to find area of a triangle
#include<stdio.h>
#include<math.h>
void main()
{
float s,area,a,b,c;
printf("Enter length of the three sides of the triangle:\n");
scanf("%f %f %f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area=%f",area);

3.
//program to find area of an equilateral triangle
#include<stdio.h>
#include<math.h>
void main()
{
float area,a;
printf("Enter length of the sides of the triangle:\n");
scanf("%f",&a);
area=sqrt(3)/4*a*a;
printf("Area=%f",area);

4.
//program to find area of a right angled triangle
#include<stdio.h>
void main()
{
float area,b,h;
printf("Enter base length and height of the triangle:\n");
scanf("%f %f",&b,&h);
area=0.5*h*b;
printf("Area=%f",area);

5.
//program to find area of a rectangle
#include<stdio.h>
void main()
{
float area,l,b;
printf("Enter length and breath of the rectangle:\n");
scanf("%f %f",&l,&b);
area=l*b;
printf("Area=%f",area);

6.
//program to find area of a trapezium
#include<stdio.h>
void main()
{
float area,a,b,h;
printf("Enter the two lengths of the parallel sides of the trapezium:\n");
scanf("%f %f",&a,&b);
printf("Enter the height of the trapezium:\n");
scanf("%f",&h);
area=((a+b)/2)*h;
printf("Area=%f",area);

7.
//program to find area of a rhombus
#include<stdio.h>
void main()
{
float area,a,b;
printf("Enter the two lengths of the diagonals of the rhombus:\n");
scanf("%f %f",&a,&b);
area=a*b/2;
printf("Area=%f",area);

8.
//program to find area of a parallelogram
#include<stdio.h>
void main()
{
float area,a,b;
printf("Enter the length and breadth of the parallelogram:\n");
scanf("%f %f",&a,&b);
area=a*b;
printf("Area=%f",area);

9.
//program to find surface area and volume of a cube
#include<stdio.h>
void main()
{
float area,a,v;
printf("Enter length of the sides of the cube:\n");
scanf("%f",&a);
area=6*a;
v=a*a*a;
printf("Area=%f\n",area);
printf("Volume=%f",v);

10.
//program to find surface area and volume of a cuboid
#include<stdio.h>
void main()
{
float area,l,b,h,v;
printf("Enter length,breadth and height of the cuboid:\n");
scanf("%f %f %f",&l,&b,&h);
area=2*l*b+(2*b*h)+(2*l*h);
v=l*b*h;
printf("Area=%f\n",area);
printf("Volume=%f",v);

11.
//program to find surface area and volume of a cylinder
#include<stdio.h>
void main()
{
float area,r,v,h;
printf("Enter the radius and height of the cylinder:\n");
scanf("%f %f",&r,&h);
area=2*3.14*r*r+(2*3.14*r*h);
v=3.14*r*r*h;
printf("Area=%f\n",area);
printf("Volume=%f",v);

12.
//program to find surface area and volume of a cone
#include<stdio.h>
void main()
{
float area,r,s,h,v;
printf("Enter radius, height and slant height of the cone:\n");
scanf("%f %f %f",&r,&h,&s);
area=3.14*r*s+(3.14*r*r);
v=3.14*r*r*h*1/3;
printf("Area=%f\n",area);
printf("Volume=%f",v);

13.
//program to find surface area and volume of a sphere
#include<stdio.h>
void main()
{
float area,r,v;
printf("Enter radius of the sphere:\n");
scanf("%f",&r);
area=4*3.14*r*r;
v=3.14*r*r*r*4/3;
printf("Area=%f\n",area);
printf("Volume=%f",v);

14.
//program to find perimeter of a circle,rectangle and triangle
#include<stdio.h>
void main()
{
float r,l,b,x,y,z;
printf("Enter radius of the circle:\n");
scanf("%f",&r);
printf("Perimeter of the circle=%f\n",2*3.14*r);
printf("Enter length and breadth of the rectangle:\n");
scanf("%f %f",&l,&b);
printf("Perimeter of the rectangle=%f\n",2*(l+b));
printf("Enter length of the 3 sides of the triangle:\n");
scanf("%f %f %f",&x,&y,&z);
printf("Perimeter of the triangle=%f\n",x+y+z);
}
C program with numbers

1.
//program to LCM of 2 numbers
#include<stdio.h>
int gcd(int a,int b);
void main()
{
int a,b;
printf("Enter the numbers:\n");
scanf("%d %d",&a,&b);
printf("The L.C.M. of the 2 numbers is=%d",a*b/gcd(a,b));
}
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}

2.
//program to find sum of digits
#include<stdio.h>
void main()
{
int n,sum=0;
printf("Enter a number:\n");
scanf("%d",&n);
while(n!=0)
{
sum+=n%10;
n/=10;
}
printf("The sum of the digits of the number=%d",sum);
}

3.
//program to find power
#include<stdio.h>
void main()
{
int a,b,pw=1;
printf("Enter the number and the power:\n");
scanf("%d %d",&a,&b);
for(int i=0;i<b;i++)
pw*=a;
printf("%d ^ %d = %d",a,b,pw);
}

4.
//program to fins sum of 2 numbers without using addition operator
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the numbers:\n");
scanf("%d %d",&a,&b);
while(b!=0)
{
int carry=a&b;
a=a^b;
b=carry<<1;
}
printf("The sum of the 2 numbers is=%d",a);
}

5.
//program to fins difference of 2 numbers without using subtraction operator
#include<stdio.h>
void main()
{
int a,b;
printf("Enter the numbers:\n");
scanf("%d %d",&a,&b);
while(b!=0)
{
int borrow=~a&b;
a=a^b;
b=borrow<<1;
}
printf("The difference of the 2 numbers is=%d",a);
}

6.
//program to find largest of 3 numbers using binary minus operator
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the numbers:\n");
scanf("%d %d %d",&a,&b,&c);
if(a==b && a==c)
printf("All the 3 numbers are equal.");
else if(a-b>=0 && a-c>=c)
printf("%d is the largest number.",a);
else if(b-a>=0 && b-c>=0)
printf("%d is the largest number.",b);
else printf("%d is the largest number.",c);
}

7.
//program to find largest of 3 numbers using conditional operator
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter the numbers:\n");
scanf("%d %d %d",&a,&b,&c);
(a==b)&&(a==c)?printf("All the three numbers are equal."):(a>=b)&&(a>=c)?printf("%d is the
largest number.",a):(b>=a)&&(b>=c)?printf("%d is the largest number.",b):printf("%d is the
largest number.",c);
}
8.
//program to find prime factors of a number
# include <stdio.h>
void primefactors(int);
void main()
{
int n;
printf("Enter a number:\n");
scanf("%d",&n);
if(n<=1)
{
printf("%d has no prime factors.",n);
return 0;
}
printf("All the prime factors of the number are:\n");
primefactors(n);
}
void primefactors(int n)
{
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
for (int i = 3; i <= n/2; i = i+2)
{
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
if (n > 2)
printf ("%d ", n);
}

9.
//program to find NCR factor
#include<stdio.h>
int fact(int);
void main()
{
int n,r,ncr;
printf("Enter 2 numbers:\n");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(n-r)*fact(r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
}
int fact(int a)
{
int f=1;
for(int i=1;i<=a;i++)
f*=i;
return f;
}

10.
//program to convert string to int
#include <stdio.h>
#include<stdlib.h>
void main()
{
char str[50];
printf("Enter a string:");
scanf("%s",str);
printf("The integer value of the string is:%d",atoi(str));
}

11.
//program to find HCF of 2 numbers
#include<stdio.h>
int hcf(int a,int b); //hcf is same as gcd
void main()
{
int a,b;
printf("Enter the numbers:\n");
scanf("%d %d",&a,&b);
printf("The H.C.F of the 2 numbers is=%d",hcf(a,b));
}
int hcf(int a,int b)
{
if(b==0)
return a;
return hcf(b,a%b);
}

12.
//program to find largest of n numbers
#include <stdio.h>
void main()
{
int n,num,large=0;
printf("Enter how many numbers?:");
scanf("%d",&n);
printf("Enter the numbers:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&num);
if(num>large)
large=num;
}
printf("The largest number is:%d",large);
}

13.
//program to split number into digits
#include<stdio.h>
void main()
{
int n,num,count=0;
printf("Enter a number:\n");
scanf("%d",&n);
num=n;
while(n!=0)
{
count++;
n/=10;
}
int digit[count];
for(int i=count;i>0;i--)
{
digit[i-1]=num%10;
num/=10;
}
printf("The digits of the number are:\n");
for(int i=0;i<count;i++)
printf("%d\n",digit[i]);
}

14.
//program to count number of digits of a number
#include<stdio.h>
void main()
{
int n,count=0;
printf("Enter a number:\n");
scanf("%d",&n);
while(n!=0)
{
count++;
n/=10;
}
printf("Number of digits=%d",count);
}

15.
//program to print last 2 digits of year
#include<stdio.h>
void main()
{
int y;
printf("Enter a year:");
scanf("%d",&y);
printf("Last two digits of year is: %02d",y%100);
}

16.
//program to find number of 7s coming between 1 to 100
#include<stdio.h>
void main()
{
int count=0;
for(int i=1;i<=10;i++)
{
while(i!=0)
{
if(i%10==7)
count++;
i/10;
}
}
printf("Number of 7s between 1 and 100=%d",count);
}

17.
//program to compute quotient and remainder
#include<stdio.h>
void main()
{
int a,b,q,r;
printf("Enter 2 numbers:");
scanf("%d %d",&a,&b);
q=a/b;
r=a%b;
printf("Quotient=%d\n",a,b,q);
printf("Remainder=%d\n",a,b,r);
}

18.
//program to check for leap year
#include<stdio.h>
void main()
{
int year;
printf("Enter the year:");
scanf("%d",&year);
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("The year %d is a leap year.",year);
else
printf("The year %d is not a leap year.",year);
}

19.
//program to check whether number is +ve or -ve
#include<stdio.h>
void main()
{
int n;
printf("Enter a number:\n");
scanf("%d",&n);
if(n>=0)
printf("Number is positive");
else
printf("Number is negative");
}

Swapping
1.
//program to swap 2 numbers
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter two number:\n");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("The numbers after swapping: %d %d",a,b);
}

2.Program to swap two numbers without using third variable


#include <stdio.h>
void main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a – b;
printf("a = %d\nb = %d\n",a,b);
}

or

#include <stdio.h>
void main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d %d", &a, &b);
a = a * b;
b = a /b;
a = a / b;
printf("a = %d\nb = %d\n",a,b);
}

Or

#include <stdio.h>
void main()
{
int a, b;
printf("Enter two integers to swap\n");
scanf("%d %d", &a, &b);
a = a ^ b;
b = a ^b;
a = a ^ b;
printf("a = %d\nb = %d\n",a,b);
}

3.
//program to swap two arrays
#include <stdio.h>
void main()
{
int n;
printf("Enter the size of the arrays: ");
scanf("%d",&n);
int a[n], b[n], temp[n];
printf("Enter elements of first array:\n");
for (int i = 0; i < n; i++)
scanf("%d",&a[i]);
printf("Enter elements of second array:\n");
for (int i = 0; i < n; i++)
scanf("%d", &b[i]);
for (int i = 0; i < n; i++)
{
temp[i] = a[i];
a[i] = b[i];
b[i] = temp[i];
}
printf("\nElements of first array:\n");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nElements of second array:\n");
for (int i = 0; i < n; i++)
printf("%d ", b[i]);
}
4.
#include <stdio.h>
#include <string.h>
void main()
{
char first[100], second[100], temp[100];

printf("Enter first string\n");


gets(first);

printf("Enter second string\n");


gets(second);

printf("\nBefore Swapping\n");
printf("First string: %s\n", first);
printf("Second string: %s\n\n", second);

strcpy(temp, first);
strcpy(first, second);
strcpy(second, temp);

printf("After Swapping\n");
printf("First string: %s\n", first);
printf("Second string: %s\n", second);
}

Conversion (Number System)


1.//program to convert decimal number to binary and vice versa
#include <stdio.h>
#include<math.h>
int decimal(int);
int binary(int);
void main()
{
int n;
char ch;
printf("Convert decimal number to binary(B) or vice versa(D)?");
scanf(" %c",&ch);
if(ch=='b'||ch=='B')
{
printf("Enter a decimal number:\n");
scanf("%d",&n);
printf("The binary equivalent of the decimal number is %d",binary(n));
}
else
{
printf("Enter a binary number:\n");
scanf("%d",&n);
printf("The decimal equivalent of the binary number is %d",decimal(n));
}
}
int binary(int n)
{
int rem,temp=1,bin=0;
while(n!=0)
{
rem=n%2;
bin+=rem*temp;
n/=2;
temp*=10;
}
return bin;
}
int decimal(int n)
{
int deci=0,rem;
for(int i=0;n!=0;i++)
{
rem=n%10;
deci+=(rem*pow(2,i));
n/=10;
}
return deci;
}
2.
//program to convert decimal number to octal and vice versa
#include <stdio.h>
#include<math.h>
int decimal(int);
int octal(int);
void main()
{
int n;
char ch;
printf("Convert decimal number to octal(O) or vice versa(D)?");
scanf(" %c",&ch);
if(ch=='o'||ch=='O')
{
printf("Enter a decimal number:\n");
scanf("%d",&n);
printf("The octal equivalent of the decimal number is %d",octal(n));
}
else
{
printf("Enter a octal number:\n");
scanf("%d",&n);
printf("The decimal equivalent of the octal number is %d",decimal(n));
}
}
int octal(int n)
{
int rem,temp=1,oct=0;
while(n!=0)
{
rem=n%8;
oct+=rem*temp;
n/=8;
temp*=10;
}
return oct;
}
int decimal(int n)
{
int deci=0,rem;
for(int i=0;n!=0;i++)
{
rem=n%10;
deci+=(rem*pow(8,i));
n/=10;
}
return deci;
}

3.
//program to convert decimal number to hexadecimal and vice versa
#include <stdio.h>
#include<math.h>
#include<string.h>
void decimal();
void hexa(int);
void main()
{
int n;
char ch;
printf("Convert decimal number to hexadecimal(H) or vice versa(D)?");
scanf(" %c",&ch);
if(ch=='h'||ch=='H')
{
printf("Enter a decimal number:\n");
scanf("%d",&n);
printf("The hexadecimal equivalent of the decimal number is:\n");
hexa(n);
}
else
{
decimal();
}
}
void hexa(int n)
{
char hex[20];
int i=0;
while(n!=0)
{
int rem=0;
rem=n%16;
if(rem<10)
{
hex[i]=48+rem;
}
else
{
hex[i]=55+rem;
}
i++;
n/=16;
}
for(int j=i-1;j>=0;j--)
printf("%c",hex[j]);
}
void decimal(int n)
{
char hex[20];
printf("Enter a hexadecimal number:\n");
scanf("%s",hex);
printf("The decimal equivalent of the hexadecimal number is:\n");
int dec=0;
for(int i=strlen(hex)-1;i>=0;i--)
{
if(hex[i]=='A')
dec+=10*pow(16,strlen(hex)-i-1);
else if(hex[i]=='B')
dec+=11*pow(16,strlen(hex)-i-1);
else if(hex[i]=='C')
dec+=12*pow(16,strlen(hex)-i-1);
else if(hex[i]=='D')
dec+=13*pow(16,strlen(hex)-i-1);
else if(hex[i]=='E')
dec+=14*pow(16,strlen(hex)-i-1);
else if(hex[i]=='F')
dec+=15*pow(16,strlen(hex)-i-1);
else
dec+=(hex[i]-48)*pow(16,strlen(hex)-i-1);
}
printf("%d",dec);

4.
//program to convert octal number to binary and vice versa
#include <stdio.h>
#include<math.h>
int octal(int);
int binary(int);
void main()
{
int n;
char ch;
printf("Convert octal number to binary(B) or vice versa(O)?");
scanf(" %c",&ch);
if(ch=='b'||ch=='B')
{
printf("Enter a octal number:\n");
scanf("%d",&n);
printf("The binary equivalent of the octal number is %d",binary(n));
}
else
{
printf("Enter a binary number:\n");
scanf("%d",&n);
printf("The octal equivalent of the binary number is %d",octal(n));
}
}
int binary(int n)
{
int deci=0,rem;
for(int i=0;n!=0;i++)
{
rem=n%10;
deci+=(rem*pow(8,i));
n/=10;
}

int rem_b,temp=1,bin=0;
while(deci!=0)
{
rem_b=deci%2;
bin+=rem_b*temp;
deci/=2;
temp*=10;
}
return bin;
}
int octal(int n)
{
int deci=0,rem;
for(int i=0;n!=0;i++)
{
rem=n%10;
deci+=(rem*pow(2,i));
n/=10;
}

int rem_o,temp=1,oct=0;
while(deci!=0)
{
rem_o=deci%8;
oct+=rem_o*temp;
deci/=8;
temp*=10;
}
return oct;
}
5.//program to convert octal number to hexadecimal
#include <stdio.h>
#include<math.h>

void main()
{
int n;
printf("Enter a octal number:\n");
scanf("%d",&n);
printf("The hexadecimal equivalent of the octal number is:\n");
int deci=0,rem;
for(int i=0;n!=0;i++)
{
rem=n%10;
deci+=(rem*pow(8,i));
n/=10;
}

char hex[20];
int i=0;
while(deci!=0)
{
int rem=0;
rem=deci%16;
if(rem<10)
{
hex[i]=48+rem;
}
else
{
hex[i]=55+rem;
}
i++;
deci/=16;
}
for(int j=i-1;j>=0;j--)
printf("%c",hex[j]);
}
6.
//program to print numbers in words
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void convert_to_words(char *num)


{
int len = strlen(num);

if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr, "Length more than 4 is not supported\n");
return;
}

char *single_digits[] = { "zero", "one", "two",


"three", "four","five",
"six", "seven", "eight", "nine"};

char *two_digits[] = {"", "ten", "eleven", "twelve",


"thirteen", "fourteen",
"fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};

char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",


"sixty", "seventy", "eighty", "ninety"};

char *tens_power[] = {"hundred", "thousand"};

printf("\n%s: ", num);


if (len == 1) {
printf("%s\n", single_digits[*num - '0']);
return;
}

while (*num != '\0') {

if (len >= 3) {
if (*num -'0' != 0) {
printf("%s ", single_digits[*num - '0']);
printf("%s ", tens_power[len-3]);
}
--len;
}

else {

if (*num == '1') {
int sum = *num - '0' + *(num + 1)- '0';
printf("%s\n", two_digits[sum]);
return;
}

else if (*num == '2' && *(num + 1) == '0') {


printf("twenty\n");
return;
}

else {
int i = *num - '0';
printf("%s ", i? tens_multiple[i]: "");
++num;
if (*num != '0')
printf("%s ", single_digits[*num - '0']);
}
}
++num;
}
}

void main(void)
{
char num[10];
printf("Enter the number:\n");
scanf("%s",num);
convert_to_words(num);
}

7.
//program to convert each digits of a number in words
#include <stdio.h>

void main()
{
int n, num = 0;

printf("Enter any number to print in words: ");


scanf("%d", &n);

while(n != 0)
{
num = (num * 10) + (n % 10);
n /= 10;
}

while(num != 0)
{
switch(num % 10)
{
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}

num = num / 10;


}

8.
//program to convert binary number to hexadecimal
#include <stdio.h>
#include<math.h>
void main()
{
int n;
printf("Enter a binary number:\n");
scanf("%d",&n);
printf("The hexadecimal equivalent of the binary number is:\n");

int deci=0,rem;
for(int i=0;n!=0;i++)
{
rem=n%10;
deci+=(rem*pow(2,i));
n/=10;
}

char hex[20];
int i=0;
while(deci!=0)
{
int rem=0;
rem=deci%16;
if(rem<10)
{
hex[i]=48+rem;
}
else
{
hex[i]=55+rem;
}
i++;
deci/=16;
}
for(int j=i-1;j>=0;j--)
printf("%c",hex[j]);
}

9.
//program to add two binary numbers
#include <stdio.h>

void main()
{

long a,b;
int i = 0, rem= 0, sum[20];

printf("Enter the first binary number: ");


scanf("%ld", &a);
printf("Enter the second binary number: ");
scanf("%ld", &b);
while (a!= 0||b!= 0)
{
sum[i++]=(a%10+b%10+rem)%2;
rem=(a%10+b%10+rem)/2;
a/=10;
b/=10;
}
if (rem!= 0)
sum[i++] = rem;
--i;
printf("Sum of two binary numbers: ");
while (i >= 0)
printf("%d", sum[i--]);

10.
#include<stdio.h>

int binaryAddition(int,int);

int main(){

long int binary1,binary2,multiply=0;


int digit,factor=1;
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);

while(binary2!=0){
digit = binary2 %10;

if(digit ==1){
binary1=binary1*factor;
multiply = binaryAddition(binary1,multiply);
}
else
binary1=binary1*factor;

binary2 = binary2/10;
factor = 10;
}

printf("Product of two binary numbers: %ld",multiply);

return 0;
}

int binaryAddition(int binary1,int binary2){

int i=0,remainder = 0,sum[20];


int binarySum=0;

while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}

if(remainder!=0)
sum[i++] = remainder;
--i;
while(i>=0)
binarySum = binarySum*10 + sum[i--];

return binarySum;
}

11.
#include<stdio.h>
#include<string.h>
void decimal2roman(int);
void main()
{
char ch;
printf("Convert decimal to roman(R) or vice versa(D)?\n");
scanf(" %c",&ch);
if(ch=='r'||ch=='R')
{
int n;
printf("Enter a number:\n");
scanf("%d",&n);
decimal2roman(n);
}
else
{
char rom[30];
int a[30], l, i, k;
printf("Enter the roman number\n");
scanf("%s", &rom);
l =strlen(rom);
for(i = 0; i < l; i++)
{
switch (rom[i])
{
case 'I': a[i] = 1;
break;
case 'V': a[i] = 5;
break;
case 'X': a[i] = 10;
break;
case 'L': a[i] = 50;
break;
case 'C': a[i] = 100;
break;
case 'D': a[i]=500;
break;
case 'M': a[i] = 1000;
break;
default : printf("Invalid choice");
break;
}
}
k = a[l - 1];
for(i = l - 1; i > 0; i--)
{
if(a[i] > a[i - 1])
{
k = k - a[i - 1];
}
if(a[i] <= a[i - 1])
{
k = k + a[i - 1];
}
}
printf("decimal equivalent is %d", k);
}
}

void decimal2roman(int num){


int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
char *symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int i = 0;

while(num)
{
while(num/decimal[i])
{
printf("%s",symbol[i]);
num -= decimal[i];
}
i++;
}
}

12.
//program to convert fractional decimal to binary fraction
#include<stdio.h>

void main()
{
double z,doub,count=0,frac;
int num,p,ar[100],i=0;

printf("Enter the number to convert");


scanf("%lf",&doub);
num=doub;
frac=doub-num;
while(num!=0)
{
ar[i]=num%2;
num=num/2;
i++;
count++;
}

printf("The Binary form of this number is ");


for(i=(count-1);i>=0;i--)
{
printf("%d",ar[i]);
}
printf(".");
while(frac!=0)
{
z=frac*2;
p=z;
frac=z-p;
printf("%d",p);
}

You might also like