What does /prog/ think of my information on number code? These are the only tools we were allowed to use plus for loops but I felt like using while when doing this.
#include <stdio.h>
int main()
{
int num;
int down;
int up;
int count;
int fix;
while (num != -9876)
{
printf("\n-------------------------------------------------------\n\n");
printf("Enter a number to learn about or enter -9876 to quit: ");
scanf("%d", &num);
if (num == -9876)
{
return 0;
}
if (num < 0)
{
printf("\n\nPlease choose a positive number.\n\n");
}
else
{
printf("You chose the number: %d\n", num);
down = num-1;
up = num+1;
printf("The number before %d is %d\n", num, down);
printf("The number after %d is %d\n", num, up);
if (num % 2 == 0)
{
printf("%d is even.\n", num);
}
if (num % 2 != 0)
{
printf("%d is odd.\n", num);
}
count = 1;
fix = num;
printf("The next five numbers are: ");
while (count <=5)
{
count = count+1;
fix = fix+1;
printf("%d ", fix);
}
printf("\n");
if (num%5==0)
{
printf("%d is a multiple of five.\n", num);
}
fix = num;
fix = fix * fix;
printf("%d squared is %d.\n", num, fix);
count=0;
fix = 0;
up = 0;
while (fix <= num)
{
up = up + 1;
if ((num / (up*7)) > 0)
{
count = count + 1;
}
else
{
(fix = num + 1);
printf("Seven fits into %d %d time(s).\n", num, count);
}
}
fix = 0;
count=0;
while (count < num)
{
fix = fix+1;
if (fix*fix==num)
{
printf("%d is %d squared.\n", num, fix);
count=num+1;
}
if (fix*fix>num)
{
printf("%d is not a perfect square.\n", num);
count = num + 1;
}
}
count=0;
up=1;
while (count < num)
{
up = up+1;
if (num % up == 0 && up != num)
{
printf("%d is not prime.\n", num);
count = num + 1;
}
else
{
if (up >= num)
{
printf("%d is prime.\n", num);
count = num + 1;
}
}
}
}
}
return 0;
}
That's why the forced indentation of the code is a good thing.
Name:
Anonymous2007-10-15 5:55
Needs more forced indentation of code thread over.
Name:
Anonymous2007-10-15 6:01
I actually just erased the indentation in notepad when copying it to piss you guys off.
Name:
Anonymous2007-10-15 6:11
On the off chance this is real... comments in CAPS
#include <stdio.h>
int main () //USE A PROPER DECLARATION
{
int num;
int down;
int up;
int count;
int fix;
while (num != -9876)
//USES NUM UNITIALIZED THE FIRST TIME
//OTHERWISE WILL NEVER FAIL, STOP USING USELESS CONDITIONS
{
printf ("\n-------------------------------------------------------\n\n");
printf ("Enter a number to learn about or enter -9876 to quit: ");
//MERGE THE TWO PRINTFS
scanf ("%d", &num);
//NICE ERROR CHECKING
if (num == -9876)
//WHAT A FUCKING IDIOTIC END CONDITION
{
return 0;
//BREAK OUT OF THE LOOP, DON'T RETURN
//STRUCTURED PROGRAMMING MOTHERFUCKER
}
if (num < 0)
{
printf ("\n\nPlease choose a positive number.\n\n");
//READABILITY COULD BE IMPROVED BY USING CONTINUE HERE
}
else
//THAT WOULD MAKE THIS ELSE SUPERFLUOUS
{
printf ("You chose the number: %d\n", num);
down = num - 1;
up = num + 1;
//NICE USELESS VARIABLES
printf ("The number before %d is %d\n", num, down);
printf ("The number after %d is %d\n", num, up);
if (num % 2 == 0)
{
printf ("%d is even.\n", num);
}
if (num % 2 != 0)
//MAKE THIS AN ELSE RETARD
{
printf ("%d is odd.\n", num);
}
count = 1;
fix = num;
//ANOTHER USELESS VARIABLE
printf ("The next five numbers are: ");
while (count <= 5)
//GUESS WHY THEY INVENTED FOR LOOPS
{
count = count + 1;
fix = fix + 1;
printf ("%d ", fix);
}
printf ("\n");
if (num % 5 == 0)
{
printf ("%d is a multiple of five.\n", num);
}
fix = num;
fix = fix * fix;
//REALLY, STOP USING TEMPORARY VARIABLES
printf ("%d squared is %d.\n", num, fix);
count = 0;
fix = 0;
up = 0;
while (fix <= num)
{
up = up + 1;
if ((num / (up * 7)) > 0)
{
count = count + 1;
}
else
{
(fix = num + 1);
printf ("Seven fits into %d %d time(s).\n", num, count);
}
}
//DIVISION IDIOT, USE IT
fix = 0;
count = 0;
while (count < num)
//FUCKING USELESS CONDITION, USE BREAKS
{
fix = fix + 1;
if (fix * fix == num)
{
printf ("%d is %d squared.\n", num, fix);
count = num + 1;
}
if (fix * fix > num)
//ELSE IF MOFO
{
printf ("%d is not a perfect square.\n", num);
count = num + 1;
}
}
//IMPROVE THE ALGORITHM, IT LOOKS LIKE SHIT
count = 0;
up = 1;
while (count < num)
//USELESS CONDITION
{
up = up + 1; //YOU REALLY NEED TO CHECK ALL EVEN NUMBERS AMIRITE
if (num % up == 0 && up != num)
//UP != NUM IS A USELESS CHECK
{
printf ("%d is not prime.\n", num);
count = num + 1;
}
else
{
if (up >= num)
//PUT ELSE IF ON ONE LINE
//ALSO CHECK IF UP * UP > NUM
{
printf ("%d is prime.\n", num);
count = num + 1;
}
}
}
//CHOOSE A BETTER ALGORITHM
//OR AT LEAST MERGE IT WITH THE PREVIOUS ONE
}
}
>>14
well suppose 00 00 00 01
Depending on endianess it could be 01 00 00 00 or 00 00 00 01
Now, 1 would be 01 00 00 00 or 00 00 01 oh WAIT SNAP no endianess issues.
fuck.
Has anyone checked the output of GCC (with -O2/O3) to see if there's any difference in the code generation between & 1 and % 2? It seems to me the compiler would optimize those to the same assembly.
>>18 the compiler cannot do that.
Why can't the compiler convert between two mathematically equivalent expressions, exactly?
Name:
Anonymous2007-10-15 17:06
>>18
you don't understand sag- erm, I mean, compilers. yes.
Name:
Anonymous2007-10-15 17:14
Okay, >>10,15-18 , you fucking angered an EXPERT PROGRAMMER.
Binary and (&) is an operation on numbers, not bytes, endianness has nothing to do with it. Compiler writers are ten times as smart as you, so yes they always do simple optimizations like shifting or anding instead of division or modulus where applicable. Note that this is often not possible on signed types.
The compiler cannot do it in signed types.
If your compiler does it in signed types as well, it's broken.
Also, i was reffering to OPs code when i said "it can't"
Notice the OP is using plain int's (signed int's)
An int is always signed except if specified otherwise OR inside bitfield.
C89 had negative % n left unspecified.
>>22
Oh you leet haqr!
your code needs more non-std functions and invalid main prototypes.