Exercise 1: Write four simple C programs demonstrating data types, variables, constants, and
input:
a. Program on Data Types
Aim: To demonstrate the use of various data types in C.
Description: This program shows how different data types such as int, float, double, and
char are used in C. It assigns values to variables of these data types and prints them.
Program:
#include <stdio.h>
int main() {
int integerVar = 25; // Integer data type
float floatVar = 12.34; // Float data type
double doubleVar = 56.789012; // Double data type
char charVar = 'A'; // Character data type
printf("Integer value: %d\n", integerVar);
printf("Float value: %.2f\n", floatVar);
printf("Double value: %.6f\n", doubleVar);
printf("Character value: %c\n", charVar);
return 0;
}
b. Program on Variables
Aim: To illustrate the declaration, initialization, and use of variables in C.
Description: This program declares and initializes variables, performs arithmetic operations, and
displays the results using printf.
Declaration of variable: <data type> <variable>;
Declaration and initialization: <data type> <variable>=<value>;
Initialization of variable: <variable> = <value>;
#include <stdio.h>
int main() {
int a = 10; // Declaring and initializing an integer variable
int b = 20; // Declaring and initializing another integer
variable
int sum = a + b; // Declaring and initializing the sum variable
printf("Value of a: %d\n", a);
printf("Value of b: %d\n", b);
printf("Sum of a and b: %d\n", sum);
return 0;
}
c. Program on Constants
Aim: To demonstrate the use of constants in C.
Description: This program shows how to define constants using both the #define preprocessor
directive and the const keyword. It calculates the area of a circle using a constant value for pi.
#include <stdio.h>
#define PI 3.14159 // Defining a constant using #define
int main() {
const int radius = 5; // Defining a constant using the const keyword
double area = PI * radius * radius; // Calculating the area of a circle
printf("Radius of the circle: %d\n", radius);
printf("Area of the circle: %.2f\n", area);
return 0;
}
d. Program on Input and Output
Aim: To demonstrate input and output operations in C.
Description: This program takes different types of input from the user (integer, float, and
character) using scanf() and then displays the input values using printf().
#include <stdio.h>
int main() {
int number;
float decimal;
char character;
// Taking integer input
printf("Enter an integer: ");
scanf("%d", &number);
// Taking float input
printf("Enter a float: ");
scanf("%f", &decimal);
// Taking character input
printf("Enter a character: ");
scanf(" %c", &character); // Space before %c to consume any newline
character left in the buffer
// Displaying the input values
printf("\nYou entered integer: %d\n", number);
printf("You entered float: %.2f\n", decimal);
printf("You entered character: %c\n", character);
return 0;
}