W rite a program to compute the sum of the int e gers that divisible by 4 from 10…
- User
W rite a program to compute the sum of the int e gers that divisible by 4 from 10 different integers (using DO/WHILE loop and FOR loop) .
- Assistant
Here is a program that computes the sum of integers divisible by 4 using a
do/whileloop and aforloop:using System;
namespace SumOfIntegersDivisibleBy4 { class Program { static void Main(string[] args) { //using do/while loop int sumDoWhile = 0; int countDoWhile = 0;
Console.WriteLine("Using do/while loop:"); do { Console.Write("Enter an integer: "); int num = Convert.ToInt32(Console.ReadLine()); if (num % 4 == 0) { sumDoWhile += num; countDoWhile++; } } while (countDoWhile < 10); Console.WriteLine("Sum of integers divisible by 4 using do/while loop: " + sumDoWhile); //using for loop int sumFor = 0; Console.WriteLine("\nUsing for loop:"); for (int i = 0; i < 10; i++) { Console.Write("Enter an integer: "); int num = Convert.ToInt32(Console.ReadLine()); if (num % 4 == 0) { sumFor += num; } } Console.WriteLine("Sum of integers divisible by 4 using for loop: " + sumFor); Console.ReadLine(); } } - User
باc++
- Assistant
Certainly! Here is the equivalent C++ program that computes the sum of integers divisible by 4 using a do/while loop and a for loop:
#include <iostream> using namespace std; int main() { // Using do/while loop int sumDoWhile = 0; int countDoWhile = 0; cout << "Using do/while loop:" << endl; do { cout << "Enter an integer: "; int num; cin >> num; if (num % 4 == 0) { sumDoWhile += num; countDoWhile++; } } while (countDoWhile < 10); cout << "Sum of integers divisible by 4 using do/while loop: " << sumDoWhile << endl; // Using for loop int sumFor = 0; cout << "\nUsing for loop:" << endl; for (int i = 0; i < 10; i++) { cout << "Enter an integer: "; int num; cin >> num; if (num % 4 == 0) { sumFor += num; } } cout << "Sum of integers divisible by 4 using for loop: " << sumFor << endl; return 0; }In this C++ program, the logic is similar to the C# program. The user is prompted to enter 10 integers, and the program checks if each number is divisible by 4 using the modulus operator (%). If the number is divisible by 4, it is added to the sum variable. Finally, the sum is printed to the console.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.