I need a program written in C, not C+ or C++. I have no idea how to go about doing it. Basically, I need it to take in the following information and spit out the following for a number table. The operation should be able to take plus, minus, division or multiplication and the low/high row/column numbers should be able to go up to where ever. But I'm sure the following example is fine.
Enter low row number: 1
Enter high row number: 3
Enter low column number: 1
Enter high column number: 4
Enter operation: +
+ 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
** Table Complete **
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
/*Libraries*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
/*declaring integers and character*/
int lr,hr,lc,hc;
char op;
/*taking inputs from the user*/
printf("Enter low row number: ");
scanf("%d",&lr);
printf("Enter high row number: ");
scanf("%d",&hr);
printf("Enter low column number: ");
scanf("%d",&lc);
printf("Enter high coloumn number: ");
scanf("%d",&hc);
/*if low values greater than high values */
if(lr>hr || lc>hc)
{
/*it is invalid and program will exit*/
printf("You have entered invalid input");
exit(0);
}
/*taking operator as input from user*/
printf("Enter operation: ");
scanf(" %c",&op);
/*if operator is not (+,-,*,/) */
if(op != '+' && op != '-' && op != '*' &&
op != '/')
{
/*operators are invalid and program exits*/
printf("You have entered wrong operation");
exit(0);
}
/*printing operator*/
printf(" %c\t",op);
/*for loop will iterate from lc(low column) to hc(high
column)*/
for(int i = lc; i <= hc; i++)
{
/*printing values from lc to hc, one after one in every
iteration*/
printf("%d\t",i);
}
/*for loop will iterate from lr(low row) to hr(high row)*/
for(int j = lr; j <= hr; j++)
{
/*\n is escape character to jump to next line*/
printf("\n");
/*printing values from lr to hr, one after one in every
iteration*/
printf("%d\t",j);
/*this for loop is used for operation purpose
it will iterate from lc to hc*/
for(int k = lc; k <= hc; k++)
{
/* if op is + */
if(op == '+')
{
/*adding row element with column element*/
printf("%d\t",j+k);
}
/* if op is - */
else if(op == '-')
{
/*subtracting row element with column element*/
printf("%d\t",j-k);
}
/* if op is * */
else if(op == '*')
{
/*multiplying row element with column element*/
printf("%d\t",j*k);
}
/* if op is / */
else if(op == '/')
{
/*dividing row element with column element*/
printf("%d\t",j/k);
}
}
}
}
/*code ended here*/
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!
Get Answers For Free
Most questions answered within 1 hours.