February 25th, 2013, 10:52 AM

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();
}
February 25th, 2013, 11:40 AM

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.
February 25th, 2013, 11:46 AM

so how exactly i correct the programee ????
February 25th, 2013, 12:51 PM

Rewrite 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.
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." 2600 Magazine, Fall 2002
Think we're being rude? Maybe you
asked a bad question or you're a
Help Vampire. Trying to argue intelligently? Please
read this.
February 25th, 2013, 01:08 PM

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 floatingpoint division, at least one of the operands, dividend or divisor, needs to be a floatingpoint datatype.
February 25th, 2013, 01:41 PM

> 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.
Comments on this post