#include "stdafx.h"
#include <iostream>
int main()
{
int n, k;
printf("Décomposition d’un nombre en facteurs premiers\n");
printf("Introduire un nombre supèrieur à zéro:");
scanf("%d", &n);
while (n > 0)
{
printf("Facteurs premier: ");
while (n % 2 == 0);
{
printf("2 \n ");
n /= 2;
}
k = 3;
while (n > 1)
{
if (n%k != 0) { k += 2; }
else { printf("%d", k); n /= k; }
}
return 0;
}
It is very hard to tell from your indenting - and it may be a cut-and-paste error when uploading your code ... but you are, strictly, missing a final curly brace }
... and you have a spurious semicolon at the end of line 13
add a read into N at the end of your program. Run the program, and do not enter a value for this input until you have looked at the program's output. If it is correct, the program is simply finishing fast and you can't see the output.
//#include "stdafx.h" // DE-COMMENT IF USING VISUAL STUDIO
#include <iostream> // C++ style, but use <cstdio> if you want to revert to printf, scanf
#include <string> // added, but only to assist line 28
usingnamespace std; // EITHER ADD THIS HERE ...
// ... OR QUALIFY EVERYTHING THAT NEEDS IT (e.g. std::cout)
int main()
{
int n, k;
cout << "Decomposition of a number into prime factors\n";
cout << "Input a number greater than 0: ";
cin >> n;
// while( n > 0 ) // DELETE (unless it's a slightly unnatural way of checking input)
// { // DELETE
cout << "Prime factors: ";
while ( n % 2 == 0 ) // REMOVE THE SEMICOLON ; ****
{
cout << 2 << " ";
n /= 2;
}
k = 3;
while ( n > 1 )
{
if ( n % k != 0 ) { k += 2; }
else { cout << k << " "; n /= k; }
}
// } // DELETE
string dummy; cout << "\nPress enter"; getline( cin, dummy ); // simulated wait-for-enter
// return 0; // moved, but not needed
} // FINAL BRACE MUST BE ADDED *******
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
int main()
{
int n, k;
printf("Décomposition d’un nombre en facteurs premiers\n");
printf("Introduire un nombre supèrieur à zéro: ");
scanf("%d", &n);
printf("Facteurs premier: ");
while (n % 2 == 0)
{
printf("2 \n ");
n /= 2;
} /* while (n % 2 == 0) */
k = 3;
while (n > 1)
{
if (n % k != 0)
{
k += 2;
}
else
{
printf("%d", k);
n /= k;
}
} /* while (n > 1) */
_getch();
return 0;
}
}
@lastchance this one work thank you
@Thomas1965 Yes this one is closer to the example in my workbook but i can't make it to work
i'm starting to think i'm doing something wrong with visual studio i just can't make "printf" and "scanf" to work even in the other example
i want to thank everyone, i'm student through correspondence and its sometime hard alone
i just can't make "printf" and "scanf" to work even in the other example
What is the problem? Error messages ?
Try this. Go to File->New->Project...->Visual C++->Win32 Console Application
Click next
In the next window check and uncheck the following options https://pasteboard.co/GIjFGni.jpg
Click finish.
In the Solution explorer on Source right-click and choose Add->New Item->C++ File, call it main.cpp and click add.
Paste your code into main.cpp, remove #include "stdafx.h" and save it.
Press Crtl+F5 to run.