G
G
gleb_oj2020-06-12 16:55:00
C++ / C#
gleb_oj, 2020-06-12 16:55:00

For some reason, the program only lists numbers, although it should find the sum of numbers that are divisible by the specified number, how to fix it?

using System;

namespace ConsoleApp19
{
    class Program
    {
        public static char d;

        public static void Main(string[] args)
        {
            Console.WriteLine("Введите начальное число");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите конечное число");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите кратное число");
            int c = Convert.ToInt32(Console.ReadLine());
            int sum = 0;
            int s = a;
            while (s < b)
            {
                int k = s % c;
                switch (d)

                {
                    case '%':
                        k = 0;
                        break;
                }
                sum = sum + k;

                Console.WriteLine(sum);
                s++;
            }
        }   
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
#
#, 2020-06-12
@gleb_oj

the code
using System;

namespace sum_of_dividends
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Введите начальное число");
            var a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите конечное число");
            var b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите кратное число");
            var c = Convert.ToInt32(Console.ReadLine());

            var sum = 0;
            for (var x = a; x <= b; x++)
                if ((x % c) == 0)
                    sum += x;

            Console.WriteLine(sum);
        }
    }
}
exhaust
eY84w6Q.png
p.s. I went a little overboard. Now I've simplified it.. and more..
ps optional switch
using System;

namespace sum_of_dividends
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Введите начальное число");
            var a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите конечное число");
            var b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите кратное число");
            var c = Convert.ToInt32(Console.ReadLine());

            var sum = 0;
            for (var x = a; x <= b; x++)
                switch (x % c)
                {
                    case 0: sum += x; break;
                    default: break;
                }

            Console.WriteLine(sum);
        }
    }
}
exhaust
blK6WLW.png
ps then for the sake of completeness and while
using System;

namespace sum_of_dividends
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Введите начальное число");
            var a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите конечное число");
            var b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Введите кратное число");
            var c = Convert.ToInt32(Console.ReadLine());

            var sum = 0;
            var x = a;
            while (x <= b)
            {
                switch (x % c)
                {
                    case 0: sum += x; break;
                    default: break;
                }
                x++;
            }

            Console.WriteLine(sum);
        }
    }
}
exhaust
8OSpPXo.png
also corrected. so more reliable

D
Dmitry Pavlov, 2020-06-12
@Stalker31

For starters, can you transfer
Console.WriteLine(sum);
under
while(s<b){}

A
AisenC, 2020-06-12
@AisenC

int sum = 0; it is better to move
Console.WriteLine(sum); move from loop visibility to method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question