Write a C++ prgm to read a number and print its Factors?
@shadowfiend @walne @myininaya @TuringTest
i want this in simple not complicated...
Sorry. I'm not the person to ask about programming. :(
ok...can to invite some one u knw..
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...
IE: for(i->n) { if((i * (num - i)) % num == 0) i and num - i are factors if(i > num / 2) break } Something like that.
wat..is this.....i need this in simple.....not complicated..
I'm not sure how much simpler it can get, sorry :/
no..prob..thxxx for helping!
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.
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; }
it....c prgm....i want c++
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.......
@hartnn @.Sam. @mukushla
lol...i dont know c++..
ok.....@hartnn u knw...
@Compassionate @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 :)
I don't know much about C++. I only took HTMLx
@A.Avinash_Goutham
#python code def factorsOf(number): factors = [] for i in xrange(1,number): if number%i == 0: factors.append(i) return factors
i dont knw thus....))
i need this in a simple...language..))
if you know c++, you can simply transfer the idea... python is quite simpler than c++ though
@theyatin
@Hashir
@ajprincess @shubhamsrg @experimentX @nick66 @eseidl
leme try int a; cin>>a; for (int =1;i<=a ;i++) {if (a % i == 0) cout<<i; }' hope this helps..
for (int =1;i<=a ;i++)????
the for loop..you must have studies this..
No..lol
even while can be used..
can u write this with out loop
well you gotta use some loop here,,otheewise i dont think will be simple..
just..try...lol
i give up ! ;)
oh...no..
PLZZ....Its urgent
loops is very easy,,comeon!!
thxxx
70 club...
it tells error @shubhamsrg
70 club? and what code you wrote ?
nothing
it tells undefined symbol i
eh ? please copy paste your entire code
same error
the code please..??
#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(); }
its meant to be int i =1 and not int =1//comeon,,atleast this much Common sense!
u have written like that
I've been getting too many notifications from this. Allow me too answer it. The answer is: 42
u knw i have nt studied.....looping
must be mis print,,but you should have got it..
u knw i have nt studied.....looping
so what,,you must be knowing int =1 means nothing!
^ No. 42
anyways,,sorry @Compassionate ! :P
42 !
now i got it
hmm,,thanks @Compassionate ! :D
Glad I could help.
SWAGGER TEN MILLION ONE HUNDRED TRILLION
okay buddy,,its alright,,relax!! ;)
Here I am giving the solution of your problem without using loops.But I am using jump statements to meet your requirements.
thxxx
welcome.
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.
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.
Join our real-time social learning platform and learn together with your friends!