Thread: multiple If statements

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    14

    Smile multiple If statements

    Hello. I'm writing a code that should take two numbers as input from the user, one 8 digit number and one that's 10-150 more than the first one. Then it should print all the numbers and assign them various features ( for example: prime number, perfect number, etc.)
    I've been having trouble with what the structure to allow this would be, I have no issue with the input, It's suppose to ultimately look like this :
    If anyone could help me with the structure of what this code should be.. like how and where to place the if statements so each number is checked for these features...
    Thanks in advance
    Attached Images Attached Images multiple If statements-capture-png 

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would suggest you first work on a isPrime and isPalindrom functions.

    Looks like you should first check to see if the number is prime.
    And, if not prime see if it is a [perfect number or ] Palindrom.
    If it is prime, you should check for the types of prime numbers.
    I never heard of the sub-types of prime numbers.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    I would suggest you first work on a isPrime and isPalindrom functions.

    Looks like you should first check to see if the number is prime.
    And, if not prime see if it is a [perfect number or ] Palindrom.
    If it is prime, you should check for the types of prime numbers.
    I never heard of the sub-types of prime numbers.

    Tim S.
    Code:
    while ( firstnum <= secondnum){			if (firstnum == 1) printf("%10d Special number.\n", firstnum);//special number check
    			for (i = 1; i < firstnum; i++){ //determining if the number is a perfect number
    				remainder = firstnum % i;
    				if (remainder == 0){
    					sum = sum + i;
    				}
    			}
    			if (sum == firstnum){ printf("%10d Perfect number.\n", firstnum); }
    			
    			else printf("%10d\n", firstnum);
    			firstnum++;
    		}
    I tried to make it work for even just 3 cases however it wouldn't display 6 as a perfect number although it is

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    Are these the only categories:
    Code:
     1 Special number
     2 Germain prime number
     4 Palindrome number
     6 Perfect number
     7 Prime number
    17 Lower pair prime number
    What is a "special number"? (Is it only 1?)

    And what if a number fits into more than one category (like 11 which is both prime and palindromic).

    As perfect numbers are extremely sparse it's best to check for them like this:
    Code:
    bool check_perfect(int n) { // only works up to 8-digit numbers
        return (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336);
    }
    (include stdbool.h for bool, true, and false)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Quote Originally Posted by john.c View Post
    Are these the only categories:
    Code:
     1 Special number
     2 Germain prime number
     4 Palindrome number
     6 Perfect number
     7 Prime number
    17 Lower pair prime number
    What is a "special number"? (Is it only 1?)

    And what if a number fits into more than one category (like 11 which is both prime and palindromic).

    As perfect numbers are extremely sparse it's best to check for them like this:
    Code:
    bool check_perfect(int n) { // only works up to 8-digit numbers
        return (n == 6 || n == 28 || n == 496 || n == 8128 || n == 33550336);
    }
    (include stdbool.h for bool, true, and false)
    There is a certain preference of what feature to assign to a number that fits more than one category, I just can't figure out a way to write it to work properly. Yeah, special is just 1 , and I know that it'd be better to check for perfect numbers that way but this is an assignment and they specifically asked up not to do that ..

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you or do you not know how to write function?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    Do you or do you not know how to write function?

    Tim S.
    No, I've never used any.. I'm trying to run a series of if statements on the variable and mark the results with flags and then let the program print a feature depending on what flags are marked '1'.. I'd like any other, more efficient way.. thanks

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Apr 2018
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    Actually for this assignment i'm not allowed to use functions.. I made it work I'm just wondering if there's any way to make it run faster using just loops, break and continue.. and everything i've used there..
    The hierarchy is 1. Special number (just 1)
    2.Perfect number ( also we were asked specifically to work with an algorithem and not a list despite the fact there are only 4 number in that range)
    3. Germain prime ( a prime number(n) so that 2*n+1 is also a prime)
    4. Lower pair prime number ( a prime number (n) such that n+2 is also prime)
    5. Palindrome
    6. anything else
    there's really just an hierarchy for the prime number ; other than that I just figured that if a number is perfect it's not prime and vice versa and the rest is how it should be.
    Code:
    #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
    int main(){
    	int firstnum = 0, secondnum = 0, remainder, sum = 0, i, j,
    		divisor, p, reverse,
    		flag1, flag2, flag3, flag4, flag5, flag6;
    	while (firstnum > 99999999 || firstnum < 1){								//checking to see if both numbers meet requirements
    		printf("Please enter the 1st int positive number (up to 8 digits):\n");
    		scanf("%d", &firstnum);
    	}
    	while (secondnum < firstnum + 10 || secondnum > firstnum + 150){
    		printf("Please enter the 2nd int positive number between %d and %d :\n", firstnum + 10, firstnum + 150);
    		scanf("%d", &secondnum);
    	}
    	printf("The range:\n");
    	while (firstnum <= secondnum){
    		p = firstnum, reverse = 0;
    		sum = 0, flag1 = 0, flag2 = 0, flag3 = 1, flag4 = 0, flag5 = 0, flag6 = 0;
    		while(1){
    		//determining if the number is a special number
    			if (firstnum == 1) { flag1 = 1; break; }
    		//determining if the number is a perfect number
    		for (j = 1; j < firstnum; j++){
    			remainder = firstnum % j;
    			if (remainder == 0){
    				sum = sum + j;
    			}
    		}
    		if (sum == firstnum){ flag2 = 1; break; }
    		//determining if the number is a prime number
    		if (firstnum < 2)
    			flag3 = 0; 
    		if (firstnum == 2){
    			flag3 = 1; 
    		}
    		for (int i = 2; i*i <= firstnum; i++)
    		{
    			if (firstnum%i == 0){
    				flag3 = 0; break;
    			} 
    			
    		}
    		if (flag3 == 1){
    			//determining if the prime number is a lower pair or a germain.
    			for ( i = 2, flag5=1; i*i <= (firstnum + 2); i++)
    			{
    				if ((firstnum + 2) % i == 0){
    					flag5 = 0; break;
    				}
    
    
    			}
    			for ( i = 2, flag6=1; i*i <= (2 * firstnum + 1); i++)
    			{
    				if ((2 * firstnum + 1) % i == 0){
    					flag6 = 0; break;
    				}
    				
    			}}
    
    
    
    
    		//determining if the number is a palindrom
    		while (p != 0)
    		{
    			reverse = reverse * 10 + p % 10;
    			p /= 10;
    		}
    		if (firstnum == reverse){ flag4 = 1; break; } break;
    	}
    		if (flag1 == 1) { printf("%10d Special number.\n", firstnum); }
    		else if (flag2 == 1) { printf("%10d Perfect number.\n", firstnum); }
    		else if (flag6 == 1) { printf("%10d Germain prime number.\n", firstnum); }
    		else if (flag5 == 1) { printf("%10d Lower pair prime number.\n", firstnum); }
    		else if (flag3 == 1) { printf("%10d Prime number.\n", firstnum); }
    		else if (flag4 == 1) { printf("%10d Palindrom number.\n", firstnum); }
    		else printf("%10d\n", firstnum);
    
    
    		firstnum++;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple if statements
    By Valter Núñez in forum C Programming
    Replies: 3
    Last Post: 01-21-2016, 08:20 PM
  2. Multiple if statements
    By Curtis Coffman in forum C Programming
    Replies: 5
    Last Post: 09-05-2012, 07:13 PM
  3. if statement with multiple switch statements.
    By blu in forum C Programming
    Replies: 27
    Last Post: 02-16-2012, 11:05 PM
  4. Multiple if statements and conditions
    By tomeatworld in forum C Programming
    Replies: 19
    Last Post: 11-07-2010, 10:43 AM
  5. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 03:06 PM

Tags for this Thread