Eleven Years of Service
Posts: 98
Threads: 10
RE: [Challenge:1] Multiples of 3 and 5 12-08-2016, 05:35 PM
#17
(12-08-2016, 09:36 AM)Bish0pQ Wrote: C#
Spoiler:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Multiples
{
class Program
{
static void Main(string[] args)
{
//Variables
int intSum = 0;
string strSum = string.Empty;
//Calculate sum
for (int i = 0; i < 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
intSum += i;
strSum = intSum.ToString();
}
}
Console.WriteLine("The sum is {0}", strSum); //Write sum result
//Reverse the number (another loop, boring I know)
string strSumReversed = string.Empty;
int intCount = strSum.Length;
for (int x = 0; x < strSum.Length; x++)
{
strSumReversed = strSumReversed + strSum.Substring(intCount -1, 1);
intCount = intCount - 1;
}
Console.WriteLine("The sum reversed is: {0}", strSumReversed); //Write reversed result
Console.ReadLine();
}
}
}
String's are immutable in .NET, but why didn't you use the for loop variable here?
Code:
for (int x = 0; x < strSum.Length; x++)
{
strSumReversed = strSumReversed + strSum.Substring(intCount -1, 1);
intCount = intCount - 1;
}
Instead of keeping track of your own variable?
(This post was last modified: 12-08-2016, 05:35 PM by bitm0de.)
- mostly braindead monkeys on this forum.
•