Write a menu driven 'C' program , which prints the options 1,2,3 (as given below) and asks the user to select an option. If user options :
1 then find the factors of the entered number
2 then check whether the entered number is prime number or not
3 then directly exit from the program
The menu should be displayed by a function.
#include <stdio.h>
#include <conio.h>
#include <process.h>
void factor ( int n )
{
int i;
printf("\n\t\t The factors are..:");
for ( i = 1; i <= n; i++ )
{
if ( n % i == 0 )
{
printf("%d, ",i);
}
}
}
int prime ( int n )
{
int i;
for ( i = 2; i < n; i++ )
{
if ( n % i == 0 )
return 0;
}
return 1;
}
void menu ( )
{
printf("\n\n\t\t*********************************************\n\n");
printf("\n\t\t PRESS 1 TO FIND FACTORS\n");
printf("\n\t\t PRESS 2 TO CHECK IF THE NUMBER IS PRIME\n");
printf("\n\t\t PRESS 3 TO EXIT\n");
printf("\n\n\t\t*********************************************\n\n");
}
void main ( )
{
int n, ch;
clrscr ( );
while ( 1 )
{
clrscr ( );
menu ( );
printf("\n\t\t Enter your choice..:"); scanf("%d",&ch);
switch ( ch )
{
case 1:
printf("\n\t\t Enter a number..:"); scanf("%d",&n);
factor ( n );
break;
case 2:
printf("\n\t\t Enter a number..:"); scanf("%d",&n);
if ( prime ( n ) )
printf("\n\t\t The number is prime.");
else
printf("\n\t\t The number is not prime.");
break;
case 3:
exit ( 0 );
default:
printf("\n Please enter a correct choice.");
}
getch ( );
}
}
Click here to contact us
1 then find the factors of the entered number
2 then check whether the entered number is prime number or not
3 then directly exit from the program
The menu should be displayed by a function.
#include <stdio.h>
#include <conio.h>
#include <process.h>
void factor ( int n )
{
int i;
printf("\n\t\t The factors are..:");
for ( i = 1; i <= n; i++ )
{
if ( n % i == 0 )
{
printf("%d, ",i);
}
}
}
int prime ( int n )
{
int i;
for ( i = 2; i < n; i++ )
{
if ( n % i == 0 )
return 0;
}
return 1;
}
void menu ( )
{
printf("\n\n\t\t*********************************************\n\n");
printf("\n\t\t PRESS 1 TO FIND FACTORS\n");
printf("\n\t\t PRESS 2 TO CHECK IF THE NUMBER IS PRIME\n");
printf("\n\t\t PRESS 3 TO EXIT\n");
printf("\n\n\t\t*********************************************\n\n");
}
void main ( )
{
int n, ch;
clrscr ( );
while ( 1 )
{
clrscr ( );
menu ( );
printf("\n\t\t Enter your choice..:"); scanf("%d",&ch);
switch ( ch )
{
case 1:
printf("\n\t\t Enter a number..:"); scanf("%d",&n);
factor ( n );
break;
case 2:
printf("\n\t\t Enter a number..:"); scanf("%d",&n);
if ( prime ( n ) )
printf("\n\t\t The number is prime.");
else
printf("\n\t\t The number is not prime.");
break;
case 3:
exit ( 0 );
default:
printf("\n Please enter a correct choice.");
}
getch ( );
}
}
Click here to contact us
No comments:
Post a Comment