C Programming Basic Projects
C Programming Basic Projects
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
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;
scanf_s("%d", &x);
printf("Enter the value of y: ");
scanf_s("%d", &y);
sum = x + y;
product = x * y;
difference = x - y;
quotient = x / y;
remainder = x % 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);
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
printf("%d\t\t%d\t\t%d\n",x,x*x,x*x*x);
return 0;
}