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

WAP in C to Convert a Given Number of Days in terms of Years, Weeks & Days(ignore leap year)?

#include<stdio.h>
#include<conio.h>
#define DAYSINWEEK 7

int main()
{
int numdays, year, week, days;
clrscr();
printf(“Enter the number of days”);
scanf(“%d”, &numdays);
year = numdays / 365;
week =(numdays % 365) / DAYSINWEEK;
days =(numdays % 365) % DAYSINWEEK;
printf (“%d is equivalent to %d years, %d weeks and %d days”,numdays, year, week, days);
getch();
return(0);
}

Output :-

convertdaytoweek.png

 

WAP in C to Find GCD of two Numbers?

#include<stdio.h>
#include<conio.h>
int main()
{
int n1, n2, i, g;
clrscr();
printf(“Enter two numbers: “);
scanf(“%d %d”, &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
g = i;
}
printf(“G.C.D of %d and %d is %d”, n1, n2, g);
getch();
return(0);
}

Output :-

gcd

WAP in C to display Day of the month ?

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

int fm(int date, int month, int year) {
int fmonth, leap;
clrscr();
//leap function 1 for leap & 0 for non-leap
if ((year % 100 == 0) && (year % 400 != 0))
leap = 0;
else if (year % 4 == 0)
leap = 1;
else
leap = 0;

fmonth = 3 + (2 – leap) * ((month + 2) / (2 * month))
+ (5 * month + month / 9) / 2;

//bring it in range of 0 to 6
fmonth = fmonth % 7;

return fmonth;
}

int day_of_week(int date, int month, int year) {

int dayOfWeek;
int YY = year % 100;
int century = year / 100;

printf(“\nDate: %d/%d/%d \n”, date, month, year);

dayOfWeek = 1.25 * YY + fm(date, month, year) + date – 2 * (century % 4);

//remainder on division by 7
dayOfWeek = dayOfWeek % 7;

switch (dayOfWeek)
{
case 0:
printf(“weekday = Saturday”);
break;
case 1:
printf(“weekday = Sunday”);
break;
case 2:
printf(“weekday = Monday”);
break;
case 3:
printf(“weekday = Tuesday”);
break;
case 4:
printf(“weekday = Wednesday”);
break;
case 5:
printf(“weekday = Thursday”);
break;
case 6:
printf(“weekday = Friday”);
break;
default:
printf(“Incorrect data”);
}
return (0);
}

int main() {
int date, month, year;

printf(“\nEnter the year “);
scanf(“%d”, &year);

printf(“\nEnter the month “);
scanf(“%d”, &month);

printf(“\nEnter the date “);
scanf(“%d”, &date);

day_of_week(date, month, year);
getch();
return(0);
}

Output :-

Untitled