Resolved C# Homework

nereloyka13

Member
Joined
Jan 21, 2022
Messages
5
Programming Experience
Beginner
Hi, I had homework questions I couldn't do it myself, can you help?



Question 1. a program that takes the square of all the numbers starting from 1 and ending with the entered number and prints the squares of the numbers and the sum of their squares from the screen.I was able to print their squares, but I couldn't take and print their totals.



Question2. Console.WriteLine(Result(2,5,7)); //((2+3+4+5)+ 7*7)=63 I couldn't do anything about it, can you help me.
 
We are not a code writing service. We won't do your homework for you. You need to show some effort and we can help you with specific issues that you run into.
 
We are not a code writing service. We won't do your homework for you. You need to show some effort and we can help you with specific issues that you run into.
i do it first question
C#:
namespace homework
{
    class Program
    {
        static void Main(string[] args)
        {
            int karesi;
            Console.Write("Enter a number: ");
            int sayi = int.Parse(Console.ReadLine());
            for (int i = 1; i <= sayi; i++)
            {
                karesi = i * i;
                Console.WriteLine(karesi);
            }
            int toplam = 0;
            for (int i = 1; i <= sayi; i++)
            {
                toplam += i * i;
            }
            Console.WriteLine("the sum of the numbers from 1 to the entered value:");
            Console.WriteLine(toplam);
            Console.ReadKey();
        }
    }
}
i did the first question, but I have no idea about the second question i can be glad if you give me a hint
 
Last edited by a moderator:
We are not in your class, but I'm guess that is your teacher's way of asking that you write a method named Result that takes 3 integer parameters, does the operation in the comment, and returns the computed value. Is that his/her style?
 
Ok. So how would you go about writing that method?
 
Ok. So how would you go about writing that method?
C#:
namespace result
{
    class Program
    {
        static int Sonuc(int a, int b, int c)

        {
            int sqr = 0;
            int total = 0;
            for (int i = a; i <= b; i++)
            {
                total += i;
            }
            if(c == null)
            {
                //???????????
            }
            sqr = c * c;
            total += sqr;
            return total;
        }
        static void Main(string[] args)
        {
            Console.Write("Sayı girin: ");
            int sayi = Convert.ToInt32(Console.ReadLine());
            Console.Write("Sayı girin: ");
            int sayi2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Sayı girin: ");
            int sayi3 = Convert.ToInt32(Console.ReadLine());
            int ttl = Sonuc(sayi, sayi2, sayi3);
            Console.WriteLine("Girdiginiz sayıların toplamı  " + ttl);
        }
    }
how can I print the sum of the numbers from the first number to the second number without squaring it when the value c is not entered here.
example: sayi =5 sayi2=7 //5+6+7=19
 
Congratulations!!! You did it! You didn't even need or help.

You are worrying about a non-event. int can never be null.
 
Solution

Latest posts

Back
Top Bottom