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

Assignment 2

The document contains 9 programming problems in C language. The problems include: 1) Evaluating expressions and operators precedence table. 2) Simple calculator using switch case. 3) Printing sum of a series. 4) Pattern printing. 5) Checking Armstrong numbers. 6) Finding sum of digits. 7) Printing Fibonacci series. 8) Checking prime numbers. 9) Blank 10th problem. Sample code solutions are provided for each problem.

Uploaded by

soniamtech2014
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 2

The document contains 9 programming problems in C language. The problems include: 1) Evaluating expressions and operators precedence table. 2) Simple calculator using switch case. 3) Printing sum of a series. 4) Pattern printing. 5) Checking Armstrong numbers. 6) Finding sum of digits. 7) Printing Fibonacci series. 8) Checking prime numbers. 9) Blank 10th problem. Sample code solutions are provided for each problem.

Uploaded by

soniamtech2014
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 2

1. What is operators and expression? Prepare operator precedence and associativity


table. Evaluate the following expression and find the value of X. Where a=2,b=3,c=4:
(i) X=3/a++ - ++b*c
(ii) X=12%5-b++ - ++a/2
(iii) X=c<<2
(iv) X=1&&(-4)||7
(v) X=a++ || b|c
2. W.A.P. in C to implement simple calculator for arithmetic operations using Switch
case.
3. W.A.P. in C to print sum of the following series:

3 3 3
4 6 8
S= 23 +
2!
+ 3!
+ 4!
+ ..………………………+UPTO Nth
4. W.A.P. in C to print the following pattern:
1
12
123
1234
5. W.A.P. in C to find Armstrong number from 1 to 100.
6. W.A.P. in C to print the sum of digits of a given number.
7. W.A.P. in C to print Fibonacci series.
8. W.A.P. in C to check that given number is prime or not.
9.
10.

1
Operators and Expressions:

Operators: In programming, operators are symbols that represent computations or operations


on variables and values. Operators perform operations on one or more operands. For
example, addition (+), subtraction (-), multiplication (*), and division (/) are arithmetic
operators.

Expressions: An expression is a combination of variables, constants, and operators that can


be evaluated to produce a value. For example, a + b is an expression where a and b are
variables, and + is the addition operator.

Operator Precedence and Associativity Table:

Category Operators Associativity


Postfix ++ -- Left to right
Unary + - ++ -- ! ~ Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < > <= >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR ` `
Logical AND && Left to right
Logical OR `
Conditional ? : Right to left
Assignment =, +=, -=, *=, /=, %= Right to left
Comma , Left to right

Expressions Evaluation:

1. X = 3/a++ - ++b * c:

makefile
 a = 2, b = 3, c = 4
X = 3/2 - 4 * 4
X = 1.5 - 16
X = -14.5

 X = 12%5 - b++ - ++a/2:

makefile
 a = 3, b = 3
X = 12 % 5 - 3 - 2
X = 2 - 3 - 2
X = -3

 X = c << 2:

bash
 c = 4
X = 4 << 2 (Left shift by 2 bits)
X = 16

 X = 1 && (-4) || 7:

makefile
 X = (1 && (-4)) || 7
X = (1 && 1) || 7
X = 1 || 7
X = 1

 X = a++ || b | c:

makefile
5. a = 3, b = 3, c = 4
6. X = 3 || (3 | 4)
7. X = 1 || 7
8. X = 1
9.

Simple Calculator:

c
 #include <stdio.h>

int main() {
char operator;
double num1, num2;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf", &num1, &num2);

switch (operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}

return 0;
}

 Sum of Series:

#include <stdio.h>
#include <math.h>

int main() {
int n;
double sum = 0;

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


scanf("%d", &n);

for (int i = 1; i <= n; i++) {


sum += pow(i * 2, 3) / fact(i + 1); }

printf("Sum of the series: %.2lf\n", sum);

return 0;
}

 Pattern Printing:

#include <stdio.h>

int main() {
int rows;

printf("Enter the number of rows: ");


scanf("%d", &rows);

for (int i = 1; i <= rows; i++) {


for (int space = 1; space <= rows - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}

 Armstrong Numbers:

#include <stdio.h>
#include <math.h>

int main() {
int n, original, remainder, result = 0;

printf("Enter an integer: ");


scanf("%d", &n);

original = n;

while (original != 0) {
remainder = original % 10;
result += pow(remainder, 3);
original /= 10;
}

if (result == n) {
printf("%d is an Armstrong number.\n", n);
} else {
printf("%d is not an Armstrong number.\n", n);
}

return 0;
}

 Sum of Digits:

#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
sum += n % 10;
n /= 10;
}

printf("Sum of digits: %d\n", sum);

return 0;
}

 Fibonacci Series:

#include <stdio.h>

int main() {
int n, first = 0, second = 1, next;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", first, second);

for (int i = 2; i < n; i++) {


next = first + second;
printf("%d, ", next);
first = second;
second = next;
}

printf("\n");
return 0;
}

 Check Prime Number:

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
if (n <= 1) {
return false;
}

for (int i = 2; i <= n / 2; i++) {


if (n % i == 0) {
return false;
}
}

return true;
}

int main() {
int num;

printf("Enter an integer: ");


scanf("%d", &num);

if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}

You might also like