I am having trouble refining some code. My code takes a number "n" and calculates that many prime numbers. I need to display 10 primes per line of output data. Any tips would be appreciated.
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("How many primes would you like?");
scanf("%d",&n);
if ( n >= 1 )
{
printf("2");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf(" %d",i);
count++;
}
i++;
}
return 0;
}
Just try
printf(" %5d", i);
/* ^ to help align the numbers
and
if ((count + 1) % 10 == 0)
fputc(stdout, '\n');
fix for the first time when you already print 2.
bool is_prime(int anyNum) //takes an integer array returns, is_prime
{
bool is_prime = true;
for (int c = 2; c <= anyNum - 1; c++)
{
if (anyNum % c == 0)
{
//printf("%d is not prime\r\n" , anyNum);
is_prime = false;
}
}
return is_prime;
}
int main()
{
int num_primes;
printf("How many primes would you like: ");
std::cin >> num_primes;
printf("\r\nScanned Primes Are---\r\n");
int foundPrimes = 0;
int x = 0;
for (; x <= num_primes; x++)
{
bool gotLuckyFindingPrime = is_prime( x );
if (gotLuckyFindingPrime)
{
if (foundPrimes % 10 == 0)
{
printf("\r\n");
}
printf(" %d", x);
foundPrimes = (foundPrimes + 1) % 10;
}
}
}
Does handle ten digit showing on cmd too, you can experiment with formatting
Related
I have an assignment and I need to add up the digits of it and ignore the once that repeat themselves
for example 234111 -> 2 + 3 + 4 + 1 -> 10
I tried doing this:
#include
int main(void)
{
int i = 0;
int num = 0;
int sum = 0;
printf("Please enter a number\n");
scanf("%d", &num);
while(num > 0){
sum += num%10;
num /= 10;
}
printf("%d", sum);
return 0;
}
what I did just adds up the digits, it doesn't ignore that ones that get repeated
What do i need to add to the code?
You can keep an array of 'flags' for which digits have been used already:
#include <stdio.h>
int main(void)
{
// int i = 0; // You don't actually use this in the code!
int num = 0;
int sum = 0;
int used[10] = { 0, }; // Set all "used" flags to zero
printf("Please enter a number\n");
scanf("%d", &num);
while (num > 0)
{
int digit = num % 10; // Get the digit
if (!used[digit]) sum += digit; // Only add if not used already
used[digit] = 1; // Now we have used it!
num /= 10;
}
printf("%d", sum);
return 0;
}
Feel free to ask for further clarification and/or explanation.
Just read each character and record if you've already seen it:
#include <stdio.h>
#include <ctype.h>
int
main(void)
{
int seen[10] = {0};
int sum = 0;
int c;
while( ( c = getchar()) != EOF ) {
int v = c - '0';
if( isspace(c)) {
continue;
}
if( v < 0 || v > 9 ) {
fprintf(stderr, "Invalid input\n");
return 1;
}
if( ! seen[v]++ )
sum += v;
}
printf("%d\n", sum);
return 0;
}
I would like to get an output of the biggest even number. but when I input 1 2 3 (3 calls to scanf) the output is 4.
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Before the loop initialize ary[0] for example the following way (otherwise uninitialized value of ary[0] is used in the program)
ary[0] = 1;
then substitute these if statements
if(ary[x]%2==0)
{
if(ary[0]<ary[x])
for
if( ary[x]%2==0 && ( x == 1 || ary[0]<ary[x] ) )
And at last write
if ( ary[0] != 1 ) printf("%d",ary[0]);
Take into account that this call
fflush(stdin);
has undefined behavior and should be removed.
In fact there is no need to declare an array. Without the array the program can look like
#include <stdio.h>
int main( void )
{
unsigned int n;
int max_even = 1;
printf("How many numbers are you going to enter: ");
scanf("%u", &n);
int x;
for (unsigned int i = 0; i < n && scanf( "%d", &x ) == 1; i++)
{
if ((x % 2) == 0 && (max_even == 1 || max_even < x))
{
max_even = x;
}
}
if (max_even != 1)
{
printf("maximum entered even number is %d\n", max_even);
}
else
{
puts("None even number was enetered");
}
return 0;
}
Its output might look like
How many numbers are you going to enter: 10
0 1 2 3 4 5 6 7 8 9
maximum entered even number is 8
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int ary[0 = 0;
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Your code does not work because ary[0] is not yet initialized the first time you compare its value to the value read, furthermore it might not be even for the other comparisons.
You should use an indicator telling you whether an even value has been seen.
Here is a solution:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int has_even = 0, max_even = 0, value, amount, x;
if (scanf("%d", &amount) != 1)
return 1;
for (x = 0; x < amount; x++) {
if (scanf("%d", &value) != 1)
break;
if (!has_even || value > max) {
max_even = value;
has_even = 1;
}
}
if (has_even)
printf("%d\n", max_even);
else
printf("no even value\n");
getchar();
return 0;
}
I am currently a student, trying to get factorials to print out as prime numbers multiplied to certain exponents like so:
5! = (2^3)(3^1)(5^1)
However, I keep getting an unusual error, which occurs right after using scanf to retrieve my input (By the way, I would really appreciate someone showing me how to retrieve multiple inputs from an exterior file to do this using input redirection, since that's how we were supposed to retrieve our inputs for this).
Anyway, I'm assuming this error is somewhere in the specification for my while loop. I would greatly appreciate any help/tips/pointers. Thank you!
#include <stdio.h> //headers
#include <stdbool.h>
//function prototypes - I will be using functions inside of each other
int find_prime_count (int prime, int num);
int find_next_prime (int prime);
bool is_prime (int num);
int main(void) //main function
{
int primeCount[100] = {0}, prime = 2, fact, i = 2, temp = 2, currentPrimeCount, printCount = 0;
printf ("Enter number: ");
scanf ("%d", &fact);
while (i <= fact)
{
printf ("i is less than factorial");
while (temp != 1)
{
printf ("Temp is not equal to one");
currentPrimeCount = find_prime_count (prime, temp);
printf ("currentPrimeCount calculated");
temp = temp / (currentPrimeCount * prime);
printf ("Temp updated");
primeCount[prime + 1] += currentPrimeCount;
printf ("primeCount[prime + 1] updated");
prime = find_next_prime (prime);
printf ("Next prime found");
}
i += 1;
temp = i;
}
printf ("%3d! = ", fact);
i = 0;
while (i < 100)
{
if (primeCount[i] != 0)
{
if (printCount == 0)
{
printf ("(%d^%d)", i, primeCount[i]);
}
else if (printCount != 0)
{
printf (" * (%d^%d)", i, primeCount[i]);
}
printCount += 1;
if ((printCount % 9) == 0)
{
printf ("/n");
}
if ((printCount > 9) && ((printCount % 9) == 0))
{
printf (" ");
}
}
}
return 0;
}
bool is_prime (int num)
{
bool check = true; //sets check variable to true
int i = 2; //starts counter variable at 2 (will test all numbers >=2 && <num)
while (i < num && check == true)
{
if ((num % i) == 0) //if it is divisible by any number other than 1 and itself
{
check = false; //it is not a prime number and the check becomes false
}
i += 1; //increasing counter
}
return check; //returns boolean value
}
int find_next_prime (int prime)
{
int i = prime;
bool check = false;
printf ("find_next_prime starts.");
while (check == false)
{
i += 1;
check = is_prime (i);
}
printf ("find_next_prime ends.");
return i;
}
int find_prime_count (int prime, int num)
{
int count = 0;
printf ("find_prime_count starts.");
while ((prime % num) == 0)
{
count += 1;
num = num / prime;
}
printf ("find_prime_count ends.");
return count;
}
Using gdb, I can tell that it is a divide by zero error in prim % num.
Hints:
Compile with the -g flag
Run using gdb
Set a breakpoint ...
I am kind of confused. I'd like to make a program where if the number in the array has been already input, then it will detect it and say it was repeated, so the program would tell the user to put another non-repeated integer.
#include <stdio.h>
#define SIZE 5
int main()
{
int array[SIZE];
int i;
int j;
for (i = 0; i < SIZE; i++)
{
printf("[%d] Insert a number: ", i + 1);
scanf("%d", &array[i]);
j = i - 1; // This is the closest that I've gotten guys. But I need to create a loop to make j be -1 until it finds a repeated number in the array.
if (array[i] == array[j])
{
printf("The number is repeated");
i--;
}
if (array[i] > 1000)
{
printf("Sorry, the number you entered cannot be bigger than 1000\n");
i--;
}
if (array[i] < 0)
{
printf("Sorry, the number you entered cannot be less than 0\n");
i--;
}
}
for (i = 0; i < SIZE; i++)
{
printf("The array inside is %d\n", array[i]);
}
return 0;
}
As you can see, I did something similar. I just put j = i - 1 so basically it will tell the program that it was repeated. However, I suppose that I should create a loop that will subtract -1 to j until it finds the repeated value (if there is one). I just not have any idea how to create that loop and make it work.
Thank you very much!
The checks can be done the following way (without testing)
int array[SIZE];
int i;
for (i = 0; i < SIZE; i++)
{
int valid = 1;
int num;
do
{
printf("[%d] Insert a number: ", i + 1);
scanf("%d", &num );
if ( !( valid = !( num > 1000 ) ) )
{
printf("Sorry, the number you entered cannot be bigger than 1000\n");
}
else if ( !( valid = !( num < 0 ) ) )
{
printf("Sorry, the number you entered cannot be less than 0\n");
}
else
{
int j = 0;
while ( j < i && num != array[j] ) j++;
if ( !( valid = j == i ) )
{
printf("The number is repeated");
}
}
} while ( !valid );
array[i] = num;
}
This should work for you:
#include <stdio.h>
#define SIZE 5
int main() {
int array[SIZE];
int numberCount, repeatCount;
for(numberCount = 0; numberCount < SIZE; numberCount++) {
printf("[%d] Insert a number:\n>", numberCount + 1);
scanf("%d", &array[numberCount]);
for(repeatCount = 0; repeatCount < numberCount; repeatCount++) {
if (array[numberCount] == array[repeatCount]) {
printf("\nThe numbe is repeated\n");
numberCount--;
break;
}
}
if (array[numberCount] < 0) {
printf("\nSorry, the number you entered cannot be less than 0\n");
numberCount--;
}
if (array[numberCount] > 1000) {
printf("\nSorry, the number you entered cannot be bigger than 1000\n");
numberCount--;
}
}
printf("\n\n");
for(numberCount = 0; numberCount < SIZE; numberCount++)
printf("The array inside is %d\n", array[numberCount]);
return 0;
}
I have created this C program that is supposed to tell me how many prime numbers are between 1-25, but it is printing that every number is prime number. Please Help
#include<stdio.h>
int main(void) {
int n = 1, counter = 0;
int i, flag = 0;
while ( n <= 25 )
{
for ( i = 2; i <= (n/2); i++ )
{
if (n%i == 0)
{
flag = 0;
}
}
if (flag == 0)
{
counter++;
printf("%d is Prime Number.\n", n);
}
else
{
printf("%d is not Prime Number.\n", n);
}
n++;
}
return 0;
}
flag = 0;
Your flag is always 0. You have to set it to 1, while entering the while loop
while ( n <= 25 ) {
flag = 1; //<-- here
for ( i = 2; i <= (n/2); i++ ) {
You can as well break in the segment, just for efficiency
if (n%i == 0)
{
flag = 0;
break;
}
you can use the code as,
for ( i = 2; i <= sqrt(n); i++ )
{
if (n%i == 0)
{
flag = 0;
}
}
if (flag == 0)
{
counter++;
printf("%d is Prime Number.\n", n);
flag=1;
}
else
{
printf("%d is not Prime Number.\n", n);
}
n++;
use sqrt() instead of n/2 program will be more efficient.