0% found this document useful (0 votes)
38 views

C Programming Basic Projects

The document contains questions and answers related to C programming language operators and precedence. It discusses evaluating expressions and statements using different operators like *, /, +, -, % and brackets. It also includes programs to calculate sums, determine even/odd numbers, and print tables of squares and cubes.

Uploaded by

Neena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

C Programming Basic Projects

The document contains questions and answers related to C programming language operators and precedence. It discusses evaluating expressions and statements using different operators like *, /, +, -, % and brackets. It also includes programs to calculate sums, determine even/odd numbers, and print tables of squares and cubes.

Uploaded by

Neena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Question 1:

Given the equation y=ax3 +7 , which of the following, if any, are correct C
statements for this equation?
i. y=a∗x∗x∗x+ 7;
ii. y=a∗x∗x∗( x+ 7 ) ;
iii. y= ( a∗x )∗x∗( x+ 7 );
iv. y= ( a∗x )∗x∗x+ 7;
v. y=a∗ ( x∗x∗x )+ 7;
vi. y=a∗x∗(x∗x +7);

Answer:
Statement v is the correct one as it follows the operator precedence and
associativity in C language.

Question 2:
State the order of evaluation of the operators in each of the following C
statements and
show the value of x after each statement is performed.
i. x=7+3∗6 /2−1;
In C language, the operators / and * have the highest precedence and then
come the operators + and -. So in this expression for the value of x, the
step-by-step evaluation will be as follows:
x = 7+18/2-1 since 3*6=18
x = 7+9-1 since 18/2=9
x = 16-1 since 7+9=16
x = 15 since 16-1=15

ii. x=2 % 2+2∗2−2/2;


In C programming language, the operators *, / and % are evaluated first
and then the operators + and – are evaluated. It is to be noted that *, / and
% have equal precedence and the + and – operators have same
precedence.
Step-by-step evaluation of given expression is as follows:
x = 0+2*2-2/2 since 2%2=0
x = 0+4-2/2 since 2*2=4
x = 0+4-1 since 2/2=1
x = 4-1 since 0+4=4
x=3 since 4-1=3

iii. x=¿ ;
In the given expression, the order of precedence of arithmetic operators
has already been highlighted by the use of brackets. So step-by-step
evaluation of above expression is given below:
x = (3*9*(3+(27/(3)))) since 9*3=27
x = (3*9*(3+9)) since27/3=9
x = (3*9*12) since 3+9=12
x = (27*12) since * is a left-associative operator and 3*9=27
x = 324 since 27*12=324

Question 3:
Write a program that asks the user to enter two numbers, obtains them from
the user and prints their sum, product, difference, quotient, and remainder.
//calculates and prints the sum, product, difference, quotient and remainder of two
numbers
#include<stdio.h>

int main()

{
int x, y;

float sum, product, difference, quotient, remainder;

printf("Enter the value of x: ");

scanf_s("%d", &x);
printf("Enter the value of y: ");

scanf_s("%d", &y);

sum = x + y;

printf("\nSum is %f", sum); //prints the sum of x and y

product = x * y;

printf("\nProduct is %f", product); //prints the product of x and y

difference = x - y;

printf("\nDifference is %f", difference); //prints the difference of x and y

quotient = x / y;

printf("\nQuotient is %f", quotient); //prints the quotient of x and y

remainder = x % y;

printf("\nRemainder is %f", remainder); //prints the remainder when x is divided


by y

return 0;

}
Question 4:
Write a program that reads an integer and determines and prints whether
it is odd or even. [Hint: Use the remainder operator. An even number is a multiple
of two. Any multiple of two leaves a remainder of zero when divided by 2.]
/*determines and prints whether a number is even or odd*/

#include<stdio.h>

int main()

{
int x;
printf("Enter the value of x:"); /*prints the value of x*/
scanf_s("%d", &x);

if (x % 2 == 0) /*definition of even number*/

printf("x is even");
else

printf("x is odd");
return 0;

When x=4

When x = 9
Question 5:
Write a program that calculates the squares and cubes of the numbers from 0 to
10 and uses tabs to print the following table of values:
Number Square Cube
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
/*prints the numbers from 1 to 10 as well as calculates and prints their squares and
cubes*/

#include<stdio.h>

int main()
{
int x;

printf("Number\t\tSquare\t\tCube\n");

for

(x=1; x<=10; x++)

printf("%d\t\t%d\t\t%d\n",x,x*x,x*x*x);

return 0;
}

You might also like