PDA WEEKLY CODING SESSION
1.Write a C program to get values in different data types from the
users and display it to the user.
Code:
Output:
2. Write a C program to get the marks (6 Subjects) from a student,
calculate Total, Average Cut-off mark of that student and display it to
the student.
Code:
Output:
3. Write a C++ program to assign value 5 to following variables a, b, c,
d using a single statement.
Code:
Output:
5. Write a C++ program to accept the following input (Input 1 and
Input 2) and produce the output using the respective operator.
Code:
Output:
6. Write a C++ program to accept the following input and produce
the respective output.
Code:
Output:
7. Write a C++ program to apply increment/decrement operator on a
= 10 sequentially and print the result of each of the following.
Code:
Output:
8. Program to convert temperature from Celsius to Fahrenheit and
vice versa using the following formula:F = (C*9/5) +32. Sample
input/output:
Code:
Fahrenheit to Celsius:
Output:
Celsius to Fahrenheit:
Code:
Output:
9. Program to input the values of two variables a and b and swap /
interchange the values.
Code:
Output:
10. Write a C program and find the output of the following.
Expression Output
10 == 10 ?
11 > 12 ?
12>=13 ?
13 <10 ?
Code:
Output:
11. Write a c program that calculates the area of a rectangle using
the length and width provided by the user. Use arithmetic operators.
Code:
Output:
12. Write a C program that takes an integer as input and checks if it's
even or odd. Use conditional statements (if-else).
Code:
Output:
13. Write a C code snippet that checks if a number is positive,
negative, or zero using conditional statements.
Code:
Output:
14. Write a C++ program that takes three integers as input and finds
the largest among them. Use conditional statements (if-else if).
Code:
Output:
15. Write a C++ program that calculates the Grade Point Average
(GPA) for a student based on their grades.
The program should take the grades as input, calculate the GPA, and
display the result.
Code:
Output:
THEOREY QUESTIONS:
1. List out the fundamental datatypes of c.
In C programming, the fundamental data types form the basic
building blocks for storing and manipulating data. These
include:
int (Integer): Used to represent integer values, such as
whole numbers. The size and range of an integer depend
on the system architecture, with examples ranging from -
32768 to 32767 for a 16-bit system or -2147483648 to
2147483647 for a 32-bit system.
float:This data type represents single-precision floating-
point numbers and is typically 32 bits in size. It is suitable
for representing real numbers with moderate precision,
making it useful in various mathematical and scientific
computations.
double: Representing double-precision floating-point
numbers, the double data type is typically 64 bits in size.
It offers higher precision compared to float and is
commonly used when more accurate representation of
real numbers is required.
char : Used to represent a single character, typically 8
bits in size. Char variables store characters using ASCII or
another character encoding system, enabling the
representation of letters, digits, and special symbols.
void: This data type represents the absence of a type and
is commonly used as the return type for functions that do
not return a value. Additionally, void serves as a
placeholder in function arguments when the function
does not take any parameters.
Bool : Introduced in C99, the _Bool data type represents
boolean values, indicating either true or false. Its size is
implementation-dependent but is typically 8 bits.
2. Write the syntax of ternary conditional operators in C.
Syntax:
Var= condition ? expression_if_true : expression_if_false;
Example:
Max=(a>b)?a:b;
This results true or false respective of teir output.
3. Explain the difference between the pre-increment operator (+
+x) and post-increment operator (x++) in c.
both the pre-increment (`++x`) and post-increment (`x++`)
operators are used to increase the value of a variable by 1.
However, there is a crucial difference between them: the order
in which the increment operation occurs in relation to the use
of the variable.
1. Pre-increment (`++x`):
- Increments the value of the variable `x` before its current
value is used in the expression.
- The updated value of `x` is used in the expression where the
increment operation is applied.
```c
#include <stdio.h>
int main() {
int x = 5;
int result = ++x;
printf("x: %d\n", x);
printf("result: %d\n", result);
return 0;
}
```
2. **Post-increment (`x++`):**
- Increments the value of the variable `x` after its current
value is used in the expression.
- The current value of `x` is used in the expression, and then
`x` is incremented.
```c
#include <stdio.h>
int main() {
int x = 5;
int result = x++;
printf("x: %d\n", x);
printf("result: %d\n", result);
return 0;
}
4. What is the difference between the switch statement and the if
statement in c ? When would you use one over the other?
5. What is the purpose of the logical AND (&&) and logical OR (||) operators in C++?
Provide example of their usage..