For loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

For loops

The try yourself portion of the for loop that explains how to use for loops to give a range of numbers from 1-10 The try yourself wants me to take the input and set a range from 1-input and add all numbers in between to produce sum This is what ive written 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 num = Convert.ToInt32(Console.ReadLine()); /* finds numbers between 1 and num */ for (int sum=1; sum<=num;sum++) /* adds numbers between one and number ???? */ /* produce output*/ { Console.WriteLine(sum); } } } }

26th Oct 2020, 10:03 PM
Da SplitUpNinja
Da SplitUpNinja - avatar
5 Answers
+ 1
//OK. May not understood. Try this,.. Complete lessons. //Da SplitUpNinja 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 num = Convert.ToInt32(Console.ReadLine()); int sum=0; //I added this, initializing variable sum /* finds numbers between 1 and num */ for (int x=1; x<=num;x++) //taking x variable tempararly { sum = sum + x ; //adding all x values to sum } /* produce output */ Console.WriteLine(sum); } } }
26th Oct 2020, 10:34 PM
Jayakrishna 🇮🇳
0
You have taken input and you started a loop it will iterate fron 1 to num. So in loop you need to sum those numbers and after loop finish print the result. How you add 1 number to another? Just add that. Edit : oh. Da SplitUpNinja you made a new question. instead you can post in same question.. Remove one dublicate which you don't need..
26th Oct 2020, 10:19 PM
Jayakrishna 🇮🇳
0
ahhhh
26th Oct 2020, 10:37 PM
Da SplitUpNinja
Da SplitUpNinja - avatar
0
YOURE THE MAN!!!! You dont know how many times i was so close but i was like incrementaly off thank you so much
26th Oct 2020, 11:11 PM
Da SplitUpNinja
Da SplitUpNinja - avatar
0
and now that i see it its like wow... that was obvious
26th Oct 2020, 11:12 PM
Da SplitUpNinja
Da SplitUpNinja - avatar