1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
2
Rep Power
0

#### Euler's_totient_function. in C++

i am working on a programme to evaluate Euler's_totient_function.
the code fragment doesnt show any error yet returns the same value with no change..helP...
Code:
#include<iostream.h>
#include<conio.h>
int check(int a)
{
int i,j,flag=0;
for(i=2;i<=a/2;i++)
{
j=a%i;
if(j==0)
flag++;
}
if(flag==0)
return 1;
else
return 0;
}
void main()
{
int i,num,p;float s,k,t=1.0;
cout<<"\n Enter the number";
cin>>num;
p=num;
for(i=2;i<num;i++)
{
if(num%i==0)
{
if(check(i)==1)
{
k=1-(1/i);
t=t*k;
cout<<"\n "<<k;

}
}
}
cout<<"\n "<<p*t;

getch();
}
2. Step 1 is learn about [code][/code] tags, then your code might look presentable.
Code:
```#include<iostream.h>
#include<conio.h>
int check(int a)
{
int i, j, flag = 0;
for (i = 2; i <= a / 2; i++) {
j = a % i;
if (j == 0)
flag++;
}
if (flag == 0)
return 1;
else
return 0;
}

void main()
{
int i, num, p;
float s, k, t = 1.0;
cout << "\n Enter the number";
cin >> num;
p = num;
for (i = 2; i < num; i++) {
if (num % i == 0) {
if (check(i) == 1) {
k = 1 - (1 / i);
t = t * k;
cout << "\n " << k;
}
}
}
cout << "\n " << p * t;

getch();
}```
> k = 1 - (1 / i);
Now look up integer arithmetic.
k might be a float, but the conversion from int to float happens a lot later than you think it does.

> #include<iostream.h>
> #include<conio.h>
It's been about 15 years since iostream.h was a valid way of including C++ IO streams, and 20+ years since conio.h last had some meaningful relevance.

In other words, your compiler is beyond obsolete (in the same way that Egyptian hieroglyphics can be considered a modern writing system).

Not to mention the whole void main thing being wrong as well.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
2
Rep Power
0
so how exactly i correct the programee ????
4. Re-write it from scratch using programming techniques from the last couple decades. Your program is so old nobody remembers how to fix it. If you're doing this for homework, drop the course and buy a C++ book written after 2005 on amazon. If you're doing this for work, buy a C++ book written after 2005 on amazon and talk to your boss about modernizing your codebase.
5. Originally Posted by DeepChanda486
so how exactly i correct the programee ????
Originally Posted by salem
> k = 1 - (1 / i);
Now look up integer arithmetic.
k might be a float, but the conversion from int to float happens a lot later than you think it does.
1/1 = 1
1/2 = 0
1/3 = 0
1/4 = 0
1/5 = 0
1/6 = 0
1/7 = 0
1/8 = 0
1/9 = 0
1/10 = 0
1/11 = 0
1/12 = 0
1/13 = 0
1/14 = 0
1/15 = 0

Are you beginning to see a pattern with integer division?

To get floating-point division, at least one of the operands, dividend or divisor, needs to be a floating-point datatype.
6. > Today, 04:40 PM salem wrote many words of wisdom.

> Today 04:46 PM DeepChanda486 wrote
> so how exactly i correct the programee ????

My next advice is spend more than 6 minutes learning from what was said before bouncing right back with another "feed me" post.

Allowing for propagation delays, the time it takes the board to send an email, the time it takes you to receive that email, read it, do something, come back to the board etc etc, one wonders whether you actually even read my post at all, save to determine that it didn't contain your code with all the corrections already done.

YOU fix it, then you'll understand both
a) how to avoid finding yourself at the bottom of this hole again.
b) if you happen to fall into the hole again, you'll know the symptoms to look for and how to solve the problem.