Mükemmel Sayı Bulma
Öncelikle mükemmel sayı demek sayının kendisi hariç pozitif tam bölenlerinin toplamı sayıya eşit olması durumudur.
#include <iostream>
#include <string>
using namespace std;
int main() {
float sayi, kontrol;
int toplam = 0;
cout << " --> Bir sayi girin: ";
cin >> sayi;
cout << "\n" << sayi << " Sayinin pozitif tam bolenleri; ";
for(int i=1; i<sayi; i++) {
kontrol = sayi/i;
if(kontrol == int(kontrol)) {
cout << i << ", ";
toplam += i;
}
}
if(toplam == sayi) {
cout << "\n Girilen sayi MUKEMMEL SAYIDIR!\n\n";
} else {
cout << "\n Girilen sayi MUKEMMEL SAYI DEGILDIR!\n\n";
}
return main();
}
Çıktı :
--> Bir sayi girin: 5
5 Sayinin pozitif tam bolenleri; 1,
Girilen sayi MUKEMMEL SAYI DEGILDIR!
--> Bir sayi girin: 6
6 Sayinin pozitif tam bolenleri; 1, 2, 3,
Girilen sayi MUKEMMEL SAYIDIR!
--> Bir sayi girin:
Ekran çıktısı Bulma
#include <iostream>
#include <string>
using namespace std;
int fonk(int t, int u){
int a;
if(u==1) {
a = t;
cout<<u<<") "<<a<<endl;
return t;
} else{
a = t * fonk(t,u-1);
cout<<u<<") "<<a<<endl;
return a; }
}
int main(){
cout<<"Main: "<<fonk(2,4)<<endl;
}
Çıktı :
1) 2
2) 4
3) 8
4) 16
Main: 16
#include <iostream>
#include <string>
using namespace std;
int a=0;
int f2(int &a, int b)
{
::a+=a-b;
a--;
return a+::a;
}
int f1(int a) {
int z=0;
for(int i=a; i>0; i--)
{ z+=f2(i,i-1);
cout<<z<<" ";
}
return z+a;
}
int main() {
int a=10;
cout<<f1(a);
return 0;
}
Çıktı :
10 19 27 34 40 50
#include <iostream>
#include <string>
using namespace std;
string f(char *p)
{
string s(2,p[0]);
int i=0;
for(char *c=p; *c!=0; c++)
if(i++%3==0)
s+=*c;
return s; }
int main() {
cout<<f("test ediliyor");
}
Çıktı :
ttttdir
#include <iostream>
#include <string>
using namespace std;
int c=1; int f2(int);
int f1(int a) {
c+=a;
return a<10?f2(a+c):1;
}
int f2(int c)
{
cout<<c<<" "<<::c<<endl;
return f1(c); }
int main(){
cout<<f2(1)<<endl;
}
Çıktı :
1 1
3 2
8 5
21 13
1
Bu yazı yorumlara kapalı.