﻿ Programme to Print Mulitples of 5 in a Range Specified by User - ITCodar

# Programme to Print Mulitples of 5 in a Range Specified by User

## Programme to print mulitples of 5 in a range specified by user

If your expected output from the range 6 to 21 is `[10, 15, 20]`, you can first test if the starting point is a multiple of 5 as you did with `if a % 5 == 0`, and if it is true, we can add 5 repeatedly until we hit the end of range, while appending all the steps into the output list.

However if `a % 5 == 0` gives you false, then we need to find the value to add to this starting point so that we can get the first multiple of 5 in this range, i.e.

``diff = 5 - a % 5``

and add this value to the starting point `a`, we get the first value:

``first = a + diff``

Then we can add 5 repeatedly until we hit the end of range.

To put all the above into code:

``# get user input for the range a to ba = int(input("Enter a value of a : "))b = int(input("Enter a value of b : "))output_list = []# determine first valueif a % 5 == 0:     first_val = aelse:     first_val = a + (5 - (a % 5))# go through the range between the first value and the end, with step=5for val in range(first_val, b + 1, 5):    # append value to the list    output_list.append(val)print(output_list)``

## How to write the program that prints out all positive integers between 1 and given number , alternating between the two ends of the range?

This should work:

``num = int(input("Please type in a number:"))number_list = [i+1 for i in range(num)]while number_list:    print(number_list.pop(0))    number_list.reverse()``

## write a program that will accept an integer value in the range of 5-95 and as a multiple of 5

Here is a program that does what you ask. This program could still be improved vastly.

``#include <stdio.h>// Delcaration of functionsint coinReturn(int coinSize, int value){    int tmp = 0;    while(value >= coinSize)    {        value-=coinSize;        tmp++;    }    printf("Number of %i cent coins are: %d\n", coinSize,tmp);    return value;}int main(){    // Declare and initialize working storage    int user_input = 0;    int remainingValue;    // Prompt user for input, prints, and loops for 3 attempts    while (1)    {        printf("\nPlease enter an amount within the range of 5 to 95\n");        printf("\nPlease enter a multiple of 5\n");        printf("\nPlease enter the amount you wish to change: \n");        scanf("%d", &user_input);        if ( (user_input < 5 || user_input > 95) || (user_input % 5 != 0))        {            printf("\nInvalid input!\n");        }        else        {            break;        }    }    //Calculate and Print Output    remainingValue = coinReturn(50,user_input);    remainingValue = coinReturn(20,remainingValue);    remainingValue = coinReturn(10,remainingValue);    remainingValue = coinReturn(5,remainingValue);    return 0;}``

I would focus on a couple things if I were you and make sure I learned the concept. These are:
- Break Statements
- Function Declarations and function Definitions
- Modulo
- Flowcharting

This problem can be slightly tricky and flowcharting it when you start is your best bet.

## C Program that prints Multiples of 3, 5 and both, professional style suggestion [closed]

This is basically Fizzbuzz where you should print FizzBuzz if it is dividable by both 5 and 3, Fizz if it is dividable by 5 but not 3 and Buzz if it is dividable by 3 but not 5.

TL;DR

There is no "right" way to do this. No matter how you do it you will break some best practices.

Long explanation

Note!

I'm the kind of programmer who prefers a flexible approach to best practices and code standards. This is not right or wrong, but the whole following text is colored by my personality. I pretty much view this test as "can you understand when to skip best practices" while other people would view it like "can you figure out how to follow best practices even in tricky cases". None is more right that the other.

This problem is intended to be very easy to understand, but pretty tricky to do it "nice". One thing you often will encounter in this example is duplicated code. You can quite easily build it away, but often at the cost of readability.

If I were to design a code that did what you ask, I would be perfectly happy with the solution you have provided and then move on to the next problem. It's not really worth spending a lot of time on. That is also a thing employers look at when they give you this test. Do you spend hours and days just to make sure your code follows all "best practices" even if the gain is minuscule or do you create code that works and is readable and move on as soon as it is good enough?

