Question

In: Computer Science

In this example you are allowed to use from the C standard library only functions for...

In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())

Complete the following functions using C programming language:

  • intQ1_for()
  • intQ1_while()
  • intQ1_do()

To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in three different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value on the screen. Return the total sum at the end of each function

Solutions

Expert Solution

Code Explanation:

  • Declare the functions
  • In the main function with in the printf statement call the three functions
  • In the Q1_for() function Initially declare sum =0,Then use for loop from 30 to 1000 and and check if i is divisible by 4 if yes then add it to sum
  • After loop exists return sum
  • In the Q1_while() initially initialize i =30 and sum =0.
  • Run a while loop with condition 1<=1000. SO when i is greater than 1000 the loop exists else it checks the i%4==0 and if yes then adds the i to sum
  • When loop exists return sum
  • do while and while loop difference is here in do while first we perform the body part and then check condition

Code:

#include <stdio.h>
#include<conio.h>
int Q1_for();
int Q1_while();
int Q1_do();
void main()
{
printf("%d\n",Q1_for());
printf("%d\n",Q1_while());
printf("%d\n",Q1_do());

}

int Q1_for(){

int sum = 0, i;;
for(i = 30; i<=1000; i++){
if(i%4==0){
sum = sum +i;
}
}
return(sum);

}


int Q1_while(){

int i=30, sum = 0;
while(i<=1000){

if(i%4==0){
sum = sum+i;
}
i++;
}
return(sum);

}

int Q1_do(){

int i = 30, sum = 0, temp;
do{
if(i%4==0){
sum = sum + i;
}
i++;
}
while(i<=1000);

return(sum);
}

O/P:

125388
125388
125388

Code screenshot:

O/P screenshot:

(If you still have any doubts please comment I will definitely help)


Related Solutions

In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to...
Look up the man pages for the following string functions from the C standard library, and...
Look up the man pages for the following string functions from the C standard library, and write implementations of each: strstr() Please explain the work please, thank you
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from...
Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from the .txt file and sort the names in lexical order Grumpy Dopey Doc Happy Bashful Sneezy Sleepy
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT