modular calculator

Back to General discussions forum

radhitya     2024-07-24 15:39:10

i dont know where is the problem.. example was ok.. but when i tried to test, i got different value..

first one i use int, but i changed because i need more data value max.. but when i use long, the problem still appears

#include <stdio.h>
int main() {

    long nilai, hasil, modular,sisa;
    char tanda;

    scanf("%ld", &hasil);

    while(1) {
    scanf(" %c", &tanda);

    if(tanda == '%') {
    scanf("%ld", &modular);
    break;
    }

    scanf("%ld", &nilai);

    if(tanda == '*') {
    hasil*= nilai;
    }

    else if ( tanda == '+') {
    hasil += nilai;
    }
    }

    sisa = hasil % modular;
    printf("%ld\n", sisa);
    return 0;
}
Rodion (admin)     2024-07-24 15:46:51
User avatar

Hi Friend!

The problem text has a link to Modular Arithmetics article. You need to read and understand it in order to solve this problem.

Values are intentionally large so that no int or long can hold it. Some careless people use endless arithmetics (e.g. in Python) but there is "advanced" version of the problem where even this won't help.

This problem requirres a bit of thinking and understanding, rather than dull coding.

radhitya     2024-07-25 02:48:37

okay admin, thank you

98ranjitdkale     2024-10-23 12:20:42

I'm not able to select a whole values as 1 input

when i tried to get input as usual like line=input().split() it only gives me 1st line and not other

plese help

Rodion (admin)     2024-10-23 22:10:32
User avatar

try more input() statements. each of them reads one line if I remember correctly.

marvish_Taylor     2024-11-17 16:23:58

looks tricky but i can try it

utpaltripathi7     2026-10-04 09:57:54

I am trying to write this program in java and since the intermediate numbers are really big . I am using javafx.util.converter.BigIntegerStringConverter to convert the scanned text to a big integer. But I get compilation failed error. the program runs fine on my netbeans 8.1.

Product Version: NetBeans IDE 8.1 (Build 201510222201) Java: 1.8.005; Java HotSpot(TM) 64-Bit Server VM 25.5-b02 Runtime: Java(TM) SE Runtime Environment 1.8.005-b13 System: Windows 7 version 6.1 running on amd64; Cp1252; en_IN (nb)

import java.util.Scanner; import java.math.BigInteger; import javafx.util.converter.BigIntegerStringConverter; /** * * @author Utpal */ public class ModularCalculator {

public static void main(String args[]){

    BigInteger initialNum ;
    String artOpStr="";
    BigInteger secondNum;
    boolean inputAvailable = true;
    Scanner input = new Scanner(System.in);
    BigIntegerStringConverter bigIStrConv = new BigIntegerStringConverter();
    BigInteger bigInterim = BigInteger.ZERO ;
    BigInteger bigFinal =  BigInteger.ZERO;

    while (inputAvailable) {
        String next = input.nextLine();
        String varArray[];
        varArray = next.split(" ");
        if (varArray.length == 1) {
            initialNum = bigIStrConv.fromString(varArray[0]);
            bigInterim =  initialNum;
        } 
        else {
            artOpStr = varArray[0];
            secondNum = bigIStrConv.fromString(varArray[1]);
            if (artOpStr.equals("+")){
                bigInterim = bigInterim.add(secondNum);
            }
            else if (artOpStr.equals( "*")) {
                bigInterim = bigInterim.multiply(secondNum);
            }
            else if (artOpStr.equals( "%")) {
               bigFinal = bigInterim.mod(secondNum);
                inputAvailable = false;
            }
        }  

    }

   System.out.println(bigFinal);

}

}

Please login and solve 5 problems to be able to post at forum