Write a vb.net program that uses a sub procedure to calculate and sum of all even numbers between 1 and n where n is supplied by the user
Question
Write a vb.net program that uses a sub procedure to calculate and sum of all even numbers between 1 and n where n is supplied by the user
Solution
Sure, here is a simple VB.NET program that uses a sub procedure to calculate the sum of all even numbers between 1 and n.
Imports System
Module Program
Sub Main(args As String())
Console.Write("Enter a number: ")
Dim n As Integer = Integer.Parse(Console.ReadLine())
Dim sum As Integer = SumOfEvenNumbers(n)
Console.WriteLine("The sum of all even numbers from 1 to " & n & " is " & sum)
End Sub
Function SumOfEvenNumbers(n As Integer) As Integer
Dim sum As Integer = 0
For i As Integer = 1 To n
If i Mod 2 = 0 Then
sum += i
End If
Next
Return sum
End Function
End Module
This program works as follows:
- It prompts the user to enter a number.
- It reads the user's input and converts it to an integer.
- It calls the
SumOfEvenNumbers
function, passing the user's number as an argument. - The
SumOfEvenNumbers
function calculates the sum of all even numbers from 1 to the user's number. - It prints the result to the console.
Similar Questions
Write an assembly language program for finding out alleven numbers among 10 eight bit numbers
Using recursion, debug the following program to find 1+2+...+N, where N is obtained from user.Sample Input:4Sample Output:The result is 10
Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.
Write the program to enter integer elements from user in singly linked list. User can enter both andeven values.
Which of the following pseudo code snippets correctly calculates the sum of numbers from 1 to n using recursion?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.