מבוא לתכנות ולמדעי המחשב בשפת C/תרגיל 1: הבדלים בין גרסאות בדף

תוכן שנמחק תוכן שנוסף
שורה 114:
 
[http://orimosenzon.com/dan/index.php?title=%D7%9E%D7%91%D7%95%D7%90_%D7%9C%D7%AA%D7%9B%D7%A0%D7%95%D7%AA/%D7%AA%D7%A8%D7%92%D7%99%D7%9C_1&printable=yes גרסת הדפסה של התרגיל]
 
== פתרון ==
=== q2 ===
<syntaxhighlight>
#include <stdio.h>
 
int main() {
int n;
printf("Please enter exercise number: "); scanf("%d",&n);
if (n <= 0)
printf("Exerecise number cannot be negative or zero\n");
else {
printf("/*\nIntroduction to programming and computer science.\n");
printf("Exercise %d\n",n);
printf("Ori Mosenzon\nID:01212121-4\n*/\n");
}
return 0;
}
</syntaxhighlight>
=== q3 ===
<syntaxhighlight>
#include <stdio.h>
#include <math.h>
int main() {
printf("Please enter the three coefficients of a quadratic equation\n(a*x^2+b*x+c=0)\n");
double a,b,c;
printf ("a: ");
scanf("%lf",&a);
printf ("b: ");
scanf("%lf",&b);
printf ("c: ");
scanf("%lf",&c);
if(a == 0)
if (b == 0)
if (c == 0)
printf("Any number is a solution to this equation\n");
else
printf("This equation has no soltions\n");
else
printf("x=%lf\n",-c/b);
else {
double d = b*b-4*a*c;
if (d < 0)
printf ("There are no real solutions to this equation\n");
else
if (d==0)
printf("x = %.2lf \n",-b/(2*a));
else {
d = sqrt(d);
printf("x1 = %.2lf, x2 = %.2lf \n",(-b+d)/(2*a),(-b-d)/(2*a));
}
}
return 0;
}
</syntaxhighlight>
 
 
<center>