שאלה 1
כתוב תכנית הקולטת 25 מספרים דו ספרתיים ומציגה ליד כל מספר אחת מההודעות הבאות:
שתי הספרות זוגיות
שתי הספרות אי זוגיות
ספרה אחת זוגית והאחת אי זוגית
פתרון בשפת C#
for ( int i = 0 ; i < 25 ; i++)
{
Console.WriteLine(“Please enter two digit number”);
int num = int.Parse(Console.ReadLine());
int d1 = num %10; // right digit
int d2 = num / 10; // left digit
if( d1%2==0 && d2%2==0)
Console.WriteLine(“Both even”);
else if( d1%2==1 && d2%2==1)
Console.WriteLine(“Both odd”);
else
Console.WriteLine(“Both odd and even”);
}
פתרון בשפת Java
for ( int i = 0 ; i < 25 ; i++)
{
System.out.println(“Please enter two digit number”);
int num = input.nextInt();
int d1 = num %10; // right digit
int d2 = num / 10; // left digit
if( d1%2==0 && d2%2==0)
System.out.println(“Both even”);
else if( d1%2==1 && d2%2==1)
System.out.println(“Both odd”);
else
System.out.println(“Both odd and even”);
}
שאלה 2
הדפס את המספרים הזוגיים בין אפס למאה:
for( int i=0 ; i<=100 ; i++ )
{
if(i%2==0)
Console.WriteLine(i);
}
סכום את המספרים הזוגיים בין אפס למאה:
int sum=0;
for( int i=0 ; i<=100 ; i++ )
{
sum+=i ; //sum=sun+i;
}
שאלה 3
כתוב תכנית המקבלת כקלט 50 מספרים תלת-ספרתיים ומציגה כפלט את המספרים שכל ספרותיהם שונות זו מזו.
פתרון מלא בשפת C#
for ( int i = 0 ; i<50 ; i++)
{
Console.WriteLine(“Please enter 3 digitis number:”);
int x = int.Parse(Console.ReadLine());
int units = x%10 ;
int tens = x/10%10 ;
int hundreds = x / 100;
if( units != tens && units !=hundreds && tens != hunderds)
Console.WriteLine(x);
}
שאלה 4
א. כתוב תכנית המקבלת כקלט num מספרים דו ספרתיים חיוביים. לכל מספר יוצג כפלט המספר וסכום ספרותיו.
ב. שנה את התכנית שכתבת בסעיף א כך שיוצג פלט רק עבור המספרים הדו ספרתיים שספרותיהם שונות (ספרת העשרות שונה מספרת האחדות)
פתרון סעיף א'
Console.WriteLine(“Please enter int number:”);
int num = int.Parse(Console.ReadLine());
for ( int i=0 ; i < num ; i++)
{
Console.WriteLine(“Please enter two digits number:”);
int x= int.Parse(Console.ReadLine());
int sum = x%10 + x/10;
Console.WriteLine(“The number is “+x+” and the sum of his digits is “+sum);
}
פתרון סעיף ב'
Console.WriteLine(“Please enter int number:”);
int num = int.Parse(Console.ReadLine());
for ( int i=0 ; i < num ; i++)
{
Console.WriteLine(“Please enter two digits number:”);
int x= int.Parse(Console.ReadLine());
if( x%10 != x/10 )
{
int sum = x%10 + x/10;
Console.WriteLine(“The number is “+x+” and the sum of his digits is “+sum);
}
}
שאלה 5
כתוב תכנית הקולטת שני מספרים, לא ידוע מי הגדול ומי הקטן. על התכנית להדפיס את המספרים ביניהם (כולל הקצוות) ללא שימוש במשתני עזר.
Console.WriteLine(“Please enter two int number:”);
int x1= int.Parse(Console.ReadLine());
int x2= int.Parse(Console.ReadLine());
if (x1<x2)
{
for( int i= x1; i<=x2 ; i++)
Console.WriteLine(i);
}
else
{
for( int i= x2; i<=x1 ; i++)
Console.WriteLine(i);
}
שאלה 6
כתוב תכנית הקולטת 35 זוגות מספרים ומדפיסה בעבור כל זוג: את העקוב של הקטן, והקודם של הגדול.
פתרון בשפת C#
for (int i=0; i<35;i++)
{
Console.WriteLine("Please enter two int numbers:");
int x1 = int.Parse(Console.ReadLine());
int x2 = int.Parse(Console.ReadLine());
if( x1 > x2)
Console.WriteLine((x2+1)+”, “+(x1-1));
else
Console.WriteLine((x1+1)+”, “+(x2-1));
}
שאלה 7
כתוב תכנית הקולטת 58 ציונים של תלמיד בשלוש מקצועות. תלמיד שעובר שלב הוא תלמיד שממוצע ציוניו הוא גבוה מ-92 ואין לו ציון נמוך מ-90.
על התכנית להציג כפלט את מי שעבר לשלב ב', וממוצע ציוניו: יש להדפיס את המספר הסידורי של אותו תלמיד. מ-1 עד 58.
פתרון מלא בשפת C#
for (int i=1; i<=58;i++)
{
Console.WriteLine("Please enter three int numbers for grades:");
int x1 = int.Parse(Console.ReadLine());
int x2 = int.Parse(Console.ReadLine());
int x3 = int.Parse(Console.ReadLine());
double avg = (x1+x2+x3)/3.0;
if(avg >92 && x1>90 && x2>90 && x3>900)
{
Console.WriteLine("Serial number: "+(i)+” and the avg is ”+avg);
}
}
שאלה 8
כתוב תכנית הקולטת כמה שערים הבקיע כל שחקן שלושת המשחקים האחרונים. ידוע כי קיימים 20 שחקנים.
על התכנית להדפיס מספר סידורי וממוצע שערים שהבקיע.
הודעה לא מבקיע לשחקנים שלא הבקיעו.
הודעה שחקן מצטיין למי שהבקיע לפחות 5 שערים.
for (int i=1; i<=20;i++)
{
Console.WriteLine("Please enter three numbers of goals: ");
int x1 = int.Parse(Console.ReadLine());
int x2 = int.Parse(Console.ReadLine());
int x3 = int.Parse(Console.ReadLine());
double avg = (x1+x2+x3)/3.0;
Console.WriteLine("Serial number: "+(i)+” and the avg is ”+avg);
if( (x1+x2+x3) == 0 )
{
Console.WriteLine(“לא הבקיע”);
}
if( (x1+x2+x3) >=5)
{
Console.WriteLine(“שחקן מצטיין”);
}
}
פתרון בשפת Java:
System.out.println(“Please enter number of people:”);
int n = input.nextInt();
int counter=0;
for( int i=0; i<n ; i++)
{
System.out.println(“Please enter number mistakes in traffic signs:”);
int signs = input.nextInt();
System.out.println(“Please enter number of rest mistakes:”);
int rest = input.nextInt();
if( signs == 0 && rest<=3)
counter++;
}
System.out.println(“People which succeeded “ +counter);
System.out.println(“Ratio :“ +(counter/n)*100);