Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

Write a C++ prgm to read a number and print its Factors?

OpenStudy (anonymous):

@shadowfiend @walne @myininaya @TuringTest

OpenStudy (anonymous):

i want this in simple not complicated...

myininaya (myininaya):

Sorry. I'm not the person to ask about programming. :(

OpenStudy (anonymous):

ok...can to invite some one u knw..

OpenStudy (anonymous):

Well here can't you just write a loop from i->num checking the modulii of i and num - i. If the remainder is 0, those are factors. Break out of the loop after i > num / 2. Just off the top of my head here...

OpenStudy (anonymous):

IE: for(i->n) { if((i * (num - i)) % num == 0) i and num - i are factors if(i > num / 2) break } Something like that.

OpenStudy (anonymous):

wat..is this.....i need this in simple.....not complicated..

OpenStudy (anonymous):

I'm not sure how much simpler it can get, sorry :/

OpenStudy (anonymous):

no..prob..thxxx for helping!

OpenStudy (anonymous):

This actually won't work, you'll need to nest two loops and run the multiplication against them to see if the remainder is 0. You can bail once the product exceeds the number.

OpenStudy (anonymous):

here is the program.i dont know c++ so well,thats why i posted it in c..... main() { int n,value; printf("enter the number"); scanf("%d",&n); if(n<0) printf("the factorial is none"); else if(n==0) printf("the factorial is 1"); else { value=factorial(n);//function use printf("factorial of %d=%d",n,value); } } factorial (int k) { int fact=1; if(k>1) fact=k*factorial(k-1);//recursion use return fact; }

OpenStudy (anonymous):

it....c prgm....i want c++

OpenStudy (anonymous):

pal i dont know c++ so well,i give u the concept in c,can u kindly write it in c++ and then run it...i hope it will run.......

OpenStudy (anonymous):

@hartnn @.Sam. @mukushla

OpenStudy (anonymous):

lol...i dont know c++..

OpenStudy (anonymous):

ok.....@hartnn u knw...

OpenStudy (anonymous):

@Compassionate @sasogeek

OpenStudy (sasogeek):

i can't write that in c++ cos i don't code c++, but that's a good idea to keep me writing some code, i'll try that with python :)

OpenStudy (compassionate):

I don't know much about C++. I only took HTMLx

OpenStudy (anonymous):

@A.Avinash_Goutham

OpenStudy (sasogeek):

#python code def factorsOf(number): factors = [] for i in xrange(1,number): if number%i == 0: factors.append(i) return factors

OpenStudy (anonymous):

i dont knw thus....))

OpenStudy (anonymous):

i need this in a simple...language..))

OpenStudy (sasogeek):

if you know c++, you can simply transfer the idea... python is quite simpler than c++ though

OpenStudy (anonymous):

@theyatin

OpenStudy (sasogeek):

OpenStudy (anonymous):

@Hashir

OpenStudy (anonymous):

@ajprincess @shubhamsrg @experimentX @nick66 @eseidl

OpenStudy (shubhamsrg):

leme try int a; cin>>a; for (int =1;i<=a ;i++) {if (a % i == 0) cout<<i; }' hope this helps..

OpenStudy (anonymous):

for (int =1;i<=a ;i++)????

OpenStudy (shubhamsrg):

the for loop..you must have studies this..

OpenStudy (anonymous):

No..lol

OpenStudy (shubhamsrg):

even while can be used..

OpenStudy (anonymous):

can u write this with out loop

OpenStudy (shubhamsrg):

well you gotta use some loop here,,otheewise i dont think will be simple..

OpenStudy (anonymous):

just..try...lol

OpenStudy (shubhamsrg):

i give up ! ;)

OpenStudy (anonymous):

oh...no..

OpenStudy (anonymous):

PLZZ....Its urgent

OpenStudy (shubhamsrg):

loops is very easy,,comeon!!

OpenStudy (anonymous):

thxxx

OpenStudy (anonymous):

70 club...

OpenStudy (anonymous):

it tells error @shubhamsrg

OpenStudy (shubhamsrg):

70 club? and what code you wrote ?

OpenStudy (anonymous):

nothing

OpenStudy (anonymous):

it tells undefined symbol i

OpenStudy (shubhamsrg):

eh ? please copy paste your entire code

OpenStudy (anonymous):

same error

OpenStudy (shubhamsrg):

the code please..??

OpenStudy (anonymous):

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a; cin>>a; for (int =1;i<=a ;i++) {if (a % i == 0) cout<<i; } getch(); }

OpenStudy (shubhamsrg):

its meant to be int i =1 and not int =1//comeon,,atleast this much Common sense!

OpenStudy (anonymous):

u have written like that

OpenStudy (compassionate):

I've been getting too many notifications from this. Allow me too answer it. The answer is: 42

OpenStudy (anonymous):

u knw i have nt studied.....looping

OpenStudy (shubhamsrg):

must be mis print,,but you should have got it..

OpenStudy (anonymous):

u knw i have nt studied.....looping

OpenStudy (shubhamsrg):

so what,,you must be knowing int =1 means nothing!

OpenStudy (compassionate):

^ No. 42

OpenStudy (shubhamsrg):

anyways,,sorry @Compassionate ! :P

OpenStudy (compassionate):

42 !

OpenStudy (anonymous):

now i got it

OpenStudy (shubhamsrg):

hmm,,thanks @Compassionate ! :D

OpenStudy (compassionate):

Glad I could help.

OpenStudy (compassionate):

SWAGGER TEN MILLION ONE HUNDRED TRILLION

OpenStudy (shubhamsrg):

okay buddy,,its alright,,relax!! ;)

OpenStudy (anonymous):

Here I am giving the solution of your problem without using loops.But I am using jump statements to meet your requirements.

OpenStudy (anonymous):

thxxx

OpenStudy (anonymous):

welcome.

OpenStudy (anonymous):

Hello all; Well, this kind of problem can be solved using two methods : loops or recursive We start with loop's method because it's the easy one (the loop means do the same thing for many times without rewriting the part of code many times) : #include<stdlib.h> #include<iostream> using namespace std; int main(){ int n; cout <<"Enter a number : "; cin >> n; if (n<0) cout << "The number must be positive\n"; else { int facto = 1; //this variable will contain the value of the factorial for(int i=1; i<=n ;i++){ // Here the loop, it means do this work for n times. facto = facto * i; //Calculate the factorial. } cout <<"The factorial of " <<n <<" is : " <<facto <<"\n"; } system("pause"); } The second is the recursive method : #include<stdlib.h> #include<iostream> using namespace std; //This is a recursive function that can calculate the fact. int fact(int n){ //We give it the number n as an argument. if(n==0 || n==1) return 1; //if n =1 or 0 then the value of factorial is 1 else return n*fact(n-1); // if n>1 then call again the function fact and so on. } int main(){ int n; cout <<"Enter a number :"; // simple message cin >> n; //read the value of n if (n<0) cout << "The number must be positive\n"; else cout <<"The factorial of " <<n <<"is : " <<fact(n) <<"\n"; system("pause"); } I hope this help you a little, good luck.

OpenStudy (anonymous):

I think the problem needs to be clarified. Factors or Factorial? This two is not the same thing. I said this because some of you give Factors solution, and the other give Factorials solution.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!