Fast Number Operations (Dev C++ Code)

jnbgames.dev 0 Tallied Votes 226 Views Share

The program below is written in C and is used to perform fast number operations. These operations are beneficial during the development of spreadsheet applications. Let me know if you find the code below helpful and if you have any additional suggestions:

/*-----------------------------------------------------------
 * Description: Program that executes fast number operations on three numbers.
 *-----------------------------------------------------------*/
#include <stdio.h>
int displayMenu(void);    // Functions Prototypes
void displayNumbers(void);
void sortNumbers(void);
double avgNumbers(void);
int minNumbers(void);
int maxNumbers(void);
int prime(int);
int perfect(int);
void input(void);
void init(void);
int n1=-1,n2=-1,n3=-1,choice; // n1,n2,n3 3 number type int initialized to -1 ; choice => is the menu choice 
int main(void) 
{
menu:	
	switch (displayMenu()) // switch to make the appropriate choice 
	{
		case 1:
			input();
			goto menu; // goto statement to take us back to menu 
			break;
		case 2:
			(n1==-1) ? puts("N1 : -1 N2 : -1 N3: -1") : displayNumbers();
			goto menu;
			break;
		case 3:
			sortNumbers();
			(n1==-1) ? puts("ERROR: No numbers entered.") : printf("The numbers after the sort are :\n->N1: %d\n->N2: %d\n->N3: %d\n ",n1,n2,n3);
			goto menu;
			break;
		case 4:
			(n1==-1) ? puts("ERROR: No numbers entered.") : printf("The average of the 3 numbers is %.2f\n",avgNumbers());
			goto menu;
			break;
		case 5:
			(n1==-1) ? puts("ERROR: No numbers entered.") : printf("The max is %d and the min is %d\n",maxNumbers(),minNumbers());
			goto menu;
			break;
		case 6:
			(n1==-1) ? puts("ERROR: No numbers entered.") : printf(" -> %d is %s\n -> %d is %s\n -> %d is %s\n",n1, prime(n1)==1 ? "a prime number" :"not a prime number",n2,prime(n2)==1 ? "a prime number" :"not a prime number",n3,prime(n3)==1 ? "a prime number" :"not a prime number");
			goto menu;
			break ; 
		case 7 :
			(n1==-1) ? puts("ERROR: No numbers entered.") : printf(" -> %d is %s\n -> %d is %s\n -> %d is %s\n",n1, perfect(n1)==1 ? "a perfect number" :"not a perfect number",n2,perfect(n2)==1 ? "a perfect number" :"not a perfect number",n3,perfect(n3)==1 ? "a perfect number" :"not a perfect number");
			goto menu;
			break ; 
		case 8 :
			init();
			goto menu;
			break ; 
		case 9:
			return 0;	
		default :
			puts("ERROR: Incorrect command, try again");
			goto menu;	
	}
	return 0;
}
int displayMenu(void)   
{
	puts("******** Main menu ********");
	puts("1 : Input the numbers");
	puts("2 : Display the numbers");
	puts("3 : Sort the numbers");
	puts("4 : Display the average");
	puts("5 : Display the min and max");
	puts("6 : Display Prime or Not");
	puts("7 : Display Perfect or Not");
	puts("8 : Initialize the numbers");
	puts("9 : Quit");
	puts("**************************");
	printf("Please, enter your choice > ");
	scanf("%d",&choice);
	return choice;
}
void input(void) // get 3 numbers from the user and check for validity 
{
	puts("Enter 3 numbers :");
try_n1 :	
	printf("The first number  => ");
	scanf("%d",&n1);
	if (n1 < 0)
	{
		puts("ERROR: Incorrect number, try again");
		goto try_n1;
	} 
try_n2:	
	printf("The second number => ");
	scanf("%d",&n2);
	if (n2<0)
	{
		puts("ERROR: Incorrect number, try again");
		goto try_n2;
	}
try_n3:
	printf("The third number  => ");
	scanf("%d",&n3);
	if (n2<0)
	{
		puts("ERROR: Incorrect number, try again");
		goto try_n3;
	}
}
int minNumbers(void) // funtion to find  min number
{
	int min=n1;  // initialize  min to n1 to make the comparison easier 
	(n2<min) ? min=n2 : min;   // if n2 is less than min we assign n2 to min
	(n3<min) ? min=n3 : min;	// if n3 is less than min we assign n3 to min
	return min; // retrun the min
}
int maxNumbers(void) // function to find the max number
{
	int max=n1;
	(n2>max) ? max=n2 : max;	 
	(n3>max) ? max=n3 : max;
	return max;
}
double avgNumbers(void) // compute the avg
{
	return (n1+n2+n3)/3.0; // return the avg
}
void sortNumbers(void) // sort n1 n2 n3
{
	int x,y,z;
	x=maxNumbers();
	z=minNumbers();
	y=(n1+n2+n3)-(x+z);
	n1=z;n2=y;n3=x;
}
int prime(int n) // prime function
{
	int x=1,i;  
	if (n>1)  // We know that prime numbers are > than 0 and 1 is not a prime number  
	{
		for (i=2;i<=(n-1);i++) // We loop for different divisors ; we put "i=2" because every number is divisible by 1 no need to cover it 
		{
			if (n%i==0)  // if the condition is ture add 1 to X
				{
					x=0;
					break;    // include "break" is we find divisors 
				}
		}	
	}
	else 
		x=0;  // case of 0 or 1 (not prime numbers)
	return x;
}
int perfect(int n) // perfect function
{
	int i,sum=0;   
	for(i=1; i<n ; i++)     // loop for divisors 
      (n%i==0) ? sum=sum+i : sum;  //conditional operator to compute the sum 
    if (n>1)  
  		(sum==n) ? n=n-(n-1) : n;  // if sum=number it's a perfect number
  	else 
	  	n=0;	
	return n;
}
void init(void) // function to initialize numbers to -1
{
	n1=n2=n3=-1;
}
void displayNumbers(void)
{
	printf("->N1: %d\n->N2: %d\n->N3: %d\n",n1,n2,n3);
}
/*-------------------------------------End----------------------*/
rproffitt 2,580 "Nothing to see here." Moderator

Looking at https://rosettacode.org/wiki/Perfect_numbers#C I wonder if line 154 needs a change.

commented: Great point! I took a different approach in my code; instead of having a max value, I used just the number N to loop for divisors. +0
Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

Looking at https://rosettacode.org/wiki/Perfect_numbers#C I wonder if line 154 needs a change.

How would you change it? Be helpful to people stumbling across this thread from a Google search and wanting the ability to copy/paste. (That's the point of our code snippet library, after all).

rproffitt 2,580 "Nothing to see here." Moderator

Sorry Dani, if folk must be spoon fed they need to outsource the work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.