Charotar University of Science & Technology
Chandubhai S. Patel Institute of Technology
V. T. Patel Department of Electronics & Communication
Engineering
Subject ECUC102 Basic C Programming
Do It Yourself (DIY) – scanf statement
Sr. No. Predict the output of following C based Snippets. Give the proper Justification for
the output. (Sr. No. 1 to 4)
1 #include <stdio.h>
int main() {
const int x ;
x=10;
printf("Value of x: %d\n", x);
return 0;
}
2 #include <stdio.h>
int main() {
const int y = 20;
y = 30;
printf("Value of y: %d\n", y);
return 0;
}
3 #include <stdio.h>
void print_value(const int num) {
num = 100;
printf("Received value: %d\n", num);
}
int main() {
int val = 50;
print_value(val);
return 0;
}
4 #include <stdio.h>
#define PI 3.14159
#define SQUARE(x) (x * x)
#define ADD_ONE(y) y + 1
#define MESSAGE "Hello, C Macros!"
Charotar University of Science & Technology
Chandubhai S. Patel Institute of Technology
V. T. Patel Department of Electronics & Communication
Engineering
int main() {
int radius = 5;
int num = 10;
printf("Value of PI: %f\n", PI);
printf("Square of %d: %d\n", radius, SQUARE(radius));
printf("Result of ADD_ONE(num): %d\n", ADD_ONE(num));
printf("Result of ADD_ONE(num * 2): %d\n", ADD_ONE(num * 2));
printf("Message: %s\n", MESSAGE);
return 0;
}
Write C program for given programming statement.
1 Declare an integer variable age and initialize it with your age.
Declare a floating-point variable price and initialize it with a product's price.
Declare a character variable initial and initialize it with the first letter of your name. Print the
values of all these variables.
2 Declare two integer variables a and b. Read values for a and b from the user.
Perform addition, subtraction, multiplication, and division on a and b.
Store the results in separate variables and print them.
3 Define a constant PI using the #define preprocessor directive with a value of 3.14159. Declare
a variable radius and assign it a value. Calculate the area of a circle using PI and radius, and
print the result.
4 Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches
and width of 5 inches.
5 Write a C program to convert specified days into years, weeks and days. Note: Ignore leap
year.
6 Write a C program that accepts two item's weight and number of purchases (floating point
values) and calculates their average value.
7 Write a C program to calculate a bike’s average consumption from the given total distance
(integer value) travelled (in km) and spent fuel (in litters, float number – 2 decimal points).
8 Write a C program to return the quotient and remainder of a division.
9 Write a C program that: Declares constants for the maximum marks per subject.
Charotar University of Science & Technology
Chandubhai S. Patel Institute of Technology
V. T. Patel Department of Electronics & Communication
Engineering
1. Takes input from the user for marks in 5 subjects:
Math, Science, English, History, and Computer.
2. Calculates:
o Total obtained marks
o Percentage
10 Input temperature in Celsius and convert it to Fahrenheit. Use float variables and const for
conversion factor.
11 Convert an amount in INR to USD. Use const float EXCHANGE_RATE and variables for
input/output.
12 Take radius and height as inputs. Use const float PI = 3.14159 and calculate volume = πr²h.
13 Swap the values of two variables using a temporary variable. Practice int or float types.
14 Take a character input and print its ASCII value using int.
15 Input weight and height, compute BMI using BMI = weight / (height * height)Use float and
constants.
16 Input number of units used, apply fixed per-unit rate (const float) and calculate bill.
Write answer of following questions (Theory)
1 What is a variable? How is it initialized? What are the rules for defining a variable name?
Explain Difference between constant and variable.
2 Write a short note on C tokens.
3 Explain different types of Constants in C.
4 What are escape sequence or backslash character constant? List them along with their usage.
5 Describe the classification of data types in C with example.
6 Write difference between const and #define.