1.8k views
0 votes
Create a script that will find the sum of the all multiples of 3 between 1 and 1000 by using For Loop. (Pseudo Code required) 3. Redo Problem 2 with While Loop and Do While Loop (Use Break at least once). (Pseudo Code required)

1 Answer

5 votes

Answer:

The code is given in the explanation while the screenshots of the output are attached along. The basic steps of the pseudo code are as follows.

  1. Initialized the variables (counter and summation)
  2. Initialize the loop(for, while, do while)
  3. Estimate the remainder of value with 3 and if the value is 0, this indicates the value is a multiple of 3 so add it to the sum
  4. Re-iterate this until the value is greater than 1000.
  5. Print the results.

Step-by-step explanation:

The code is given as below

With For Loop

#include<stdio.h> //The library is added

int main(){ //the main function is initialized

int sum=0,i; //The variables are initialized

for(i=1;i<=1000;i++){ //The for loop is initiated with iterations from 1 to 1000

//with and increment of 1

if(i%3==0) //Remainder of i by 3 is calculated to identify multiples of 3

sum=sum+i; //summation is done

}

printf("Using for loop : ");//results are displayed

printf("Sum is %d.\\",sum);

With While Loop

i=1; //counter variable is initialized

sum=0; //summation variable is initialized

//infinite loop which breaks only when i becomes >1000

while(i){

if(i>1000) //condition which breaks only when i becomes >1000

break;

if(i%3==0)//Remainder of i by 3 is calculated to identify multiples of 3

sum=sum+i; //summation is done

i++;//counter is incremented

}

printf("Using while loop : ");//results are displayed

printf("Sum is %d.\\",sum);//results are displayed

With Do While Loop

i=1;//counter variable is initialized

sum=0;//summation variable is initialized

do{

if(i>1000) //condition which breaks only when i becomes >1000

break;

if(i%3==0)//Remainder of i by 3 is calculated to identify multiples of 3

sum=sum+i;//summation is done

i++;//counter is incremented

}while(i);

printf("Using do-while loop : ");//results are displayed

printf("Sum is %d.\\",sum);

return 0;

}

Create a script that will find the sum of the all multiples of 3 between 1 and 1000 by-example-1
User AdibP
by
3.2k points