Return to Snippet

Revision: 53290
at November 18, 2011 03:44 by AlexanderRavikovich


Initial Code
#include <stdio.h>

void main()
{
	// Progamm 1
	// initializing variables
	int unum1=0, unum2=0, unum3=0, index=0, large=0, medium=0, small=0;

	/* Ask to enter first number, with validating */
	do
	{
		/* If we ask for 1st time, print request */
		if (!index)
		{
			printf("Enter first number between 20 and 48:\n");
		}
		/* Put entered number in variable unum1 */
		scanf("%d", &unum1);
		/* If unum1 is not in condition, say to user enter another bumber */
		if (!(unum1 > 20 && unum1 < 48))
		{
			printf("Your number is wrong. Try again:\n");
		}
		/* Set index to 1, to avoid repeating line "Enter first number between 20 and 48" */
		index=1;
	}
	while (!(unum1 > 20 && unum1 < 48)); /* <----- till entered number is not in condition, run code in do {} */

	/* Set index to 0, to use this variable again for next valdating loop */
	index = 0;

	/* Ask to enter second number, with validating.
	Code is almost as previous, only now for second number */
	do
	{
		if (!index)
		{
			printf("Enter second number between 20 and 48:\n");
		}
		
		scanf("%d", &unum2);
		
		if (!(unum2 > 20 && unum2 < 48))
		{
			printf("Your number is wrong. Try again:\n");
		}
		/* If entered number is equal to first number, say to user enter another number */
		else if (unum2 == unum1)
		{
			printf("Enter different number from previous:\n");
		}
		
		index=1;
	}
	while (!(unum2 > 20 && unum2 < 48) || unum2==unum1); /* I added check for preventing entering the same numbers */

	/* Set index to 0,to use this variable again for next valdating loop */
	index=0;
	/* Ask to enter third number, with validating.
	Code is almost as previous two,
	only now for third number */
	do
	{
		if (!index)
		{
			printf("Enter third number between 20 and 48:\n");
		}
		
		scanf("%d", &unum3);
		
		if (!(unum3 > 20 && unum3 < 48))
		{
			printf("Your number is wrong. Try again:\n");
		}
		/* If entered number is equal to first or second number, say to user enter another number */
		else if (unum3 == unum1 || unum3 == unum2)
		{
			printf("Enter different number from two previous:\n");
		}

		index=1;
	}
	while (!(unum3 > 20 && unum3 < 48) || (unum3 == unum1 || unum3 == unum2)); /* Check for preventing entering the same numbers as unum1 and unum2 */

	/* When we know that all three number is in our condition,
	write code for printing large, medium and small number.
	*/

	/* If 1st number larger them 2nd and 3rd, the 1st number is LARGE */
	if (unum1 > unum2 && unum1 > unum3)
	{
		/* Set variable "large" = unum1; */
		large = unum1;
		/* If 3rd number smaller than 2nd number, 3rd number is SMALL, and 2st number is MEDIUM */
		if (unum3 < unum2)
		{
			medium = unum2;
			small = unum3;
		}
		/* If 3rd number is large than 2nd number, 3rd number is MEDIUM, and 2st number is SMALL */
		else
		{
			medium = unum3;
			small = unum2;
		}
	}

	/* If 2nd number larger them 1st and 3rd, the 2st number is LARGE */
	if (unum2 > unum1 && unum2 > unum3)
	{
		large = unum2;
		if (unum3 < unum1)
		{
			medium = unum1;
			small = unum3;
		}
		else
		{
			medium = unum3;
			small = unum1;
		}
	}

	/* If 3rd number larger them 1st and 2nd, the 3rd number is LARGE */
	if (unum3 > unum1 && unum3 > unum2)
	{
		large = unum3;
		if (unum1 < unum2)
		{
			medium = unum2;
			small = unum1;
		}
		else
		{
			medium = unum1;
			small = unum2;
		}
	}

	/* Printing result */
	printf("%d - is Large\n%d - is Medium\n%d - is Small\n", large, medium, small);
}

Initial URL


Initial Description
In ths program i also check for wrong numbers, and prevent entering equal numbers.

Initial Title
Programm that take 3 numbers from user between 20 and 48, and print weight of each number

Initial Tags


Initial Language
C