Download תרגול 4 - פתרון קובץ

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
‫פתרונות תרגול מס ‪ – 5‬לולאות‬
‫‪ .1‬כיתבו תוכנית אשר מקבלת שני מספרים (שלמים) ומדפיסה את טבלת הכפל‬
‫מ ‪ 1‬עד ‪ A*B‬בטבלה‪ .‬בצעו את ההדפסה של הטבלה תוך שמירה על רוחב של‬
‫‪ 4‬תווים עבור כל טור‪.‬‬
‫"‪#include "stdafx.h‬‬
‫>‪#include <conio.h‬‬
‫)(‪void main‬‬
‫{‬
‫; ‪int x,y‬‬
‫;)" ‪printf("Enter two numbers X,Y:‬‬
‫;)‪scanf("%d %d",&x,&y‬‬
‫) ‪for(int i=1; i <= x ; i++‬‬
‫{‬
‫) ‪for(int j=1; j <= y ; j++‬‬
‫;)‪printf("%4d ",i*j‬‬
‫;)"‪printf("\n‬‬
‫}‬
‫;)(‪_getch‬‬
‫}‬
1+1/2+1/4+1/6 ...1/x ‫ כיתבו תוכנית אשר מדפיסה את תוצאת החיבור של‬.2
‫ בצעו את התוכנית בתוך לולאת‬.‫ הוא מספר שלם שהתקבל כקלט‬X ‫כאשר‬
0 = X ‫ כאשר הקלט‬BREAK ‫ אינסופית אשר תישבר באמצעות‬WHILE
#include "stdafx.h"
#include <conio.h>
void main()
{
int x ;
double sum;
// loop until input is 0
while(1)
{
printf("Enter int X: ");
scanf("%d",&x);
// stop running if input is 0
if( !x ) break ;
// calculate the sum
sum = 1 ;
for(int i=2 ; i <= x ; i++ )
{
// pay attention to the float, int/int = int
sum += 1.0/i ;
}
printf("Sum of calculation is: %lf\n",sum);
}
}
‫ עליכם להדפיס את‬.‫ ספרות‬X ‫ כתבו תוכנית שקולטת את מספר שלם בן‬.3
‫ התוכנית תמשיך לפעול עד‬.)7654321 >- 1234567 :‫המספר הפוך (כלומר‬
.‫ לביצוע ההפיכה של המספר‬do-while ‫ השתמשו בלולאת‬.0 ‫להכנסה של‬
#include "stdafx.h"
#include <conio.h>
void main()
{
int num, temp ;
while(1)
{
printf("Enter a number: ");
scanf("%d",&num);
// stop running if number equals 0
if( !num ) break ;
temp = num ;
// flipping the number
do
{
// printing the right digit
printf("%d",temp%10);
// removing the right digit from the number
temp /=10 ;
}while( temp > 0 );
printf("\n");
}
}
: ‫ באופן הבא‬switch ‫) כתבו תוכנית המשתמשת במשפט‬1
‫ – תקלוט שני מספרים ותחבר בינהם‬1 ‫בקבלה של‬
5 ‫ – תקלוט מספר ותחזיר את המספר בחזקת‬2 ‫בקבלה של‬
)sin(x)( ‫ – תקלוט מספר ותחזיר את הסינוס שלו‬3 ‫בקבלה של‬
‫ – תקלוט שלושה מספרים ותחזיר‬4 ‫בקבלה של‬
𝑐
(𝑎 + 𝑏)2
∑
𝑛
𝑛=2
#include "stdafx.h"
#include <conio.h>
#include <math.h>
void main()
{
int sel ,a,b,n;
float x,sum ;
bool flag = true ;
while(flag)
{
printf("Enter your selection (0 to exit): ");
scanf("%d",&sel);
switch(sel)
{
case 1:{
printf("Please enter two numbers. result will be
their sum:");
scanf("%d%d",&a,&b);
printf("result: %d\n",a+b);
break;
}
case 2:{
printf("Please enter a number. result is 5 power of
X: ");
scanf("%d",&a);
printf("result: %.0f\n",pow(float(a),5));
break ;
}
case 3:{
printf("Please enter a number. result is sin(x):
");
scanf("%f",&x);
printf("result: %f\n",sin(float(x)));
break ;
}
case 4:{
printf("Please enter 3 numbers (a,b,n): ");
scanf("%d %d %d",&a,&b,&n);
sum = 0 ;
for(int i=2 ; i<=n ; i++ )
{
sum += pow(float(a+b),2)/i ;
}
printf("result: %f\n",sum);
break;
}
case 0:
flag = false ;
break;
default: printf("Error: selection between 0-4\n");
}
}
}
‫ התוכנית תדפיס עבור כל‬.)‫ כיתבו תוכנית אשר מקבלת שני מספרים (שלמים‬.4
‫ כדי ליעל‬.Z ‫ שהן המחלקות של‬Y ‫ את הכפולות של‬X ‫ עד‬1 ‫ מ‬Z ‫מספר זוגי‬
.Y ‫ בידקו אם המספר מתחלק ל‬.‫את התוכנית‬
2=Y ‫ ו‬10=X ‫עבור‬
2
4
6
8
10
2
2,4
2,6
2,4,8
2,10
3=Y ‫ ו‬20=X ‫עבור‬
2
4
6
8
10
12
14
16
18
20
3,6
3,6,12
3,6,9,18
CONTINUE ‫ ועם‬BREAK ‫ עם‬:‫פתור בשני אופנים‬
#include "stdafx.h"
#include <conio.h>
//void main()
//{
//
int x, y ;
//
bool Flag ;
//
printf("Enter two numbers X,Y:");
//
scanf("%d %d",&x,&y);
//
//
for(int i=2 ; i<= x ; i+=2 )
//
{
//
printf("%d\t",i);
//
Flag = true ;
//
for(int j=y ; j<= i ; j+=y )
//
{
//
if(((float)i/j - (int)((float)i/j)) < 0.0000001)
//
{
//
printf("%d ",j);
//
Flag = false ;
//
}
//
if( Flag ) break ;
//
}
//
printf("\n");
//
}
//
_getch();
//}
void main()
{
int x, y ;
bool Flag ;
printf("Enter two numbers X,Y:");
scanf("%d %d",&x,&y);
// going over all even numbers till x
for(int i=2 ; i<= x ; i+=2 )
{
// printing the number
printf("\n%d\t",i);
// checking if i does not divide by y no use checking
more dividers
if( i%y ) continue ;
printf("%d",y);
for(int j=y*2 ; j<= i ; j+=y )
{
if( i%j == 0)
{
printf(",%d",j);
}
}
}
_getch();
}