Q1. The following program generates the sum of the first n numbers.
#include <stdio.h>
main()
{
int n;
int i = 0;
int sum = 0;
printf("Please enter a positive integer: ");
scanf("%d", &n);
while (i < n) sum+=++i;
printf("the sum of the first %d numbers is %d\n", n, sum);
}
(a) Convert the program to calculate the factorial of n, which is the product of the first n numbers, and obtain the values of the factorial for the following inputs:
a. n = 3 n! = _______________
b. n = 12 n! = _______________
c. n = 13 n! = _______________
d. n = 20 n! = _______________
e. n = 23 n! = _______________
f. n = 43, n! = _______________
(2 points)
(b) If you see a problem with the results, what is the cause?
(1 point)
(c) Modify the program so that if the answer is correct, the program outputs the result, otherwise it outputs the message "Error: cannot compute factorial for this number". Obtain the output for the following inputs:
a. n = 3 n! = _______________
b. n = 12 n! = _______________
c. n = 13 n! = _______________
d. n = 20 n! = _______________
e. n = 23 n! = _______________
f. n = 43, n! = _______________
(2 points)
Arkadaşlar cevabı arıyorum sınavdayım HELP ME
Acil Sınav Sorusu
5
●529
- 28-09-2012, 18:19:18Kimlik doğrulama veya yönetimden onay bekliyor.//1. sorunun cevabi :
n yerine 4 girdiginiz zaman cikti 10 dur yani
1 den girdiginiz sayiya kadar olan sayilari toplayip ekrana basmakta.
saglamasi;
1+2+3+4 = 10
// soru 2 nin cevabi
#include <stdio.h> int faktoryel(int n) { int sonuc = 1; int i; i=0; for (i = n; i>= 2; i--) { sonuc *=i; } return sonuc; } main(){ printf("3! %d\n", faktoryel(3)); printf("12! %d\n", faktoryel(12)); printf("13! %d\n", faktoryel(13)); printf("20! %d\n", faktoryel(20)); printf("23! %d\n", faktoryel(23)); printf("43! %d\n", faktoryel(43)); getch(); }3! 6
12! 479001600
13! 1932053504
20! -2102132736
23! 862453760
43! 0
//3. soru
20 ve 43 sayilarinin faktoryelleri negatif ve 0 cikti sebebini degisken tiplerinden olabilir deneme yapmadim, bilmiyorum.
iyi forumlar. - 28-09-2012, 19:23:26Gemini adlı üyeden alıntı: mesajı görüntüle
Cevap şöyle:
#include <stdio.h>
main()
{
int n;
int i = 0;
int prod = 1;
printf("Please enter a positive integer: ");
scanf("%d", &n);
while (i >0) prod*=i--;
printf("the product of the first %d numbers is %d\n", n, prod );
}
Bu şekilde önce 3 girildi ise mesela, 3 den başlar, 0'dan büyükmü diye bakar. Büyükse sonuçla çarpar. 3*1 =3 eder. sonra i 1 azaltılır bu şekilde 3*2*1 hesaplanır ve i 0'a ulaşınca while döngüsünden çıkılır.
Fakat 0'ın verilmesi durumunda program sonucu 1 bulacaktır. 0'ın faktöryeli hesaplanmak istendiğinde (ya da negatif) hata mesajı görüntülenmelidir. O da şöyle:
#include <stdio.h>
main()
{
int n;
int i = 0;
int prod = 1;
printf("Please enter a positive integer: ");
scanf("%d", &n);
if(n<=0)
{
printf("Cannot calculate the factorial of this number");
return;
}
while (i >0) prod*=i--;
printf("the product of the first %d numbers is %d\n", n, prod );
}
Bir de eğer sonuç 32 biti aşarsa sonuç gösterilemez. Bu yüzden int veritipi değiştirilmeli. Sonucu tutamıyor. long yada ulong olabilesi aslında. - 29-09-2012, 09:50:49Evet zaten sorunun bir o kısmı programlama ile alakalı. Gerisi matematikGemini adlı üyeden alıntı: mesajı görüntüle