Complete the given program code by creating the sumOfOdd function so that when program executes it will display the sum of the numbers of Odd or Even from 1 to the input value. B fun main() { print("Enter a number: ") val a = rdInt () print("Display the sum of odd numbers [Y/N]: ") val choice rd() var isOdd false if (choice.equals("Y")) isOdd = true sumofodd (isodd, a) //create this function } private fun rd() = readLine()!! private fun rdInt() = rd().toInt () Example Output 1 Enter a number: 7 Display the sum of odd numbers [Y/N]: Y The sum of odd numbers from 1 to 7 is 16 Example Output 2 Enter a number: 7 Display the sum of odd numbers [Y/N]: N The sum of even numbers from 1 to 7 is 12

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.2: Returning A Single Value
Problem 12E
icon
Related questions
Question
100%

Use Kotlin

1. Function: Sum of Odd or Even Numbers
Complete the given program code by creating the sumOfOdd function so that when
program executes it will display the sum of the numbers of Odd or Even from 1 to
the input value.
fun main() {
print("Enter a number: ")
val a = rdInt ()
print("Display the sum of odd numbers [Y/N]: ")
val choice = rd()
var isOdd = false
if (choice.equals("Y")) isOdd = true
sumOfOdd (isOdd, a) //create this function
readLine()!!
=
}
private fun rd()
=
private fun rdInt ()
rd().toInt ()
Example Output 1
Enter a number: 7
Display the sum of odd numbers [Y/N]: Y
The sum of odd numbers from 1 to 7 is 16
Example Output 2
Enter a number: 7
Display the sum of odd numbers [Y/N]: N
The sum of even numbers from 1 to 7 is 12
Transcribed Image Text:1. Function: Sum of Odd or Even Numbers Complete the given program code by creating the sumOfOdd function so that when program executes it will display the sum of the numbers of Odd or Even from 1 to the input value. fun main() { print("Enter a number: ") val a = rdInt () print("Display the sum of odd numbers [Y/N]: ") val choice = rd() var isOdd = false if (choice.equals("Y")) isOdd = true sumOfOdd (isOdd, a) //create this function readLine()!! = } private fun rd() = private fun rdInt () rd().toInt () Example Output 1 Enter a number: 7 Display the sum of odd numbers [Y/N]: Y The sum of odd numbers from 1 to 7 is 16 Example Output 2 Enter a number: 7 Display the sum of odd numbers [Y/N]: N The sum of even numbers from 1 to 7 is 12
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning