Programming in C
4th Edition
ISBN: 9780321776419
Author: Stephen G. Kochan
Publisher: Addison-Wesley
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 3, Problem 8E
Explanation of Solution
The following program will round off the value to the next largest even multiple of another value:
//include the header file
#include <stdio.h>
//definition of main method
int main (void)
{
//declare the variables
int i, j, ans;
//get the inputs from the user
printf("Enter the value to round off: ");
scanf("%d", &i);
printf ("Enter the value to make i divisible of...
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
Given an integer N and a base X, the task is to find the minimum number of operations required to represent N as a sum of the distinct powers of X. In each operation, you can either increment or decrement N. You are allowed to make the given operation any number of times
Examples:
Input: N = 7, X = 3 Output: 3.
Mathematics is a very crucial subject in our life. It gives a power to calculate the
daily things. Rajesh is the one of the brightest student in this subject so he has an
integer N. His task is to break N into some number of (integer) powers of 2. TO
achieve this, he can perform the following operation several times (possibly, zero):
Choose an integer X which he already has, and break X into 2 integer parts
(Y and Z) such that X = Y + Z.
Find the minimum number of operations required by Rajesh to accomplish his task
using Python Language.
Input
1
3
Output
1
I am what you call a perfectionist. I always strive for perfection, and I appreciate everyone and everything that is perfect. That is why I have recently acquired an appreciation for perfect numbers! I absolutely need to know which numbers from 1 to 1000 are considered perfect. From what I recall, a perfect number is a positive integer that is equal to the sum of all its divisors other than itself.
Example:
6 is a perfect number because the divisors of 6 other than itself are 1, 2, and 3, and 1 + 2 + 3 = 6.
c++ code
Knowledge Booster
Similar questions
- please read the question carefully and answer the question carefullyarrow_forwardO(nlgn) means that there is function f(n) that is O(nlgn) which is an upper bound for the running time at large n Select one: True Falsearrow_forwardplease read the question carefully and answer the question carefullyarrow_forward
- Description Xiao Zhang is often distressed because there are too many things to arrange. Now he has n tasks at hand, and each task has a starting times, and an ending time e;. To complete a task, he must do it from the starting time to the ending time, and Xiao Zhang can only perform one task at the same time. Xiao Zhang wants to know how many tasks he can accomplish at most. Input The first line is an integer n (1sns300000), indicating the number of tasks Xiao Zhang has at hand. In the next n lines, each line contains two integers s₁, e¡ (1≤s;arrow_forwardA formula for finding the greatest common divisor (GCD) of two numbers was formulated by the mathematician Euclid around 300 BCE. The GCD of two numbers is the largest number that will divide into both numbers without any remainder. For example, the GCD of 12 and 16 is 4, the GCD of 18 and 12 is 6.The basic algorithm is as follows:Assume we are computing the GCD of two integers x and y. Follow the steps below:1. Replace the larger of x and y with the remainder after (integer) dividing the larger number by the smaller one.2. If x or y is zero, stop. The answer is the nonzero value.3 If neither x nor y is zero, go back to step 1.Here is an example listing the successive values of x and y: x y135 20 %(135 / 20) = 15 15 20 %(20 / 15) = 5 15 5 %(15 / 5) = 0 0 5 GCD = 5 Write a recursive method that finds the GCD of two numbers using Euclid’s algorithm. public class Arithmetic{ public static int gcd(int a, int…arrow_forwardHALP m + n = 2k + 2k = 4k therefore adding even numbers always gives an even number. The above is wrong because.... it doesn't consider the case when adding to odd numbers m and n are different so you cannot use 2k for both it doesn't consider what m and n are for particular values in the integers it doesn't consider the case when m is even and n is oddarrow_forwardThe following function f uses recursion:def f(n):if n <= 1return nelse return f(n-1) + f(n-2)Let n be a valid input, i.e., a natural number. Which of the following functions returns the same result but without recursion?a) def f(n):a <- 0b <- 1 if n = 0return aelsif n = 1 return belsefor i in 1..nc <- a + b a <- b b <- c return bb) def f(n):a <- 0i <- n while i > 0 a <- a + i + (i-1) return ac) def f(n): arr[0] <- 0 arr[1] <- 1 if n <= 1return arr[n]elsefor i in 2..n arr[i] <- arr[i-1] + arr[i-2]return arr[n]d) def f(n): arr[0..n] <- [0, ..., n] if n <= 1return arr[n]elsea <- 0 for i in 0..n a <- a + arr[i]return aarrow_forwardThe following function f uses recursion: def f(n): if n <= 1 return n else return f(n-1) + f(n-2) Let n be a valid input, i.e., a natural number. Which of the following functions returns the same result but without recursion? a) def f(n): a <- 0 b <- 1 if n = 0 return a elsif n = 1 return b else for i in 1..n c <- a + b a <- b b <- c return b b) def f(n): a <- 0 i <- n while i > 0 a <- a + i + (i-1) return a c) def f(n): arr[0] <- 0 arr[1] <- 1 if n <= 1 return arr[n] else for i in 2..n arr[i] <- arr[i-1] + arr[i-2] return arr[n] d) def f(n): arr[0..n] <- [0, ..., n] if n <= 1 return arr[n] else a <- 0 for i in 0..n a <- a + arr[i] return aarrow_forwardThe following function f uses recursion: def f(n): if n <= 1 return n else return f(n-1) + f(n-2) Let n be a valid input, i.e., a natural number. Which of the following functions returns the same result but without recursion? a) def f(n): a <- 0 ъ <-1 if n = 0 return a elsif n = 1 return b else for i in 1..n C <- a + b a <- b b <- c return barrow_forwardThe following function f uses recursion:def f(n):if n <= 1return nelsereturn f(n-1) + f(n-2)Let n be a valid input, i.e., a natural number. Which of the following functions returns the same result but withoutrecursion?a) def f(n):a <- 0b <- 1if n = 0return aelsif n = 1return belsefor i in 1..nc <- a + ba <- bb <- creturn bb) def f(n):a <- 0i <- nwhile i > 0a <- a + i + (i-1)return ac) def f(n):arr[0] <- 0arr[1] <- 1if n <= 1return arr[n]elsefor i in 2..narr[i] <- arr[i-1] + arr[i-2]return arr[n]d) def f(n):arr[0..n] <- [0, ..., n]if n <= 1return arr[n]elsea <- 0for i in 0..na <- a + arr[i]return aarrow_forwardNeed help in python. Problem: 2D random walk. A two dimensional random walk simulates the behavior of a particle moving in a grid of points. At each step, the random walker moves north, south, east, or west with probability 1/4, independently of previous moves. Compose a program that takes a command-line argument n and estimates how long it will take a random walker to hit the boundary of a 2n+1-by-2n+1 square centered at the starting point. //Given codeImport stdioImport randomImport sysn = int(sys.argv[1])//write code herestdio.write('The walker took ')stdio.write(c)stdio.writeln(' steps')arrow_forwardle.com/forms/d/e/1FAlpQLSc6PlhZGOLJ4LOHo5cCGEf9HDChfQ-tT1bES-BKgkKu44eEnw/formResponse The following iterative sequence is defined for the set of positive integers: Sn/2 3n +1 ifn is odd if n is even Un = Using the rule above and starting with 13, we generate the following sequence: 13 u13 = 40 u40 =20 u20 = 10→ u10 =5 u5 = 16 u16 = 8 ug = 4 → Us =2 u2 =1. It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. The below function takes as input an integer n and returns the number of terms generated by the sequence starting at n. function i-Seq (n) u=n; i=%3; while u =1 if statement 1 u=u/2; else statement 2 end i=i+1; end statement 1 and statement 2 should be replaced by: None of the choices statement 1 is "mod(u,2)=D%3D0" and statement 2 is "u = 3*u+1;" statement 1 is "u%2" and statement 2 is "u = 3*u+1;" O statement 1 is "mod(n,2)=30" and statement 2 is "u = 3*n+1;"arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT