7.Define a method to find sum of even numbers up to given limit
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Evensum
{
class Program
{
public static int Even(int n)
{
int i, sumeven = 0;
for (i = 1; i <= n; i++)
{
if ((i % 2) == 0)
{
sumeven = sumeven + i;
}
}
return sumeven;
}
static void Main(string[] args)
{
int n,ans;
Console.WriteLine("ENTER THE LIMIT");
n = int.Parse(Console.ReadLine());
ans=Even(n);
Console.WriteLine("Sum of Even Numbers upto Given Limit is :" + ans);
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Evensum
{
class Program
{
public static int Even(int n)
{
int i, sumeven = 0;
for (i = 1; i <= n; i++)
{
if ((i % 2) == 0)
{
sumeven = sumeven + i;
}
}
return sumeven;
}
static void Main(string[] args)
{
int n,ans;
Console.WriteLine("ENTER THE LIMIT");
n = int.Parse(Console.ReadLine());
ans=Even(n);
Console.WriteLine("Sum of Even Numbers upto Given Limit is :" + ans);
}
}
}
No comments:
Post a Comment