I would like the perfect numbers and the twirly working without the program havi
ID: 3620014 • Letter: I
Question
I would like the perfect numbers and the twirly working without the program having to put the user input. I should be automatically displayed on the screen.Q. Use a "twirly" to indicate that your program is happily working away. A "twirly" is the following characters printed over the top of each other in the following order: '|' '/' '-' ''. This has the effect of producing a spinning wheel - ie a "twirly". Hint: to do this you can use (instead of ) in printf to give a carriage return only (instead of a carriage return linefeed). (Note: this may not work on some systems - you do not have to do it this way.) Notes The program has no user input.
#include <stdio.h>
static int perfect ( int n )
{
int i ;
int sum = 0 ;
for ( i = 1 ; i <= n / 2 ; ++i ) if ( n % i == 0 ) sum += i ;
return sum == n ;
}
static void twirl ( )
{
static char *str = "|/-\" ;
static int len = 4 ;
static int p = 0 ;
printf ( "%c " , str [ p ] ) ;
p = ++p % len ;
}
int main ( )
{
int i ;
for ( i = 1 ; i <= 10000 ; ++i )
{
if ( perfect ( i ) ) printf ( "%d " , i ) ;
if ( i % 100 == 0 ) twirl ( ) ;
}
return 0 ;
}
I would like the perfect numbers and the twirly working without the program having to put the user input. I should be automatically displayed on the screen.
Q. Use a "twirly" to indicate that your program is happily working away. A "twirly" is the following characters printed over the top of each other in the following order: '|' '/' '-' ''. This has the effect of producing a spinning wheel - ie a "twirly". Hint: to do this you can use (instead of ) in printf to give a carriage return only (instead of a carriage return linefeed). (Note: this may not work on some systems - you do not have to do it this way.) Notes The program has no user input.
#include <stdio.h>
static int perfect ( int n )
{
int i ;
int sum = 0 ;
for ( i = 1 ; i <= n / 2 ; ++i ) if ( n % i == 0 ) sum += i ;
return sum == n ;
}
static void twirl ( )
{
static char *str = "|/-\" ;
static int len = 4 ;
static int p = 0 ;
printf ( "%c " , str [ p ] ) ;
p = ++p % len ;
}
int main ( )
{
int i ;
for ( i = 1 ; i <= 10000 ; ++i )
{
if ( perfect ( i ) ) printf ( "%d " , i ) ;
if ( i % 100 == 0 ) twirl ( ) ;
}
return 0 ;
}