using
System;
class
GFG{
static
string
decToHexa(
int
n)
{
char
[]hexaDeciNum =
new
char
[2];
int
i = 0;
while
(n != 0)
{
int
temp = 0;
temp = n % 16;
if
(temp < 10)
{
hexaDeciNum[i] = (
char
) (temp + 48);
i++;
}
else
{
hexaDeciNum[i] = (
char
) (temp + 55);
i++;
}
n = n / 16;
}
string
hexCode =
""
;
if
(i == 2)
{
hexCode += hexaDeciNum[0];
hexCode += hexaDeciNum[1];
}
else
if
(i == 1)
{
hexCode =
"0"
;
hexCode += hexaDeciNum[0];
}
else
if
(i == 0)
hexCode =
"00"
;
return
hexCode;
}
static
string
convertRGBtoHex(
int
R,
int
G,
int
B)
{
if
((R >= 0 && R <= 255) &&
(G >= 0 && G <= 255) &&
(B >= 0 && B <= 255))
{
string
hexCode =
"#"
;
hexCode += decToHexa(R);
hexCode += decToHexa(G);
hexCode += decToHexa(B);
return
hexCode;
}
else
return
"-1"
;
}
public
static
void
Main(
string
[] args)
{
int
R = 0, G = 0, B = 0;
Console.Write(convertRGBtoHex(R, G, B) +
"\n"
);
R = 255; G = 255; B = 255;
Console.Write(convertRGBtoHex(R, G, B) +
"\n"
);
R = 25; G = 56; B = 123;
Console.Write(convertRGBtoHex(R, G, B) +
"\n"
);
R = 2; G = 3; B = 4;
Console.Write(convertRGBtoHex(R, G, B) +
"\n"
);
R = 255; G = 255; B = 256;
Console.Write(convertRGBtoHex(R, G, B) +
"\n"
);
}
}