Some examples I have seen to avoid first checking if it is dividable by both numbers and then check them separately is concatenating strings. Something like this pseudo:

``string str = ""if n % 5 = 0: str += "Fizz"if n % 3 = 0: str += "Buzz"print str``

Looks nice, right? Well translate it to real C where string handling is pretty messy.

``char str = "";if(n%5 == 0) strcat(str, "Fizz");if(n%3 == 0) strcat(str, "Buzz");puts(str);``

Does not look too bad though. But is it really worth it? And what if you want to change "Fizz" and "Buzz" to something longer? Then you need to make sure that `str` has more space, which is easy to forget and can cause hard traced bugs. I'm not saying that this code is extremely dangerous, but the bottom line here is how you reason. Is that risk really, really worth the effort of avoid some code duplication?

Some people refactor out the conditions as functions, like `bool dividable_by_15(int n)` because "it's good to break out functionality in separate functions". Some would even go as far as this:

``bool dividable_by(int n, int d) { return (n%d) == 0; }bool dividable_by_3(int n) { return dividable_by(n, 3); }bool dividable_by_5(int n) { return dividable_by(n, 5); }bool dividable_by_15(int n) { return dividable_by_3(n) && dividable_by_5(n); }``

But is it really needed in this case? I don't think so, but I would not say that the choice is 100% obvious, and it also depends on what language you're using. But for most cases I would say that this is a quite clear case of over engineering.

This test is not so much about seeing if you can follow all best practices. It's more of a personality test. Some employers want you to do all sorts of stuff, while others would prefer if you can just leave the code as it is when it does what it should.

When it comes to your code, I actually only have one objection, and that is that you're omitting the braces for the for loop. I'd never do that, except when the body is one simple line. However, I would omit the braces for the if statements. Some people would agree on the last, and some would not. Those who favor always using braces even for single statements often use the argument that it reduces the risk of bugs if you need to add an extra statement in the body. Another argument is consistency, that you always should strive to do the same thing everywhere. In my opinion, those factors are not worth the extra lines, but hey, that's me. You do you.

``for (int i = 1; i <= n; i++) {    if (i % 15 == 0)         printf("Multiple of 3 and 5\n");    else if (i % 5 == 0)        printf("Multiple of 5\n");    else if (i % 3 == 0)        printf("Multiple of 3\n");    else        printf("%d\n", i);}``

That looks much better to me. I would even consider having the if's as one liners:

``    if      (i % 15 == 0) printf("Multiple of 3 and 5\n");    else if (i % 5 == 0)  printf("Multiple of 5\n");    else if (i % 3 == 0)  printf("Multiple of 3\n");    else                  printf("%d\n", i);``

I would also consider extracting the newline, like this:

``    if      (i % 15 == 0) printf("Multiple of 3 and 5");    else if (i % 5 == 0)  printf("Multiple of 5");    else if (i % 3 == 0)  printf("Multiple of 3");    else                  printf("%d", i);    printf("\n");``

Since it's only a new line, you could change `printf("\n")` to just `puts("")`.

BUT - here is the thing - In my opinion, I have already spent far to much energy on this problem. :)

As a perfectionist, I'm trying to implement highest best standards

I read a quote that answers this perfectly: "Blindly following best practices is not best practice"

"Best practices" have the purpose of offering a very simple way to fulfill some goal. If your code fulfill that purpose without following best practices, why change it? Especially if it fulfills that purpose even better.

## Count multiple of a number in a given range with O(1) complexity?

`` quo1=l/k; quo2=r/k; rem=l%k; if(rem==0) {   count=quo2-quo1+1; } else {    count=quo2-quo1;  }   ``

## finding multiples of a number in Python

If you're trying to find the first `count` multiples of `m`, something like this would work:

``def multiples(m, count):    for i in range(count):        print(i*m)``

Alternatively, you could do this with range:

``def multiples(m, count):    for i in range(0,count*m,m):        print(i)``

Note that both of these start the multiples at `0` - if you wanted to instead start at `m`, you'd need to offset it by that much:

``range(m,(count+1)*m,m)``