Download תרגול 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
‫משפטי תנאי – פתרונות‬
‫ אם הקלט לא‬,1-11 ‫) כיתבו תוכנית אשר קולטת שני מספרים שלמים בין‬1
.‫ תודיע הודעת שגיאה‬,‫תקין‬
‫ אם‬.‫ מדפיסה את התוצאה של הראשון בחזקת השני‬8 ‫אם סכומם קטן מ‬
‫ אחרת מדפיסה את‬.‫ מדפיסה את השני בחזקת הראשון‬11 ‫ ל‬8 ‫סכומם הוא‬
.‫המספר הקטן בריבוע ואת השורש של המספר השני‬
#include "stdafx.h"
#include <conio.h>
#include <math.h>
/* n is a positive integer. The function returns its factorial */
int fact(int n) {
int lcv;
/* loop control variable */
int p;
/* set to the product of the first lcv positive
integers */
for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
return p;
}
void main()
{
int n1,n2,smallnum,bignum;
printf("Enter 2 numbers between 1-10:\n");
scanf("%d %d",&n1,&n2);
if( n1 > 10 || n1 < 0 || n2 > 10 || n2 < 0 ) printf("ERROR:
numbers should be between 1-10\n");
if( n1 + n2 < 8 )
{
printf("N1=%d power of N2=%d equals:
%f\n",n1,n2,pow((float)n1,(float)n2));
}
if( n1 + n2 > 8 && n1+n2 < 15){
printf("N2=%d power of N1=%d equals:
%f\n",n2,n1,pow((float)n2,(float)n1));
}
if( n1 + n2 > 15 ){
if( n1 > n2 ){
bignum = n1 ;
smallnum = n2;
}else{
bignum = n2 ;
smallnum = n1;
}
printf("The second power of %d is:
%d\n",smallnum,smallnum*smallnum);
printf("The root of %d is: %f\n",bignum,
sqrt((float)bignum));
}
_getch();
}
‫ התוכנית תיקלוט מספר‬.‫) כיתבו תוכנית שקולטת שני מספרים שלמים‬2
‫שמייצג אופרטור‬
‫ – חיבור‬1
‫ – חיסור‬2
‫ – כפל‬3
‫ – חילוק‬4
‫ יש לבדוק‬.‫עליכם לבדוק מה אתם התבקשתם לבצע ולהדפיס את התוצאה‬
.)‫חוקיות הקלט (ולשים לב לתוצאת החילוק‬
#include "stdafx.h"
#include <conio.h>
void main()
{
int a,b,op ;
// Getting two whole numbers
printf("Enter 2 whole numbers: ");
scanf("%d %d",&a,&b);
printf("Enter an operator -\n");
printf("1 - Add\n2 - Substract\n3 scanf("%d",&op);
// check if op is valid
if( op >= 1 && op <= 4 )
{
// perform operation based on
if( op == 1 ) printf("A + B =
if( op == 2 ) printf("A - B =
if( op == 3 ) printf("A * B =
if( op == 4 ) printf("A / B =
}
else
{
printf("Error: operation must
}
_getch();
}
Multiply\n4 - Divide\n");
the op input
%d\n",a+b);
%d\n",a-b);
%d\n",a*b);
%f\n",(float)a/b);
be between 1 - 4 \n");
‫) קילטו שני מספרים שלמים‬3
)IF ‫ אם לא (ללא שימוש ב‬1 ‫ אם המספרים שווים ו‬1 ‫יש להדפיס‬
‫ אם המספר הראשון זוגי‬1 ‫מדפיסה‬
#include "stdafx.h"
#include <conio.h>
void main()
{
int a,b ;
// Getting two whole numbers
printf("Enter 2 whole numbers: ");
scanf("%d %d",&a,&b);
printf("The two number are equal/not equal %d\n",a == b );
printf("The first number is even: %d\n", a % 2 == 0);
_getch();
}
‫ יש להדפיס את הגדול מבין השניים תוך שימוש‬.‫) קילטו שני מספרים שלמים‬4
? :‫באופרטור תנאי מקוצר‬
#include "stdafx.h"
#include <conio.h>
void main()
{
int a,b ;
// Getting two whole numbers
printf("Enter 2 whole numbers: ");
scanf("%d %d",&a,&b);
printf("The larger number is : %d\n",a>b?a:b);
_getch();
}