WAP in C to find the Largest Number in an Array of Numbers using Recursion?

#include<stdio.h>
#include<conio.h>
int large(int[], int, int);
int main()
{
int size, largest, list[20], i;
clrscr();
printf(“Enter size of the array:”);
scanf(“%d”, &size);
printf(“Enter the elements:\n”);
for (i = 0; i < size ; i++)
{
scanf(“%d”,&list[i]);
}
if (size == 0)
{
printf(“Empty list\n”);
}
else
{
largest = list[0];
largest = large(list, size – 1, largest);
printf(“\nThe largest number in the list is: %d\n”, largest);
}

getch();

return(0);
}
int large(int list[], int size, int largest)
{
if (size == 1)
return(largest);
if (size > -1)
{
if (list[size] > largest)
{
largest = list[size];
}
return(largest = large(list, size – 1, largest));
}
else
{
return(largest);
}
}

Output:-

larnumrec.png

WAP in C to find the nth number in Fibonacci series using recursion?

#include <stdio.h>
#include<conio.h>
int fibo(int);
int main()
{
int num, result;
printf(“Enter the nth number in fibonacci series: “);
scanf(“%d”, &num);
if (num < 0)
{
printf(“Fibonacci of negative number is not possible.\n”);
}
else
{
result = fibo(num);
printf(“The %d number in fibonacci series is %d\n”, num, result);
}
getch();
return(0);
}
int fibo(int num)
{
if (num == 0)
{
return(0);
}
else if (num == 1)
{
return(1);
}
else
{
return(fibo(num – 1) + fibo(num – 2));
}
}

Output:-

fabirecu.png

WAP in C to Convert Hexadecimal Number into Binary Number?

#include <stdio.h>
#include<conio.h>
#define MAX 1000
int main()
{
char hexa[MAX];
long int i = 0;
clrscr();
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++;
}
getch();
return(0);
}

Output :-

HexToBin.png

WAP in C to Convert Octal Number into Decimal Number?

#include <stdio.h>
#include<conio.h>
#include <math.h>

int main()
{

long int octal, decimal = 0;
int i = 0;
clrscr();
printf(“Enter any octal number: “);
scanf(“%ld”, &octal);
while (octal != 0)
{
decimal = decimal +(octal % 10)* pow(8, i++);
octal = octal / 10;
}
printf(“Equivalent decimal value: %ld”,decimal);
getch();
return(0);
}

Output :-

OctToDec

WAP in C program to Convert Decimal Number to Octal Number?

#include<stdio.h>
#include<conio.h>
int main()
{
long dnum, quotient;
int onum[100], i = 1, j;
clrscr();
printf(“Enter the decimal number: “);
scanf(“%ld”, &dnum);
quotient = dnum;
while (quotient != 0)
{
onum[i++] = quotient % 8;
quotient = quotient / 8;
}
printf(“Equivalent octal value of decimal no %d: “, dnum);
for (j = i – 1; j > 0; j–)
printf(“%d”, onum[j]);
getch();
return(0);
}

Output:-

DecToOct

WAP in C to Convert a Decimal Number into Binary Number?

#include <stdio.h>
#include<conio.h>
void main()
{
long num, decimal, rem, base = 1, binary = 0;
clrscr();
printf(“Enter a decimal integer \n”);
scanf(“%ld”, &num);
decimal = num;
while (num > 0)
{
rem = num % 2;
binary = binary + rem * base;
num = num / 2;
base = base * 10;
}
printf(“Decimal number is = %d\n”, decimal);
printf(“Binary equivalent is = %ld\n”, binary);
getch();
}

Output:-

DecToBin

WAP in C to Convert the Binary Number into Decimal Number ?

#include <stdio.h>
#include<conio.h>
void main()
{
int num, binary, decimal = 0, base = 1, rem;
clrscr();
printf(“Enter a binary number(1s and 0s) \n”);
scanf(“%d”, &num); /* maximum five digits due to range of integer */
binary = num;
while (num > 0)
{
rem = num % 10;
decimal = decimal + rem * base;
num = num / 10 ;
base = base * 2;
}
printf(“The Binary number is = %d \n”, binary);
printf(“Decimal equivalent is = %d \n”, decimal);
getch();
}

Output :-

BinToDec