מבוא למדעי המחשב תרגיל 3

benny++C, כללי, מבוא למדעי המחשבLeave a Comment

שאלה 1

/*************************
targil bait 3 shela 1 
lecturer: Meir Kommer
discription:
the INPUT is a tow digit number (0<number<100), the program mast 
pirnt Different OUTPUT every Different case,For example, if a tow digit number
contain tow odd numbers (99), the OUTPUT will Be different form case that the number
containtow even numbers (88)
**************************/


#include <iostream>
using namespace std;

int main ()
{
	cout << "enter a number:"<<endl;
	int num;
	cin>>num;
	if (num <100 && num!=0 && num>0)   // The purpose of this  line is to go Straight to command else end print "ERROR"  in case the conditions are not true
	{
		
		if ((num%10)%2!=0 &&(num/10)%2!=0)  
		{
			cout<<"odd digits only"<<endl;
			cout <<(num%10)+(num/10)<<endl;
		}




		if ((num%10)%2==0&& (num/10)%2==0)
		{
		
			cout <<"even digits only"<<endl;
			cout<<(num%10)*(num/10)<<endl;

		}	 	  	 	   	   		   	 	    	 	
		if ((num/10)%2==0&&(num%10)%2!=0 ||(num%10)%2==0 && (num/10)%2!=0)
		{
			cout <<"mixed number"<<endl;
		}
	}
	else
	{
		cout <<"ERROR"<<endl;
	}
	

	

	/****************************************OUTPUT**************************************************
	enter a number:   *         enter a number:			*	enter a number:		*	enter a number:
	19                *         28						*	16					*	100
	odd digits only   *         even digits only		*		mixed number	*		ERROR
	10						   16						*						*
					  *									*						*
					  *									*						*
					  *									*						*
					  *									*						*
	*************************************************************************************************/


	return 0;
}	 	  	 	   	   		   	 	    	 	

שאלה 2

/*************************
targil bait 3 shela 2 
Lecturer: dr Meir Kommer
discription: Finding out if a certain number is within a certain range, or smaller or larger than him
**************************/


#include <iostream>
using namespace std;

int main ()
{
	
	int first, last, number;

	cout <<"enter 3 numbers: "<<endl;
	cin >> first>>last>>number;        //INPUT

	if (number>=first&&number<=last)
	{
		cout<<"between"<<endl;        
	}
	if (number<first)
	{
		cout<<"smaller"<<endl;
	}
	if (number>last)
	{
		cout<<"bigger"<<endl;
	}












	/*****************************output*************************
	enter 3 numbers:  *  enter 3 numbers:	*	 enter 3 numbers:
	3  9 7			  *  3 9 1				*	 3 9 11
	between			  *  smaller			*	 biger
					  *						*
					  *						*
	************************************************************/

	

	return 0;
}	 	  	 	   	   		   	 	    	 	

שאלה 3

/*************************
targil bait 3 shela 3
lecture: Dr meir komer
discription:calculator
**************************/


#include <iostream>
using namespace std;

int main()
{
	int num1, num2;
	char operaretor;
	cout << "enter 2 numbers:" << endl;
	cin >> num1 >> num2;
	cout << "enter an operator:" << endl;
	cin >> operaretor;
	switch (operaretor)
	{
	case '-': cout << num1 << "-" << num2 << "=" << num1 - num2 << endl;
		break;
	case '+': cout << num1 << "+" << num2 << "=" << num1 + num2 << endl;
		break;
	case '*': cout << num1 << "*" << num2 << "=" << num1 * num2 << endl;
		break;
	case '/': cout << num1 << "/" << num2 << "=" <<(float) num1 / num2 << endl;
		break;
	default: cout << "ERROR" << endl;
		break;
	}




	/*******************************OUTPUT************************************************
	enter 2 numbers: 	  *	enter 2 numbers:		enter 2 numbers:	enter 2 numbers: 
	10  2				  *	10  2					10  2				10  2
	enter an operator: 	  *	enter an operator:		enter an operator: 	enter an operator:				 
	–					  *	+						/
	10 – 2 = 8			  *	10 + 2 = 12				10 / 2 = 5			10 * 2 = 20
						  *						
	**************************************************************************************/

	

	return 0;
}	 	  	 	   	   		   	 	    	 	

שאלה 4

/*************************
targil bait 3 shela 4 
discription: Distinguishing between the types of triangles
**************************/


#include <iostream>
using namespace std;

