Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Intro to C

Name: Anonymous 2007-10-15 5:26

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;
}

Name: Anonymous 2007-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.

Here's a link to a related question in the comp.lang.c faq, which you should all read btw: http://c-faq.com/misc/shifts.html

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List