I'm a first year collage student and I'm working on an exercise with a while function.
my current objective is to identify the lowest and highest value digits in a given number.
this is my code so far:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void main() {
printf("enter an integer\n");
int one,onemax,onemin, min=9, max=0;
scanf("%d", &one);
onemax = one;
onemin = one;
while (onemax >= 0) {
if (max <= onemax % 10) {
max = onemax % 10;
onemax=onemax / 10;
if (onemax == 0) {
break;
}
}
else {
onemax=onemax / 10;
}
}
while (onemin != 0) {
if (min >= onemin % 10) {
min = onemin % 10;
onemin=onemin / 10;
if (onemin == 0) {
break;
}
}
else {
onemin=onemin / 10;
}
}
printf("min = %d max = %d", min, max);
}
now I know I'm stuck in a loop here and I need help getting out.
when onemax reaches 0 you are done. You should change the >= condition to >:
while (onemax > 0) {
// Here ------^
Related
My code seems to work on some credit card numbers but for others it doesn't even run. I've tried to use long long for the credit card number but it didn't worked. I've spent like 2 hours trying to solve this issue but I can't figure it out. All help is welcomed :)
int main(void)
{
// Get credit card number
long num = get_cc_number();
// Check if the credit card number is valid
valid = check_sum(num);
// Check the length of the credit card number
length = check_length(num);
// Get the first two digits of the credit card number and first number of VISA
digits = get_first_digits(num);
digit_visa = digits / 10;
// Check if the card is American Express, Mastercard, Visa or Invalid
if (valid == 1)
{
if (length == 16)
{
if (digits <= 55 && digits >= 51)
{
printf("MASTERCARD\n");
}
else if (digit_visa == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else if (length == 15)
{
if (digits == 34 || digits == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else if (length == 13)
{
if (digit_visa == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
else
{
printf("INVALID\n");
}
}
long get_cc_number(void)
{
long cc_number;
cc_number = get_long("Credit Card Number: ");
return cc_number;
}
int check_sum(int num)
{
int num1 = num;
while (num1 >= 10)
{
sec_to_last = num1 % 100;
double_sec_to_last = sec_to_last * 2;
if (double_sec_to_last >= 10)
{
first_dig = double_sec_to_last / 10;
sec_dig = double_sec_to_last % 10;
first_sum += first_dig;
first_sum += sec_dig;
}
else
{
first_sum += double_sec_to_last;
}
num1 = num1 / 100;
}
int num2 = num;
while (num2 >= 10)
{
last = num2 % 10;
second_sum += last;
}
second_sum += first_sum;
if (second_sum % 10 == 0)
{
return 1;
}
else
{
return 0;
}
}
long check_length(long num)
{
long num_length = floor(log10(labs(num))) + 1;
return num_length;
}
long get_first_digits(long num)
{
long i = num;
while (i >= 100)
{
i /= 10;
}
return i;
}
A while back, I reviewed an issue like this where the user was getting tripped up on acquiring a credit card check digit. With that, I wrote a small proof-of-principle test program that allows validation of credit card numbers using the Luhn algorithm. Following, is that code snippet.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_valid(char * num)
{
int sum = 0;
int work = 0;
char card[20];
if ((strlen(num) %2 == 0)) /* Even numbers - do not need a leading zero */
{
strcpy(card, num);
}
else /* Odd numbers - add a leading zero to evaluate */
{
strcpy(card, "0");
strcat(card, num);
}
printf("Length of number is: %d\n", (int)strlen(num));
for (int i = 0; i < strlen(card); i++)
{
work = card[i] - '0';
if ((i %2) == 0)
{
work = (card[i] - '0') * 2;
if (work > 9)
{
work = work - 9;
}
}
sum = sum + work;
printf("Digit is: %d Value is: %d Sum is %d\n", (card[i]- '0'), work, sum);
}
return ((sum % 10) == 0);
}
int main()
{
char number[20];
int x = -1;
printf("Enter a number: ");
x = scanf("%s", number);
x = check_valid(number);
if (x == 0)
printf("Invalid\n");
else
printf("Valid\n");
return 0;
}
It doesn't identify the card issuer, just verifies that the number is valid.
As noted in the comments, one probably would want to utilize a string entry as this code snippet does rather than trying to utilize a very large integer. You might try going that route with a string as well. Give this a try and see if it allows you to progress.
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
I am learning C , could you help me find the error in my program?
I retyped code from the book and still I don't know where is my mistake.
I tried to use https://www.diffchecker.com/diff but I don't see any logical difference. I give up.
Could you help me?
My code:
//ex7_9.c
#include <stdio.h>
#include <stdbool.h>
int main(void){
int num;
int limit;
int div;
bool isPrime;
printf("Please insert your number: ");
while((scanf("%d", &limit) == 1) && limit > 0){
if(limit > 1)
printf("Here are prime numbers up to %d limit\n", limit);
else
printf("Change limit - to bigger than one\n");
for(num=2; num <= limit; num++){
for(div=2, isPrime=true; (div*div) <=num; div++){
if(num % div ==0)
isPrime = false;
if(isPrime)
printf("%d is prime \n",num);
}
}
}
return 0;
}
proper code:
//ex7_9.c
#include <stdio.h>
#include <stdbool.h>
int main(void){
int limit;
int num;
int div;
bool numIsPrime;
printf("Enter a positive integer: ");
while (scanf("%d", &limit) == 1 && limit > 0){
if (limit > 1)
printf("Here are the prime numbers up through %d\n", limit);
else
printf("No primes.\n");
for (num = 2; num <= limit; num++)
{
for (div = 2, numIsPrime = true; (div * div) <= num; div++)
if (num % div == 0)
numIsPrime = false;
if (numIsPrime)
printf("%d is prime.\n", num);
}
printf("Enter a positive integer (q to quit): ");
}
printf("Done!\n");
return 0;
}
results from my code:
Please insert your number: 10
Here are prime numbers up to 10 limit
5 is prime
7 is prime
9 is prime
You added brackets on your inner loop.
Your code is
for(num=2; num <= limit; num++){
for(div=2, isPrime = true; div * div <=num; div++){
if(num % div == 0)
{
isPrime = false;
}
if(isPrime)
{
printf("%d is prime \n",num);
}
}
}
while the original with explicit brackets added is
for (num = 2; num <= limit; num++)
{
for (div = 2, numIsPrime = true; div * div <= num; div++)
{
if (num % div == 0)
{
numIsPrime = false;
}
}
if (numIsPrime)
{
printf("%d is prime.\n", num);
}
}
Your second if is inside the inner loop; in the original it is outside.
I am doing the first problem on Project Euler.
I have the following code:
#include <stdio.h>
int main() {
int number;
int sum;
while (number < 1000) {
if (number % 3 == 0 || number % 5 == 0) {
sum += number;
number++;
}
else {
number++;
}
}
printf("The answer is %d", sum);
return 0;
}
When I compile this via compileonline.com, I get 233168. When I compile this in gcc I get 2686824. What causes this difference?
Compileonline probably initializes the variables.
You have to initialize them manually.
#include <stdio.h>
int main() {
int number = 0;
int sum = 0;
while (number < 1000) {
if (number % 3 == 0 || number % 5 == 0) {
sum += number;
number++;
}
else {
number++;
}
}
printf("The answer is %d", sum);
return 0;
}
I wrote a program to find all primes less that a user-input integer. However, it just hangs. I assume using all these continues and whatnot is a mess and I have made spaghetti code...can anyone help me?
/*takes integer input, displays all primes less than that integer*/
#include <stdio.h>
int main(void) {
unsigned int num_in, test_num = 0, divisor = 0;
_Bool primestate = 0;
printf("Please enter an integer.\n");
scanf("%d", &num_in);
while(test_num < num_in) {
while(divisor < test_num) {
if(test_num % divisor == 0) {
primestate = 1;
}
test_num++;
}
if(primestate == 1) {
printf("%d is prime and less than %d.\n", test_num, num_in);
} else {
continue;
}
}
return 0;
}
You never increment test_num or divisor, so it gets stuck in one of the loops.
while(test_num < num_in) {
while(divisor < test_num) {
if(test_num % divisor == 0) {
primestate = 1;
}
divisor++; // NEW LINE
}
if(primestate == 1) {
printf("%d is prime and less than %d.\n", test_num, num_in);
} else {
continue;
}
test_num++; // NEW LINE
}
You also have a possible division by 0 (when divisor equals 0) on this line:
if(test_num % divisor == 0) {
#include <stdio.h>
#include <stdbool.h>
int main(void) {
unsigned int num_in, test_num, divisor;
bool primestate;
printf("Please enter an integer.\n");
scanf("%u", &num_in);
if(2 < num_in)
printf("\n%u\n", 2);
for(test_num = 3; test_num < num_in; test_num += 2){
primestate = true;
for(divisor=3; divisor * divisor <= test_num ; divisor += 2) {
if(test_num % divisor == 0) {
primestate = false;
break;
}
}
if(primestate) {
printf("%u\n", test_num);
}
}
return 0;
}