Thread: c multiplication table and sum of all values

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    36

    c multiplication table and sum of all values

    I made this program after some trial and error and learning about two dimensional arrays. Is there another, perhaps more elegant way to write this program, I can put down the condition statements later, but anyway here's the code

    Code:
    #include <stdio.h>
    
    int i = 0;
    int j = 0;
    int SIZE;
    int sum;
    
    int
    main(void)
    {
    	printf("Enter a number for its multiplication number ");
    	scanf("%d", &SIZE); // SIZE will determine the size of the array
    
    	int array[SIZE][SIZE]; //Initialize array after user inputs a number
    
    	for (i = 0; i < SIZE; i++)
    	{
    
    		for (j = 0; j < SIZE; j++)
    		{
    			array[i][j] = (i + 1) * (j + 1); // This will mutliply the rows and columns together each loop count
    
    			printf("%4d", array[i][j]); // Prints the array value done by above equation
    		}
    		
    		printf("\n");
    	}
    	printf("\n");
    
    	for (i = 0; i < SIZE; i++)
    	{
    		for (j = 0; j < SIZE; j++)
    		{
    			printf("array[%d][%d] is %d\n", i, j, array[i][j]); // Prints out where the loop is in the array and the value in said element
    			sum += array[i][j]; // Loop goes through and saves the values in sum while adding from the last value of sum each loop count
    
    		}
    	}
    	
    	printf("\nThe sum of all numbers is %d\n\n", sum); // Output of total sum
    
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    1
    You can probably reduce it to a simpler form

    Code:
    #include <stdio.h>
    
    int i = 0;
    int j = 0;
    int SIZE;
    int sum;
    
    int main(void)
    {
            printf("Enter a number for its multiplication number ");
            scanf("%d", &SIZE); // SIZE will determine the size of the array
    
            int array[SIZE][SIZE]; //Initialize array after user inputs a number
    
            for (i = 0; i < SIZE; i++)
            {
    
                    for (j = 0; j < SIZE; j++)
                    {
                            array[i][j] = (i + 1) * (j + 1); // This will mutliply the rows and columns together each loop count
                            sum += array[i][j]; // Loop goes through and saves the values in sum while adding from the last value of sum each loop count
    
                            printf("%4d", array[i][j]); // Prints the array value done by above equation
                    }
    
                    printf("\n");
            }
            printf("\nThe sum of all numbers is %d\n\n", sum); // Output of total sum
            printf("\n");
            return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplication table
    By yatesj05 in forum C Programming
    Replies: 7
    Last Post: 04-17-2011, 02:46 AM
  2. C Multiplication Table
    By trueman1991 in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 07:58 AM
  3. Multiplication table
    By freddyvorhees in forum C++ Programming
    Replies: 6
    Last Post: 08-02-2008, 11:09 AM
  4. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:46 PM
  5. multiplication table help
    By cprogstudent in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:32 PM