int main ()
{
	cout <<"enter 3 numbers:"<<endl;
		
	int tsela1, tsela2, tsela3;	
	cin>> tsela1 >> tsela2 >> tsela3;
	if (tsela1> 0 && tsela1 < 10*10^100 && tsela2 > 0 && tsela1 < 10*10^100 && tsela3 > 0 && tsela1 < 10*10^100)  //  Making sure the user enters the number
	{
		if (tsela1>tsela2+tsela3||tsela2>tsela1+tsela3||tsela3>tsela2+tsela1)      // The sum of the two sides of the triangle must be greater than the third (it is nut in the definition of triangle)
			{
				cout<<"cannot form a triangle"<< endl;
			}
			else
			{
				
				if (tsela1==tsela2 && tsela2==tsela3)            // if the all side of triangle are Equal
				{
					cout<<"equilateral triangle"<<endl;
				}
				if (tsela1==tsela2&&tsela1!=tsela3||tsela1==tsela3&&tsela1!=tsela2||tsela3==tsela2&&tsela3!=tsela1)  // if to side of triangle are Equal but the third is diffrent
				{
					cout<<"isosceles triangle"<<endl;
				}
				if (tsela1!=tsela2 && tsela1!=tsela3&&tsela2!=tsela3)     // if the all side of triangle are diffrent
				{
					cout<<"scalene triangle"<<endl;
				}	 	  	 	   	   		   	 	    	 	
			}
	}
	else
	{
		cout << "ERROR"<< endl;
	}
			
		
	
	
	/****************************************OUTPUT*********************************************
	
	enter 3 numbers:	 	enter 3 numbers:			enter 3 numbers:		enter 3 numbers: 
	5 5 5					5 7 5						3 4 5					1 7 5
	equilateral triangle	isosceles triangle			scalene triangle		cannot form a triangle

	
	***************************************************************************************************/

	
	return 0;
}

שאלה 5

/*************************
targil bait 3 shela 5 
discription: Saying the "day in week" by the date
**************************/
#include <iostream>
using namespace std;

int main()
{


	cout << "enter a date:" << endl;
	int year, B, C, D, E, F, month, day;
	char revach;
	cin >> day >> revach >> month >> revach >> year;
	if (day>31 || month>12 || year>99)
	{
		cout<<"ERROR"<<endl;
	}
	else
	{
		switch (month)
	{case 1: D=1;
	break;
		case 2: D=4;
	break;
		case 3: D=4;
	break;
		case 4: D=0;
	break;
		case 5: D=2;
	break;
		case 6: D=5;
	break;
		case 7: D=0;
	break;
		case 8: D=3;
	break;
		case 9: D=6;
	break;
		case 10: D=1;
	break;
		case 11: D=4;
	break;
		case 12: D=6;
	break;

	}	 	  	 	   	   		   	 	    	 	

	B = year / 4;
	C = B + year;
	E = C + D;
	F = E + day;

	int dayInWeek = F%7;
	switch (dayInWeek)
	{
	case 1: cout <<"Sunday"<<endl;
		break;
	case 2: cout <<"Monday"<<endl;
		break;
	case 3: cout <<"Tuesday"<<endl;
		break;
	case 4: cout <<"Wednesday"<<endl;
		break;
	case 5: cout <<"Thursday"<<endl;
		break;
	case 6: cout <<"Friday"<<endl;
		break;
	case 0: cout <<"Saturday"<<endl;
		break;
	
		
	}

	}
	
	

	return 0;
}
/************OUTPUT*************
enter a date:
17/8/44
Thursday

********************************/










	 	  	 	   	   		   	 	    	 	

שאלה 6

/*

LECTURER: MEIR KOMAR
DESCRIPTION: diplays numbers by their order
*****************************************************/

#include<iostream>
using namespace std;
int main()
{
	int num1, num2, num3, smallest, middle, biggest;

	cout << "enter 3 numbers: " << endl;
	cin >> num1 >> num2 >> num3;


	if (num1 > num2)
	{
		biggest = num1;
	}
	else
	{
		biggest = num2;
	}
	if (biggest < num3)
	{
		biggest = num3;
	}
	if (num1 < num2)
	{
		smallest = num1;
	}
	else
	{
		smallest = num2;
	}
	if (smallest > num3)
	{
		smallest = num3;
	}

	middle = (num1 + num2 + num3) - (smallest + biggest);

	cout << smallest << " " << middle << " " << biggest << endl;


	//system("pause");
	return 0;
}

/*************************************************************
outcome:

1)
enter 3 numbers:
946
541
1
1 541 946
Press any key to continue . . .
*/

שאלה 6

/**************************************
LECTURER: MEIR KOMAR
DESCRIPTION: enter three random numbers, the program display the numbers in order 1>2>3. 
*****************************************************/
#include <iostream>
using namespace std;
int main()

{
	int num1, num2, num3, smallest, middle, biggest;
	cout << "enter 3 numbers: " << endl;
	cin >> num1 >> num2 >> num3;

	if (num1 >= num2 && num2 >= num3)
	{
		biggest = num1; middle = num2; smallest = num3;
	}
	if (num1 >= num3 && num3 >= num2)
	{
		biggest = num1; middle = num3; smallest = num2;
	}
	if (num2 >= num3 && num3 >= num1)
	{
		biggest = num2; middle = num3; smallest = num1;
	}
	if (num2 >= num1 && num1 >= num3)
	{
		biggest = num2; middle = num1; smallest = num3;
	}
	if (num3 >= num2 && num2 >= num1)
	{
		biggest = num3; middle = num2; smallest = num1;
	}
	if (num3 >= num1 && num1 >= num2)
	{	 	  	 	   	   		   	 	    	 	
		biggest = num3; middle = num1; smallest = num2;
	}

	

	cout << smallest << " " << middle << " " << biggest << endl;

	return 0;
}
/*************OUTPUT*****************
enter 3 numbers:
54 68 47
47 54 68
Press any key to continue . . .

*****************************************/

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *