What's new

Closed Patulong mga paps! c#

Status
Not open for further replies.

xxxolayxxx

Addict
Joined
Sep 20, 2018
Posts
12
Reaction
0
Points
71
Age
24
MGA PAPS! PA-HELP!
-
Ang problem nya kapag idididplay na nya yung EVEN may 0 sa unahan at hulihan. Paano ba alisin yun. Sana may maka tulong salamat po!
-
int even = 0;
int odd = 0;
int total = 0;
int x = 0;
string y = Convert.ToString(x);
string z = Convert.ToString(x);
int i = 1;
Console.WriteLine("Please enter integers: Press 0 to exit..");
do
{
x=int.Parse(Console.ReadLine());
if(x%2==0)
{
even+=x;
y = y + " " + x; -------//Heto yung sinasabi ko.
}
else
{
odd+=x;
z = z +" "+x;
}
i++;
}while(x != 0);
Console.WriteLine("Series of Even: " + y);
Console.WriteLine("Sum of Even: " + even);
Console.WriteLine("Series of Odd: " + z);
Console.WriteLine("Sum of Odd: " + odd);
Console.WriteLine("Sum of all series: "+ (total=even+odd));
-
SAMPLE RUN:
Please enter integers: Press 0 to exit..
1
2
3
4
5
0
Series of Even: 0 2 4 0 <<<------ ayan bakit po may 0 pa?
Sum of Even: 6
Series of Odd: 1 3 5 0 <<<------ ayan may zero pa po sa huli?
Sum of Odd: 9
Sum of all series: 15
 
parang may value na kasi 2ng dalawa pagka initialize pa lang..

string y = Convert.ToString(x);
string z = Convert.ToString(x);

di lng ako sure :D java kasi gamit ko .. wala bang null sa c# ?

or

string y = "";
string z = "";

or

just dont mind me :D
 
di ko sure pero
string y = Convert.ToString(x);
string z = Convert.ToString(x);
yan ata ung prob ts..
 
Mukhang sa logic ng do..while loop mo ang problem.

Try mo yung while loop lang.

while(x !=0)
{ //your codes here }
 
int even = 0;
int odd = 0;
int total = 0;
int x = 0;
string y = "";
string z = "";
int i = 1;
Console.WriteLine("Please enter integers: Press 0 to exit..");
do
{
x=int.Parse(Console.ReadLine());
if(x != 0){
if(x%2==0)
{
even+=x;
y = y + " " + x;
}
else
{
odd+=x;
z = z +" "+x;
}
}
i++;
}while(x != 0);
Console.WriteLine("Series of Even: " + y);
Console.WriteLine("Sum of Even: " + even);
Console.WriteLine("Series of Odd: " + z);
Console.WriteLine("Sum of Odd: " + odd);
Console.WriteLine("Sum of all series: "+ (total=even+odd));


OUTPUT:

Please enter integers: Press 0 to exit..
1
2
3
4
5
0
Series of Even: 2 4
Sum of Even: 6
Series of Odd: 1 3 5
Sum of Odd: 9
Sum of all series: 15


Learn from your mistakes :D
 
I'm still wondering para saan ang variable na 'i'.

yea, sabi nga nila.. naka initiate sa value of 'x' yung 'y' at 'z'.

here's mine using for loop..

int even = 0;
int odd = 0;
string x = "";
string y = "";
Console.Write("Enter number: ");
int input = int.Parse(Console.ReadLine());

for(int i = 1;i<= input; i++)
{
if (i % 2 == 0)
{
x += i + " ";
even += i;
}
else
{
y += i + " ";
odd += i;
}

}

Console.WriteLine("Series of Even numbers: {0}", x);
Console.WriteLine("Even: {0}", even);
Console.WriteLine("Series of Odd numbers: {0}", y);
Console.WriteLine("Odd: {0}", odd);
Console.WriteLine("Total: " + (even + odd));
 
Status
Not open for further replies.

Similar threads

Back
Top