0
Need help for C# practice 18.3
Hi everyone, If anyone have done the practice 18.3 please show me how, I have been working for over 1 hour and still don't get it, Thanks a lot A store is running a unique promotion. If an item's price is an odd number, the item is free. The program you are given takes the number of purchased items as input, followed by the prices of each of the items, then outputs the total amount. Modify the code to skip the odd prices and exclude them in total amount calculation. Sample Input 3 140 235 100 Sample Output 240 Explanation The first input represents the number of purchased items, followed by their prices. Because 235 is an odd number, only 140 and 100 should be summed: 140+100 = 240.
6 Answers
+ 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] n = {3,140,235,100};
int final = 0;
for (int x = 0; x<n.Length; x++)
{
if (n[x]%2==0)
{
final+=n[x];
}
}
Console.WriteLine(final);
}
}
}
I dont know how its done in the practice but heres something
0
Please post your attempt....
0
Hint:
<price> % 2 yields non zero value when <price> is an odd value.
0
Thank you guy
0
if ((price%2) == 0){
totalAmount += price;
}