I need basic coding help Yea Forums

i need basic coding help Yea Forums.

i've been given task to make a program that will print out numbers in reverse with visible 0s and negative numbers and i'm given these restrictions:

i can only use do-while
i can only use one line of code to reverse the number and using division
i can only use long as a variable
i can't use if-else
i can't use string

i already made it and it works but i broke some of the restrictions and they wont accept it.

Attached: 1564225037021.jpg (499x491, 73K)

Other urls found in this thread:

geeksforgeeks.org/write-a-program-to-reverse-digits-of-a-number/
twitter.com/AnonBabble

Python:

number=input("enter string")
print(number[::-1])

>I can't use string
Oops I didn't see that.

Btw, which language?

c/c++

i was told that i can only use % and /

Did you mean this algo?

geeksforgeeks.org/write-a-program-to-reverse-digits-of-a-number/

this is exactly what i did, but when i enter negative numbers the calculation fucks up

Will basic work?

101
CLS
PRINT " (Q)uit"
PRINT " Your credit: "; credit
PRINT: PRINT
INPUT " (B)uy for 10 or (S)ell for 10"; bs$
bs$ = LCASE$(bs$)
IF bs$ = "b" THEN GOTO 2
IF bs$ = "s" THEN GOTO 3
IF bs$ = "q" THEN END ELSE GOTO 101

2
credit = buy
GOTO 101

3
credit = sell
GOTO 101

This should work with the Apple iigs, commodore 64 etc

Sounds like uni? I'm assuming it's not a company, they don't give this much of a fuck in the real world.

Learning how to use declare functions? Good Lord I don't miss college.

public static boolean containsNumbers(String str){
for(char ch : str.toCharArray()){
if(Character.isDigit(ch)){
return true;
}
}
return false;
}

Here ya go op good luck on your exam.

String str with declare etc

>I can't use string
Impossible

>hi I'm a freshman CS student and I'm already flunking
ftfy

one of the guys i'm with use only % and /
and using this format:
Do
{
formula
print
formula
}
while

fucking cunt don't wanna explain how he did it. but it outputs the right results.

I KNOW, but apparently it works :/

Do your own homework, you lazy tit.

Attached: 1547753649245.gif (234x249, 269K)

testVar = input
Print(number[::-1])

I actually had to fucking think. Op you need to slap you professor with your dick because this bullshit creates buffer overflows.

>i can only use one line of code
>c/c++

You do understand how much you can put onto a single line of C code, don't you?
Theoretically, the entire program can be one line.

i already did, but the out is not "right" somehow.

testVar=input ("Foo")|Print(number[::-1])

Here's the code in one line

Learn to do your own homework. This will create a memory hole. It's bad form and dirty code.

What is the required behaviour on negative numbers?

#include

int main() {
long in;
std::cout > in;
while (in!=0) {
std::cout

This is python. I don't code in c all that often.

If it's not "right", that means you didn't do your pointless homework.

cin is string

Holy shit I ran this and while it worked, it overheated my processor...

Op must be in college, only a Democrat professor would ask for hot garbage like this.

cout and cin are string variables.

if you entered
-14120
it should output
-02141

this is already quite close user thank you so much

cin and cout are basic stream objects and when operating on the console the stream object is bound to operate on char (note: not char[], which would be a string in c)

you're welcome

thank you for the explanation as well!

fixed it:
replace
in /= 10;
with
in = abs(in / 10);
and your are good to go

If child appears in thread.
>deepnude.exe

what? :/

>abs
what is this for?

why you asking us to do your first assignment of the semester?

As long as it works, you comment, and push all your commits.

to get the behaviour on negative numbers right

oh this needs right?

shouldn't... works for me with gcc / MinGW... used g++ to compile, didn't use -lm to link math

also didn't #include at all

oh okay, well thank you anyways!

C (GCC):

#include
#include

int main() {
long num;

scanf("%ld", &num);

do {
printf("%ld", num % 10);
num = labs(num / 10);
} while (num);

printf("\n");
}

gcc num.c && chmod +x a.out && ./a.out
(tried on macos with brew)

it won't print stuff like -00321

I'm not sure how to get around it without using if else. But since you didn't say you can't use the if else analogue... :)

#include
#include

int main() {
long num;

scanf("%ld", &num);

// print sign
printf("%s", num < 0 ? "-" : "");

do {
printf("%ld", num % 10);
num = labs(num / 10);
} while (num);

printf("\n");
}

ok this one is with any ifs and analogues
looks great, doesn't it?
it basically extracts the sign (which is just a bit set to either 1 or 0)
then it multiplies it by '-', meaning that printf will only print either the sign or nothing
still won't work if you enter numbers like -00123 (but I assume such numbers are invalid); will work for -12300

#include
#include

int main() {
long num;

scanf("%ld", &num);

// print sign
printf("%c", (char) ((unsigned long) '-' * ((unsigned long) num >> ((sizeof(long) * 8) - 1)) ));

num = labs(num);

do {
printf("%ld", num % 10);
num /= 10;
} while (num);

printf("\n");
}

>i can only use one line of code to reverse the number and using division
not sure i understand that restriction.
one line or one instruction? cause you dont nee dlines in C, that's not python.

Don't do this faggots homework anyone.

but anyway the basic idea is
int inputnegative = input < 0;
if (inputnegative)
input = -input;

int x = 0;
int cnt = 0;
while (input > 0) {
x *= 10;
x += input % 10;
input /= 10;
++cnt;
}

the reason we have cnt is because if input was, say, 100 then your output would be 1, but you want to print 001. cnt - how many digits you actually have is how many 0 you need to output.

btw
it's possible to do shit like
y = 1234
x = y / 1000 + // 1
y % 1000 / 100 * 10 + // 234 / 100 * 10 == 2 * 10 == 20
y % 100 / 10 * 100 + // 34 / 10 * 100 = 3 * 100 = 300
y % 10 * 1000 // 4 * 1000 = 4000
so you end up with 4321

but that depends on your initial length.

it's basically
sum of y % 10^n / 10^(n-1) for n in 1..number-of-digits

er, * 10^(number-of-digits - n), forgot about that

no ifs