[Link] the average of first n natural numbers and assign it to output1.
If list contain
any negative number, assign -1 to output1.
using System;
using [Link];
using [Link];
using [Link];
namespace sample
{
class avg
{
public static void Main(string[] args)
{
avg a1 = new avg();
int output = a1.avg1(7);
[Link]("output is:{0}", output);
[Link]();
}
int avg1(int x)
{
int sum = 0;
if (x < 0)
{
return -1;
}
else
{
for (int i = 0; i <= x; i++)
{
sum = sum + i;
}
return sum / x;
}
}
}
}
[Link] Reverse of a string "Hello World" --> "dlroW olleH". If string other then alphabet
assign -1 to output1.
using System;
using [Link];
using [Link];
using [Link];
namespace sample
{
class reverse
{
static void Main(string[] args)
{
string output=null;
reverse r = new reverse();
string s="hello world1";
output = [Link](s);
[Link]("the reversed string is:{0}", output);
[Link]();
}
string rev(string s1)
{
char[] str=[Link]();
foreach(char s in str)
{
if([Link](s))
{
return "-1";
}
}
[Link](str);
return new string(str) ;
}
}
}
[Link] Factorial of [Link] n is negative number, assign -1 to output1.
using System;
using [Link];
using [Link];
using [Link];
namespace sample
{
class fact
{
public static void Main(string[] args)
{
fact f1 = new fact();
int output = f1.fact1(3);
[Link]("the output is: {0}", output);
[Link]();
}
int fact1(int x)
{
if (x < 0)
{
return -1;
}
else if (x == 0)
{
return 1;
}
else
{
return (x * fact1(x - 1));
}
}
}
}
[Link] Validation.
If any angle in negative, assign -1 to output1.
The triangle is valid if sum of angle1,angle2,angle3 is 180 [Link] it is valid
assign 1 to output1.
If it is not valid, assign -2 to output1.
using System;
using [Link];
using [Link];
using [Link];
namespace sample
{
class triangle
{
public static void Main(string[] args)
{
int angle1, angle2, angle3;
[Link]("enter 3 angles");
angle1 = Convert.ToInt32([Link]());
angle2 = Convert.ToInt32([Link]());
angle3 = Convert.ToInt32([Link]());
triangle t1 = new triangle();
int output1 =[Link](angle1, angle2, angle3);
[Link]("output:{0}",output1);
[Link]();
}
int triag(int x, int y, int z)
{
int sum = 0;
if (x == 0 || y == 0 || z == 0)
{
return -1;
}
sum = x + y + z;
if (sum != 180)
{
return -2;
}
else
{
return 1;
}
}
}
}
[Link] the Second Smallest integer in array and assign it to [Link] array contain any
negative number, assign -1 to output1.
using System;
using [Link];
using [Link];
using [Link];
namespace sample
{
class second
{
public static void Main(string[] args)
{
int[] a = { 2, 4, 5, 6, 1, 8, 3 };
second s = new second();
int output = s.sec_small(a);
[Link](output);
[Link]();
}
int sec_small(int[] arr)
{
[Link](arr);
foreach (int a1 in arr)
{
if (a1 < 0)
return -1;
}
int sec = (from a in arr
orderby a
select a).Skip(1).First();
return sec;
}
}
}
[Link] the average of numbers in list and assign it to [Link] list contain any
negative number, assign -1 to output1.
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace sample
{
class averagelist
{
public static void Main(string[] args)
{
List<int> a = new List<int>() { 1, 3, -4, 5, 2 };
averagelist a1 = new averagelist();
[Link](a);
}
public void avg(List<int> a)
{
double output = 0;
foreach (int a2 in a)
{
if (a2 < 0)
{
output = -1;
break;
}
else
{
output = [Link]();
}
}
[Link](output);
[Link]();
}
}
}