Thread: Multiplication with Sort Function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    3

    Multiplication with Sort Function

    I need to include validation to only accept numbers and to only the last four odd numbers in the table. Appreciate if someone could help


    Code:
    #include <stdio.h>void main() {
    int upper, range, i;
        printf("Enter an integer to find multiplication table: ");
        scanf("%d",&upper);
        printf("Enter range of multiplication table: ");
        scanf("%d",&range);
        for(i=1;i<=range;++i) {
            if(i % 2 !=0){
             printf("%d x %d = %d""*""\n", upper, i, upper*i);
            }
            else {
                printf("%d x %d = %d\n", upper, i, upper*i);
            }
            }
            printf("\n* Indicates the Odd Numbers\n");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    Sorry abt that.. i need to mark the last odd numbers with an asterisk

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I need to include validation to only accept numbers...
    For validation, one approach would be to check the return value of "scanf()". A better approach would be to create a separate function for reading and validating input, using "fgets()" to read the entire input as a string and parsing it as needed (e.g. "sscanf()").

    Sorry abt that.. i need to mark the last odd numbers with an asterisk
    So not all odd numbers, but only the last few? You need to be more clear on your requirements. If you are only supposted to be marking the last couple of odd numbers, you would need a way to know when you're "getting close" to the end so you can start printing the asterisks - this would likely require keeping track of the results generated (i.e. with an array).

    Also, your logic is adding an asterisk to the equations where the multiplicand, not the result, is odd.



    Note that "main()" should be returning int, not void: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    You should also make sure your code has neat and consistent formatting and indenting; something like this:

    Code:
    #include <stdio.h>
    
    void main()
    {
        int upper, range, i;
        
        printf("Enter an integer to find multiplication table: ");
        scanf("%d",&upper);
        
        printf("Enter range of multiplication table: ");
        scanf("%d",&range);
        
        for(i=1;i<=range;++i) {
            if(i % 2 !=0){
                printf("%d x %d = %d""*""\n", upper, i, upper*i);
            }
            else {
                printf("%d x %d = %d\n", upper, i, upper*i);
            }
        }
        
        printf("\n* Indicates the Odd Numbers\n");
        
        return 0;
    }

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Using gigantic, bold text will not make us pay any more attention to your question as we would have in the first place.


    Sorry for not contributing to the question itself, but that annoyed me. And also Matticus is good at what he does so there isn't much that needs to be said on my end anyway.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-30-2011, 07:54 AM
  2. Problem in Matrix Multiplication (Using friend function)
    By wantsree in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2011, 08:03 AM
  3. multiplication function in Binary Field, please help
    By lovesunset21 in forum C Programming
    Replies: 18
    Last Post: 12-08-2010, 08:03 PM
  4. Return of the multiplication function
    By figura in forum C Programming
    Replies: 12
    Last Post: 10-09-2010, 11:40 AM
  5. Simple Multiplication Function.
    By admin in forum C Programming
    Replies: 3
    Last Post: 10-04-2010, 01:49 PM