Computer Science, asked by Vedang69, 1 year ago

PLz answer the attchment fast

Attachments:

Answers

Answered by rakeshchennupati143
4

sum = 0

i = 0

print("Natural number from 1- 10")

while i < 10:

     print(i)

     sum = sum + i

    i = i + 1

Print("sum of the 10 natural number")

print(sum)

NOTE: you did not mention the language so i wrote the code in python 3


TheKnowledge: can i apply in java ??
rakeshchennupati143: yes same logic can be applied in java
rakeshchennupati143: you need code in java?
Answered by poojan
5

Note:

Natural Numbers start from 1.

So a variable i (can take any identifier) should be initialized with 1.

‘Sum’ variable is taken to store the progressing addition of numbers continuously upto 10

===========

Language using: Python.

============

Program:

i = 1

Sum=0

// As we need sum of numbers upto 10

// Make sure to follow indentation

// Condition can also be written as i <=10

while i < 11:

print(i)

Sum=Sum+i

i = i + 1

// As i in while loop strikes 11, loop terminates.

Print(“Sum of first 10 natural numbers is”, Sum)

============

Output:

1

2

3

4

5

6

7

8

9

10

Sum of first 10 natural numbers is 55

=============

You can also input the values dynamically.

===========

Language: Java

============

Program:

public class A

{

public static void main( String[] rags)

{

i= 1;

Sum = 0;

while(i<=10)

{

System.out.println(i);

Sum=Sum+i;

}

System.out.println(“Sum of first 10 natural numbers is” + Sum);

}

}

=============

Output:

1

2

3

4

5

6

7

8

9

10

Sum of first 10 natural numbers is 55

=============

Indentation won’t be a problem in the compiling process, in java.

Similar questions