الدرس 2: المكتبات وَ أول مشروع |
الدرس 3: العوامل و الإنشاءات |
الدرس 4: قراءة وكتابة المعطيات |
الدرس 5: أدوات الشرط |
الدرس 6: الحلقات (أدوات التكرار) |
»» الدرس 2: المكتبات وَ أول مشروع »» |
التمرين 2.1 |
التمرين 2.2 |
التمرين 2.3 |
التمرين 2.4 |
التمرين 2.5 |
التمرين 2.1
في Pascal |
في C |
توجد الدوال الرجعية والدوال اللارجعية | توجد فقط الدوال (أما الدوال اللارجعية) فهي تمثل بالدوال المسبوقة بكلمة void |
نستعمل كلمة program لتعريف الشفرة الأساسية |
الدالة الأساسية main تنفذ تلقائيا وهي تحتوي على الشفرة الأساسية |
مجموعة الأوامر تحصر بـ : begin ... end |
مجموعة الأوامر تحصر بـ :{ ... } |
التعليقات يرمز لها بـ : (* ... *) أو { ... } |
التعليقات يرمز لها بـ : /* ... */ |
الفاصلة المنقطة ; تقوم بفصل الأوامر بعضها عن بعض |
الفاصلة المنقطة ; تقوم بفصل الأوامر بعضها عن بعض |
التمرن 2.2
fonction-1 | غير مقبول (يحتوي على الرمز '-' ) |
_MOYENNE_du_MOIS_ | مقبول |
3e_jour | غير مقبول (توجد 3 في البداية) |
limite_inf. | غير مقبول (يحتوي على الرمز '.' ) |
lim_supérieure | غير مقبول (يحتوي على الرمز 'é' ) |
__A_ | مقبول |
_ | مقبول |
a | مقبول |
3 | غير مقبول (يوجد رقم فقط) |
التمرين 2.3
#include <stdio.h>
/* C أول مشروع في */
main()
{
printf("salamo ");
printf("3alaikom");
printf("\n");
return 0;
}
التمرين 2.4
الرموز | شرح بالعربية | شرح بالإنجليزية | شرح بالفرنسية |
\n | الرجوع إلى السطر | new line | passage à la ligne |
\t | الجدولة | tabulator | tabulation |
\b | المشير إلى الوراء | back | curseur arrière |
\r | الرجوع إلى بداية السطر | return | retour au début de la ligne |
\" | علامتين | quotation marks | guillemets |
\\ | المائل | back-slash | trait oblique |
\0 | نهاية متسلسلة | NUL | fin de chaîne |
\a | ارسال صوت |
attention (bell) |
signal acoustique |
التمرين 2.5
#include <stdio.h>
main()
{
int NOMBRE, SOMME, COMPTEUR;
SOMME = 0;
COMPTEUR = 0;
while (COMPTEUR < 4)
{
printf("Entrer nombre entier :");
scanf("%i", &NOMBRE);
SOMME += NOMBRE;
COMPTEUR++;
}
printf("La somme = %i \n", SOMME);
return 0;
}
الدرس 3: العوامل والإنشاءات »» »» |
التمرين 3.1 |
التمرين 3.2 |
التمرين 3.3 |
التمرين 3.4 |
التمرين 3.5 |
التمرين 3.6 |
التمرين 3.7 |
التمرين 3.8 |
التمرين 3.1
No | signed char | short | int | long | unsigned char | short | int | long | float | double | long double |
1 | !X! | X | X | X | X | X | X | ||||
2 | !X! | !X! | X | X | X | X | |||||
3 | X | X | X | !X! | X | X | X | X | X | X | |
4 | !X! | X | X | ||||||||
5 | !X! | !X! | X | X | X | X | |||||
6 | !X! | ||||||||||
7 | X | !X! | !X! | X | X | X | X | ||||
8 | !X! | X | X | X | |||||||
9 | !X! | X | |||||||||
10 | !X! | X | X | X | |||||||
11 | !X! | X | |||||||||
12 | !X! | X | |||||||||
13 | !X! | X |
X تعني اختيار ممكن
!X! تعني أحسن اختيار
التمرين 3.2
(1) |
صحيح COMPTEUR |
{0 ,..., 300} |
int COMPTEUR; |
||
(2) |
صحيح X,Y |
{-120 ,..., 100} |
char X,Y; |
||
(3) |
صحيح MESURE |
{-10 ,..., 104} |
int MESURE; |
||
(4) |
حقيقي SURFACE1 |
{0.5 ,..., 150075} |
float SURFACE1; |
6 مواقع مميزة |
|
(5) |
حقيقي SURFACE2 |
{-12 ,..., 1500750.5} |
double SURFACE2; |
8 مواقع مميزة |
|
(6) |
صحيح N1 |
{0 ,..., 210} = {0 ,..., 1024} |
int N1; |
||
(7) |
صحيح N2 |
{-47 ,..., 47} = {-16384 ,..., 16384} |
int N2; |
||
(8) |
صحيح N3 |
{0 ,..., 326} = {0 ,..., 1 073 741 824} |
long N3; |
||
(9) |
صحيح N4 |
{-1280 ,..., 1285} = {-1 ,..., 3.4*1010} |
double N4; |
11 مواقع مميزة |
|
(10) |
منطقي TROUVE |
{صح, خطأ} |
int TROUVE; |
التمرين 3.3
النظام العشري | النظام الثماني | النظام الستعشري | النظام الثنائي |
1016 | 01770 | 0X3F8 | 1111111000 |
8100 | 017644 | 0X1FA4 | 1111110100100 |
43690 | 0125252 | 0XAAAA | 1010101010101010 |
585 | 01111 | 0X249 | 1001001001 |
51966 | 0145376 | 0XCAFE | 1100101011111110 |
10000 | 023420 | 0X2710 | 10011100010000 |
156 | 0234 | 0X9C | 10011100 |
التمرين 3.4
المعطيات |
النوع إذا كان صحيحا | القيمة أو نوع الخطأ |
12332 | int | 12332 |
23.4 | double | 23.4 |
345lu | - | تعريف صحيح: 345ul |
34.5L | long double | 34.5 |
-1.0 | double | -1.0 |
0xeba | int | 10+11*16+14*256 = 3770 |
0123l | long | 3+2*8+1*64 = 83 |
'\n' | char | ASCII: 10 |
1.23ul | - | فقط بالنسبة للأعداد الصحيحة |
-1.0e-1 | double | -0.1 |
0FE0 | - | تمثيل بالنظام الثماني |
40000 | long | 40000 |
40000u | unsigned int | 40000 |
70000u | unsigned long | 70000 |
1e1f | float | 10 |
'0' | char | ASCII: 48 |
o | - | حرف |
'\0' | char | قيمة تساوي في الأصل 0 |
0 | int | الصفر |
'O' | char | ASCII: 79 |
67e0 | double | 67 |
\r | - | لا توجد العلامتين |
01001 | int | 1+0*8+0*64+1*512 = 513 |
0.0l | long double | 0 |
0XEUL | unsigned long | 14 |
التمرين 3.5
لنعتبر أن A=20 B=5 C=-10 D=2 X=12 Y=15
(1) |
(5*X)+2*((3*B)+4) | -> 98 | / |
(2) | (5*(X+2)*3)*(B+4) | -> 1890 | / |
(3) | A == (B=5) | -> 0 | B=5 |
(4) | A += (X+5) | -> 37 | A=37 |
(5) | A != (C *= (-D)) | -> 0 | C=20 |
(6) | A *= C+(X-D) | -> 0 | A=0 |
(7) | A %= D++ | -> 0 | D=3 A=0 |
(8) | A %= ++D | -> 2 | D=3 A=2 |
(9) | (X++)*(A+C) | -> 120 | X=13 |
(10) | A = X*(B<C)+Y*!(B<C) | -> 0+15 = 15 | A=15 |
(11) | !(X-D+C)||D | -> !0||1 = 1 | / |
(12) | A&&B||!0&&C&&!D | -> 1||1&&1&&0 = 1 | / |
(13) | ((A&&B)||(!0&&C))&&!D | -> (1||1)&&0 = 0 | / |
(14) | ((A&&B)||!0)&&(C&&(!D)) | -> (1||1)&&(1&&0) = 0 | / |
التمرين 3.6
(1) |
(5*X) + 2*((3*B)+4) | <=> 5*X + 2*(3*B+4) |
(2) | (5*(X+2)*3)*(B+4) | <=> 5*(X+2)*3*(B+4) |
(3) | A == (B=5) | / |
(4) | A += (X+5) | <=> A += X+5 |
(5) | A != (C *= (-D)) | <=> A != (C *= -D) |
(6) | A *= C + (X-D) | <=> A *= C + X-D |
(7) | A %= D++ | / |
(8) | A %= ++D | / |
(9) | (X++) * (A+C) | <=> X++ * (A+C) |
(10) | A = X*(B<C) + Y*!(B<C) | / |
(11) | !(X-D+C)||D | / |
(12) | A&&B||!0&&C&&!D | / |
(13) | ((A&&B)||(!0&&C))&&!D | <=> (A&&B||!0&&C)&&!D |
(14) | ((A&&B)||!0)&&(C&&(!D)) | <=> (A&&B||!0)&&C&&!D |
التمرين 3.7
#include <stdio.h>
#include <math.h>
main()
{
double A;
double B;
double RES;
printf("A : "); scanf("%lf", &A);
printf(" B : "); scanf("%lf", &B);
RES = pow(A,B);
printf("A exposant B : %f \n", RES);
RES = sqrt(pow(A,2)+pow(B,2));
printf("Hypotenuse: %f \n", RES);
RES = sin(A)/cos(A);
printf("Tangente A: %f \n", RES);
/* الحساب */
RES = floor(A/B);
printf("Round of A/B : %f \n", RES);
RES = floor(1000*(A/B))/1000;
printf("The round value A/B with three decimal: %f \n", RES);
return 0;
}
التمرين 3.8
نعتبر التعريفات التالية :
long A = 15;
char B = 'A'; /* code ASCII : 65 */
short C = 10;
التعبير |
النوع | القيمة | |
(1) | C + 3 | int | 13 |
(2) | B + 1 | int | 66 |
(3) | C + C | int | 75 |
(4) | 3 * C + 2 * B | int | 160 |
(5) | 2 * B + (A + 10) / C | long | 132 |
(6) | 2 * B + (A + 10.0) / C | double | 132.5 |
الدرس 4: قراءة وكتابة المعطيات »»»» |
التمرين 4.1 |
التمرين 4.2 |
التمرين 4.3 |
التمرين 4.4 |
التمرين 4.5 |
التمرين 4.6 |
التمرين 4.7 |
التمرين 4.8 |
التمرين 4.9 |
التمرين 4.10 |
التمرين 4.1
النتائج :
C : n=6 p=2 q=1
D : n=6 p=3 q=1
E : n=6 p=2 q=0
F : n=6 p=3 q=1
G : S S
H : 83 83
I : 53 53
التمرين 4.2
#include <stdio.h>
main()
{
int DAY, MONTH, YEAR, RECU;
printf("Write the date (DAY, MONTH, YEAR) : ");
RECU=scanf("%i %i %i", &DAY, &MONTH, &YEAR);
printf("\nReceived data : %i\nday : %i\nmonth :
%i\nyear : %i\n", RECU, DAY, MONTH, YEAR);
return 0;
}
يمكنك تعويض scanf بالشكل التالي:
. . .
RECU=scanf("%i-%i-%i", &DAY, &MONTH, &YEAR);
. . .
التمرين 4.3
أ) نستخدم getchar و printf,
#include <stdio.h>
main()
{
int C;
printf("Write a character and press 'Enter'\n");
C = getchar();
printf("%c has the ASCII code %d\n", C, C);
return 0;
}
ب) نستخدم getch و printf,
#include <stdio.h>
#include <conio.h>
main()
{
int C;
printf("Write a character \n");
C = getch();
printf("%c has the ASCII code %d\n", C, C);
return 0;
}
التمرين 4.4
#include <stdio.h>
main()
{
int A, B, C, AIDE;
printf("Write three integers (A, B, C) : ");
scanf("%i %i %i", &A, &B, &C);
/* إظهار القيم بتنسيق جدولي */
printf("A = %i\tB = %i\tC = %i\n", A, B, C);
AIDE=A;
A=C;
C=B;
B=AIDE;
printf("A = %i\tB = %i\tC = %i\n", A, B, C);
return 0;
}
التمرين 4.5
#include <stdio.h>
main()
{
int A, B;
printf("write two integers : ");
scanf("%i %i", &A, &B);
printf("Division : %i\n", A/B);
printf("Remaing : %i\n", A%B);
printf("Quotient : %f\n", (float)A/B);
return 0;
}
التمرين 4.6
#include <stdio.h>
main()
{
double R1, R2, R3, RRES;
printf("Give the values of R1, R2 et R3 : ");
scanf("%lf %lf %lf", &R1, &R2, &R3);
RRES=R1+R2+R3;
printf("Serial Resistance : %f\n", RRES);
RRES=(R1*R2*R3)/(R1*R2+R1*R3+R2*R3);
printf("Parallel Resistance : %f\n", RRES);
return 0;
}
ويمكنك أن تكتب أيضا
#include <stdio.h>
main()
{
double R1, R2, R3;
printf("Write the values of R1, R2 and R3 : ");
scanf("%lf %lf %lf", &R1, &R2, &R3);
printf("Serial Resistance : %f\n", R1+R2+R3);
printf("Parallel Resistance : %f\n", (R1*R2*R3)/(R1*R2+R1*R3+R2*R3));
return 0;
}
التمرين 4.7
#include <stdio.h>
#include <math.h>
main()
{
int A, B, C;
double P;
printf("Values of A, B and C : ");
scanf("%i %i %i", &A, &B, &C);
P=((double)A+B+C)/2;
printf("Surface of the triangle S = %f\n", sqrt(P*(P-A)*(P-B)*(P-C)));
return 0;
}
التمرين 4.8
أ) استعمال 5 متغيرات
#include <stdio.h>
main()
{
int A, B, C, D;
long S;
printf("Write the 1st number : "); scanf("%d", &A);
printf("Write the 2nd number : "); scanf("%d", &B);
printf("Write the 3rd number : "); scanf("%d", &C);
printf("Write the 4th number : "); scanf("%d", &D);
S = (long)A+B+C+D;
printf(" %d + %d + %d + %d = %ld\n", A, B, C, D, S);
return 0;
}
ب)استعمال متغيرين فقط
main()
{
int A;
long SOM;
SOM = 0;
printf("Write the 1st number : "); scanf("%d", &A);
SOM+=A;
printf("Write the 2nd number : "); scanf("%d", &A);
SOM+=A;
printf("Write the 3rd number : "); scanf("%d", &A);
SOM+=A;
printf("Write the 4th number : "); scanf("%d", &A);
SOM+=A;
printf("sum = %ld\n", SOM);
return 0;
}
التمرين 4.9
1)
#include <stdio.h>
main()
{
int PNET, TVA;
double PTTC;
printf("Net price of the item : "); scanf("%d", &PNET);
printf("Tax (%) : "); scanf("%d", &TVA);
PTTC = PNET+(double)PNET*TVA/100;
printf("The full price TTC = %.2lf dirham\n",PTTC);
return 0;
}
2)
#include <stdio.h>
main()
{
int TVA;
double PNET, PTTC;
printf("Write the full price TTC of the item : "); scanf("%lf", &PTTC);
printf("Write the tax rate TVA (in %) : "); scanf("%d", &TVA);
PNET = PTTC*100/(100+TVA);
printf("Net price is %.2lf dirham\n", PNET);
return 0;
}
التمرين 4.10
#include <stdio.h>
#include <math.h>
main()
{
int XA, YA, XB, YB;
double DIST;
printf("Coordinates of the point A : XA,YA "); scanf("%d,%d", &XA, &YA);
printf("Coordinates of the point B : XB,YB "); scanf("%d,%d", &XB, &YB);
DIST=sqrt(pow(XA-XB,2)+pow(YA-YB,2));
printf("Distance between A(%d,% d) and B(%d, %d) = %.2f\n", XA, YA, XB, YB, DIST);
return 0;
}
الدرس 5: أدوات الشرط »» »» |
التمرين 5.1 |
التمرين 5.2 |
التمرين 5.3 |
التمرين 5.4 |
التمرين 5.5 |
التمرين 5.6 |
التمرين 5.7 |
التمرين 5.1
if (A>B ) printf ("1st choice \n"); else
if (A>10) printf ("2nd choice \n");
if (B<10) printf ("3rd choice \n");
else printf ("4th choice \n");
1)
if (A>B) printf ("1st choice \n");
else if (A>10) printf ("2nd choice \n");
if (B<10) printf ("3rd choice \n");
else printf ("4th choice \n");
2)
A=10 and B=5 : |
1st choice |
3rd choice | |
A=5 and B=5 : | 3rd choice |
A=5 and B=10 : | 4th choice |
A=10 and B=10 : | 4th choice |
A=20 and B=10 : | 1st choice |
4th choice | |
A=20 and B=20 : | 2nd choice |
4th choice |
التمرين 5.2
نعتبر الأوامر التالية
if (A>B)
if (A>10)
printf ("1st choice \n"); else if (B<10)
printf ("2nd choice \n"); else
if (A==B) printf ("3rd choice \n");
else printf ("4th choice \n");
1)
if (A>B)
if (A>10)
printf ("1st choice \n");
else if (B<10)
printf ("2nd choice \n");
else if (A==B)
printf ("3rd choice \n");
else
printf ("4th choice \n");
2) النتيجة
"1st choice" | (A>B) و (A>10) | ||
"2nd choice" | (10=A>B) | ||
"3rd choice" | (10=A>B=10) و (A=B) | ||
10>10 مستحيل | |||
A>B و A=B مستحيل | => | "3rd choice" | |
"4th choice" | (10=A>B=10) و (A=B) | ||
10>10 مستحيل | => | "4th choice" |
التمرين 5.3
1) if - else و متغير للمساعدة MAX
#include <stdio.h>
main()
{
int A, B, C;
int MAX;
printf("Give three integers please :");
scanf("%i %i %i", &A, &B, &C);
if(A>B) MAX=A;
else MAX=B;
if(C>MAX) MAX=C;
printf("Maximum = %i\n", MAX);
return 0;
}
2) if - else if - ... - else بدون متغير للمساعدة
int A, B, C;
printf("Give three integers :");
scanf("%i %i %i", &A, &B, &C);
printf("maximum = ");
if (A>B && A>C) printf("%i\n",A);
else if (B>C) printf("%i\n",B);
else printf("%i\n",C);
3) العوامل المنطقية ومتغير للمساعدة MAX
int A, B, C;
int MAX;
printf("Give three integers :");
scanf("%i %i %i", &A, &B, &C);
MAX = (A>B) ? A : B;
MAX = (MAX>C) ? MAX : C;
printf("maximum = %i\n", MAX);
4) العوامل المنطقية بدون متغير للمساعدة
int A, B, C;
printf("Give three integers :");
scanf("%i %i %i", &A, &B, &C);
printf("maximum = %i\n",(A>((B>C)?B:C)) ? A : ((B>C)?B:C));
التمرين 5.4
#include <stdio.h>
main()
{
int A, B, C, AIDE;
printf("Give three integers :");
scanf("%i %i %i", &A, &B, &C);
printf("Before soring : \tA = %i\tB = %i\tC = %i\n", A, B, C);
if (A<B)
{
AIDE = A;
A = B;
B = AIDE;
}
if (A<C)
{
AIDE = A;
A = C;
C = AIDE;
}
if (B<C)
{
AIDE = B;
B = C;
C = AIDE;
}
printf("After sorting : \tA = %i\tB = %i\tC = %i\n", A, B, C);
return 0;
}
التمرين 5.5
#include <stdio.h>
main()
{
int A, B;
printf("Give three integers :");
scanf("%i %i", &A, &B);
if ((A>0 && B>0) || (A<0 && B<0))
printf("The sign of the product %i * %i is positive\n", A, B);
else if ((A<0 && B>0) || (A>0 && B<0))
printf("The sign of the product %i * %i is negative\n", A, B);
else
printf("The product %i * %i is zero\n", A, B);
return 0;
}
التمرين 5.6
#include <stdio.h>
#include <math.h>
main()
{
int A, B;
printf("Write two integers :");
scanf("%i %i", &A, &B);
if ((A>0 && B>0) || (A<0 && B>0 && fabs(A)<fabs(B))
|| (A>0 && B<0 && fabs(A)>fabs(B)))
printf("the sign of %i + %i is positive\n",A,B);
else if ((A<0 && B<0) || (A<0 && B>0 && fabs(A)>fabs(B))
|| (A>0 && B<0 && fabs(A)<fabs(B)))
printf("the sign of %i + %i is negative\n",A,B);
else
printf("the sum %i + %i is zero\n", A, B);
return 0;
}
التمرين 5.7
#include <stdio.h>
#include <math.h>
main()
{
int A, B, C;
double D;
printf("Solutions of the quadratic equation \n");
printf("in the form ax^2 + bx + c = 0 \n\n");
printf("Write the values of a, b, et c : ");
scanf("%i %i %i", &A, &B, &C);
D = pow(B,2) - 4.0*A*C;
if (A==0 && B==0 && C==0) printf("any real number is a solution.\n");
else if (A==0 && B==0) printf("there is no solution.\n");
else if (A==0) /* bx + c = 0 */
{
printf("It has only one solution = :\n");
printf(" x = %.4f\n", (double)C/B);
}
else if (D<0) /* b^2-4ac < 0 */
printf("This equation doesnt have real solutions.\n");
else if (D==0) /* b^2-4ac = 0 */
{
printf("This equation has only one solution:\n");
printf(" x = %.4f\n", (double)-B/(2*A));
}
else /* b^2-4ac > 0 */
{
printf("Real solutions are :\n");
printf(" x1 = %.4f\n", (-B+sqrt(D))/(2*A));
printf(" x2 = %.4f\n", (-B-sqrt(D))/(2*A));
}
return 0;
}
الدرس 6: أدوات التكرار »» »» |
التمرين 6.1 |
التمرين 6.2 |
التمرين 6.3 |
التمرين 6.4 |
التمرين 6.5 |
التمرين 6.6 |
التمرين 6.7 |
التمرين 6.8 |
التمرين 6.9 |
التمرين 6.10 |
التمرين 6.11 |
التمرين 6.12 |
التمرين 6.13 |
التمرين 6.14 |
التمرين 6.15 |
التمرين 6.1
أ) باستعمال while
#include <stdio.h>
main()
{
int N;
int NOMB;
int I;
long SOM;
double PROD;
printf("Number of data : ");
scanf("%d", &N);
SOM=0;
PROD=1;
I=1;
while(I<=N)
{
printf("%d. number: ", I);
scanf("%d", &NOMB);
SOM += NOMB;
PROD *= NOMB;
I++;
}
printf("The sum of %d numbers = %ld \n", N, SOM);
printf("Le product of %d numbers = %.0f\n", N, PROD);
printf("La average of %d numbers = %.4f\n", N, (float)SOM/N);
return 0;
}
ب) باستعمال do - while
SOM=0;
PROD=1;
I=1;
do
{
printf("%d. number: ", I);
scanf("%d", &NOMB);
SOM += NOMB;
PROD *= NOMB;
I++;
}
while(I<=N);
ج) باستعمال for
for (SOM=0, PROD=1, I=1 ; I<=N ; I++) { printf("%d. number: ", I); scanf("%d", &NOMB); SOM += NOMB; PROD *= NOMB; }
التمرين 6.2
نعوض هذه الأسطر
printf("Number of data : ");
scanf("%d", &N);
بــما يلي
do
{
printf("Number of data : ");
scanf("%d", &N);
}
while(N<1 || N>15);
أحسن أداة هي do - while.
التمرين 6.3
#include <stdio.h> main() { int NUM; int DEN; int DIV; int RES; printf("Write the numerator : "); scanf("%d", &NUM); printf("Write the denominateur : "); scanf("%d", &DEN); RES=NUM; DIV=0; while(RES>=DEN) { RES-=DEN; DIV++; } /* أو تستعمل */ /* for (RES=NUM, DIV=0 ; RES>=DEN ; DIV++) RES-=DEN; */ printf(" %d divided by %d = %d remains %d\n", NUM, DEN, DIV, RES); return 0; }
التمرين 6.4
#include <stdio.h> main() { int N; int I; double FACT; do { printf("Write a natural number : "); scanf("%d", &N); } while (N<0); I=1; FACT=1; while (I<=N) { FACT*=I; I++; } /* يمكن استعمال */ /*for (FACT=1.0, I=1 ; I<=N ; I++) FACT*=I; */ printf ("%d! = %.0f\n", N, FACT); return 0; }
التمرين 6.5
#include <stdio.h> main() { int X, N; int I; double RESU; do { printf("Write a natural number X : "); scanf("%d", &X); } while (X<0); do { printf("Write the exposant N : "); scanf("%d", &N); } while (N<0); for (RESU=1.0, I=1 ; I<=N ; I++) RESU*=X; if (N==0 && X==0) printf("zero exposant zero is not defined !\n"); else printf("Result : %d ^ %d = %.0f\n", X, N, RESU); return 0; }
التمرين 6.6
#include <stdio.h> main() { int N; int I; float SOM; do { printf ("Numbre of terms: "); scanf ("%d", &N); } while (N<1); for (SOM=0.0, I=1 ; I<=N ; I++) SOM += (float)1/I; printf("Sum of the %d first terms is %f \n", N, SOM); return 0; }
التمرين 6.7
#include <stdio.h> main() { int X; int N=0; int SOM=0; long PROD=1; do { printf("Write the %d%s numero: ", (N+1), (N)?"e":"er"); scanf("%d", &X); if (X<0||X>9) printf("\a"); else if (X) { N++; SOM+=X; PROD*=X; } else if (!X && N>0) { printf("Sum = %d \n", SOM); printf("Product = %ld\n", PROD); printf("Average = %f \n", (float)SOM/N); } } while (X); return 0; }
التمرين 6.8
#include <stdio.h>
main()
{
int X;
int N=0;
long VALD=1;
long NOMB=0;
do
{
printf("Write the %d%s numero : ", (N+1), (N)?"e":"er");
scanf("%d", &X);
if (X<0||X>9)
printf("\a");
else if (X)
{
NOMB += VALD*X;
N++;
VALD *= 10;
}
else
printf("The value of the reversed number is %ld\n", NOMB);
}
while (X);
return 0;
}
ملاحظة :
يمكن أن نعوض السطر
NOMB += VALD*X;
بــما يلي
NOMB += pow(10, N)*X;
التمرين 6.9
#include <stdio.h> main() { int NOMB; int NINV; do { printf("Write an integer (<10000) : ", NOMB); scanf("%d", &NOMB); } while (NOMB<0 || NOMB>9999); NINV=0; while(NOMB>0) { NINV *= 10; NINV += NOMB%10; NOMB /= 10; }
/* for (NINV=0 ; NOMB>0 ; NOMB/=10) NINV = NINV*10 + NOMB%10; */ printf("The inveresed number is : %d\n", NINV); return 0; }
التمرين 6.10
#include <stdio.h> main() { int N; float X; float A; float P; printf("Write the degree N of the polynom: "); scanf("%d", &N); printf("Write the value X of the argument: "); scanf("%f", &X); for(P=0.0 ; N>=0 ; N--) { printf("Write the coefficient A%d : ", N); scanf("%f", &A); P = P*X + A; } printf("Value of the polynom X = %.2f : %.2f\n", X, P); return 0; }
التمرين 6.11
#include <stdio.h> main() { int A, B; int X, Y, RESTE; do { printf("Write an integer A (not nul) : "); scanf("%d", &A); } while(!A); do { printf("Write an integer B (not nul) : "); scanf("%d", &B); } while(!B); for (RESTE=A, X=A, Y=B ; RESTE ; X=Y, Y=RESTE) RESTE = X%Y; printf("PGCD of %d and %d = %d\n", A, B, X); return 0; }
التمرين 6.12
#include <stdio.h>
main()
{
int U1, U2, UN;
int N;
int I;
do
{
printf("Rang of demanded terme : ");
scanf("%d", &N);
}
while(N<1);
U1=U2=1;
if (N==1)
UN=U1;
else if (N==2)
UN=U2;
else
{
for (I=3 ; I<=N ; I++)
{
UN = U1+U2;
U1 = U2;
U2 = UN;
}
}
printf("Value of the rang of %d : %d\n", N, UN);
return 0;
}
int U1, U2, UN; |
(==> %d) | U23 = 28657 |
long U1, U2, UN; | (==> %ld) | U46 = 1836311903 |
double U1, U2, UN; | (==> %e) | U1476 = 1.306989e308 |
long double U1, U2, UN; | (==> %Le) | U23601 = 9.285655e4931 |
التمرين 6.13
#include <stdio.h> main() { double A; double X; int N; int J; do { printf("Write a postive real number A : "); scanf("%lf", &A); } while(A<0); do { printf("Write the degree of the approximation : "); scanf("%d", &N); } while(N<=0 || N>=50); for(X=A, J=1 ; J<=N ; J++) { X = (X + A/X) / 2; printf("The %2d%s approximation of the square" " of %.2f is %.2f\n", J, (J==1)?"ère":"e", A, X); } return 0; }
التمرين 6.14
#include <stdio.h> main() { int LIG; int L; int ESP; int I; do { printf("Number of lines : "); scanf("%d", &LIG); } while (LIG<1 || LIG>20); for (L=0 ; L<LIG ; L++) { ESP = LIG-L-1; for (I=0 ; I<ESP ; I++) putchar(' '); for (I=0 ; I<2*L+1 ; I++) putchar('*'); putchar('\n'); } return 0; }
التمرين 6.15
#include <stdio.h>
main()
{
const int MAX = 10;
int I;
int J;
printf(" X*Y I");
for (J=0 ; J<=MAX ; J++) printf("%4d", J);
printf("\n------");
for (J=0 ; J<=MAX ; J++) printf("----");
printf("\n");
for (I=0 ; I<=MAX ; I++)
{
printf("%3d I", I);
for (J=0 ; J<=MAX ; J++)
printf("%4d", I*J);
printf("\n");
}
return 0;
}
تأليف
المؤلف الأصلي: فرديريك فابر (Frédéric FABER)
البريد الإلكتروني: عنوان البريد الإلكتروني هذا محمي من روبوتات السبام. يجب عليك تفعيل الجافاسكربت لرؤيته.
الموقع الإلكتروني: http://www.ltam.lu/cours-c
ترجمة بتصرف: محمد عبد الرحمان