Find prime divisors of a number

Started by thiruvasagamani, Sep 22, 2008, 11:56 AM

Previous topic - Next topic

thiruvasagamani

Find prime divisors of a number

#include <stdio.h>
#include <conio.h>

void primediv( int n)
{
int i= 2; // starting divisor is 2
while( n > 1) // execute loop until n = 1
{
if( n % i == 0) // if 'i' is divisor of 'n', display 'i'
{
printf("%d\n", i);
n = n / i ; // modify 'n' value to avoid infinite loop
}
else
i++; // if 'i' is not divisor , go to next divisor
}
}

main()
{
int n;
printf( enter a value");
scanf("%d", &n);
clrscr();
primediv( n); // display prime divisors of 'n'
getch();
}


o/p:
If you enter 'n' value as 2100 output is 2 , 2 , 3 , 5, 5, 7

Display mathematical table for 1 to n page wise

#include <stdio.h>
#include <conio.h>

void table( int n)
{
int j;
for(j = 1; j <= 10 ; j++ ) // display 10 rows in table
printf("%d * %d = %d \n", n , j , n * j); // display table num, row num, result n* j
}

main()
{
int i, n, void table( int);
printf(" enter number of tables \n");
scanf("%d", &n);
clrscr();
for( i= 1; i<= n; i++) // display tables 1 to n
{
printf(" table %d \n", i); // display heading consists of table number
table( i); // display table for 'i' where 'i' varies from 1 to n
printf(" wait for 5 seconds\n");
sleep( 5); // execute next statement after 5 seconds
clrscr(); // clear current table before displaying next table
}
}


o/p:
Suppose if you enter 'n' value as 5 output displays tables from 1 to 5

NOTE:

Sleep() :
syntax: sleep( seconds);
eg:
sleep( 5) next executed after 5 seconds, execution stops temporarily for 5 seconds.Sleep takes seconds parameter.
delay() take milli seconds parameter
delay(1500) is correct but not sleep( 1.5), sleep takes integer parameter , 1000 ms is 1 second
Thiruvasakamani Karnan