Can you please help regarding Out of Memory exception?

Posted by Naga116 under C# on 8/23/2011 | Points: 10 | Views : 1099 | Status : [Member] | Replies : 2
Aim is to print odd and even numbers and Odd number which ends with 3
even numbers: 2,4,6,8,10...
like: 3,13,....

I am getting System out of memory exception..

protected void Page_Load(object sender, EventArgs e)
{
int evensum = 0;
int oddsum = 0;

int a = 10;
int j = 10;

for(int i=1; i<=a;i++)
{
if (i % 2 == 0)
{
Response.Write("Even number" + i+"</br>");
evensum = evensum + i;
}
else
{
while (i <= 10)
{
Response.Write("Odd Number" + i*3 + "</br>");
}

oddsum = oddsum + j;

}
}
Response.Write("Even sum" + evensum + "Odd sum" + oddsum);
}
}

Nagaraju


Responses

Posted by: Harikatakam on: 8/23/2011 [Member] Starter | Points: 25

Up
0
Down
Dera Naga Hope below code will help you


int evensum = 0;
int oddsum = 0;
int sumof3s=0;
ArrayList arryof3s=new ArrayList();
ArrayList arryofevens = new ArrayList();
ArrayList arryofOdds = new ArrayList();

int a = 50;
int j = 10;

for (int i = 1; i <= a; i++)
{
if (i % 2 == 0)
{

evensum = evensum + i;
arryofevens.Add(i);
}
else
{
string temp = i.ToString();
if (temp.EndsWith("3"))
{
sumof3s += i;
arryof3s.Add(i);
}
else
{
oddsum += i;
arryofOdds.Add(i);
}
oddsum = oddsum + j;

}
}
for(int i=0;i<arryof3s.Count;i++)
{
if (i == 0)
Console.WriteLine("Numbers end with 3 are:-");
Console.Write(arryof3s[i].ToString()+" ");
}
for (int i = 0; i < arryofevens.Count; i++)
{
if (i == 0)
Console.WriteLine("Even Numbers Are:-");
Console.Write(arryofevens[i].ToString() + " ");
}
for (int i = 0; i < arryofOdds.Count; i++)
{
if (i == 0)
Console.WriteLine("Odd Numbers are:-");
Console.Write(arryofOdds[i].ToString() + " ");
}

Console.WriteLine("Even sum:-" + evensum + " Odd sum:- " + oddsum+" Odd 3s sum:- " + sumof3s);
Console.ReadLine();

Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 8/23/2011 [Member] Starter | Points: 25

Up
0
Down
int MaxBound = 20;

var evens = Enumerable.Range(1, MaxBound).Where(a => a % 2 == 0).ToArray();
foreach (var item in evens)
{
Console.Write(item + " ");
}
Console.WriteLine();
var numendswith3 = Enumerable.Range(1, MaxBound).Where(a => a % 10 == 3).ToArray();
foreach (var item in numendswith3)
{
Console.Write(item + " ");
}


Naga116, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response