0% found this document useful (0 votes)
4 views

Code

The program prompts the user to enter an integer between 1 and 500. It then checks if the number is odd or even, calculates the sum of odd numbers and product of even numbers up to the input number, and displays the results.

Uploaded by

ulthrone
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code

The program prompts the user to enter an integer between 1 and 500. It then checks if the number is odd or even, calculates the sum of odd numbers and product of even numbers up to the input number, and displays the results.

Uploaded by

ulthrone
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

class Program

static void Main(string[] args)

// Prompt the user to enter an integer between 1 and 500

Console.WriteLine("Enter an integer between 1 and 500:");

int number = Convert.ToInt32(Console.ReadLine());

// Check if the entered number is within the valid range

if (number < 1 || number > 500)

Console.WriteLine("Please enter a number between 1 and 500.");

return; // Exit the program if the number is not within the valid range

// Display whether the number is odd or even

if (number % 2 == 0)

Console.WriteLine($"The number {number} is even.");

else

Console.WriteLine($"The number {number} is odd.");

// Initialize variables to store the sum of odd numbers and product of even numbers

int sumOfOdd = 0;
int productOfEven = 1;

// Loop through numbers from 1 to the input number

for (int i = 1; i <= number; i++)

// Check if the current number is odd

if (i % 2 != 0)

sumOfOdd += i; // Add the odd number to the sumOfOdd

// Check if the current number is even

else

productOfEven *= i; // Multiply the even number to the productOfEven

// Display the sum of all odd numbers up to the input number

Console.WriteLine($"Sum of all odd numbers up to {number}: {sumOfOdd}");

// Display the product of all even numbers up to the input number

Console.WriteLine($"Product of all even numbers up to {number}: {productOfEven}");

You might also like