C

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Perfect number

#include <iostream>
using namespace std;
void perfect(int x){
int sum, num;
for (int i = 1; i <= x; i++) {
sum = 0;
for (int j = 1; j <= i / 2; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
cout << i << " is a perfect number." <<endl;
}
}
}

int main() {
cout << "Perfect numbers between 1 and ";
int x;
cin>>x;
perfect(x);
return 0;
}

Power a^b
#include <iostream>
using namespace std;
void power(int x,int y){
int sum;
sum =x ;
for (int i = 1; i < y; i++) {
sum *=x ;
}
cout<<sum;
}

int main() {
int x,y;
cout<<"a^b"<<endl;
cout<<"a = ";
cin>>x;
cout<<"b = ";
cin>>y;
power(x,y);
return 0;
}

Prime num
#include <iostream>
using namespace std;
void prime(int x){
bool y;
for (int i = 2; i*i <= x; i++) {
cout << i<<endl;
if (x%i == 0)
{ y=true;}
}
if (y)
{cout << x << " is a non-prime number." <<endl;}
else
{cout << x << " is a prime number." <<endl;}
}

int main() {
cout << "cheack prime or not ";
int x;
cin>>x;
prime(x);
return 0;
}

Stuct

#include <iostream>
using namespace std;
struct student{
string name;
int id;
double pey;

void info(string n,int x,double y)


{
name=n;
id=x;
pey=y;
}

void show()
{
cout<<"name = "<<name<<endl;
cout<<"id = "<<id<<endl;
cout<<"peyment = "<<pey<<endl;

};

int main() {
student s[3];
for (int i = 0; i < 3; i++) {
string n;
int x;
double y;
cin >> n >> x >> y;
s[i].info(n, x, y);
}

for (int i = 0; i < 3; i++) {


s[i].show();
}

return 0;
}

Class

#include <iostream>
using namespace std;
class student{
private:
string name;
int id;
double pey;
public:
void info(string n,int x,double y)
{
name=n;
id=x;
pey=y;
}

void show()
{
cout<<"name = "<<name<<endl;
cout<<"id = "<<id<<endl;
cout<<"peyment = "<<pey<<endl;

};

int main() {
student s[1];
for (int i = 0; i < 1; i++) {
string n;
int x;
double y;
cin >> n >> x >> y;
s[i].info(n, x, y);
}
for (int i = 0; i < 1; i++) {
s[i].show();
}

return 0;
}

You might also like