C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 25th, 2013, 09:52 AM
DeepChanda486 DeepChanda486 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 DeepChanda486 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 6 sec
Reputation 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();
}

Reply With Quote
  #2  
Old February 25th, 2013, 10:40 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 3 m 48 sec
Reputation Power: 1774
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old February 25th, 2013, 10:46 AM
DeepChanda486 DeepChanda486 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 DeepChanda486 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 6 sec
Reputation Power: 0
so how exactly i correct the programee ????

Reply With Quote
  #4  
Old February 25th, 2013, 11:51 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
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.
__________________
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.

Reply With Quote
  #5  
Old February 25th, 2013, 12:08 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 50 m 50 sec
Reputation Power: 1949
Quote:
Originally Posted by DeepChanda486
so how exactly i correct the programee ????


Quote:
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.

Reply With Quote
  #6  
Old February 25th, 2013, 12:41 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,835 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 3 m 48 sec
Reputation Power: 1774
> 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
ManiacDan agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Euler's_totient_function. in C++

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap