// Character and boolean
printf("Char: %c\n", charVar);
// Using constants and computation
printf("\nUsing constants in computation:\n");
printf("Radius: %d\n", radius);
printf("Area of Circle (PI * r^2): %.2f\n", area);
return 0;
}
Design and test a C program to swap 2 numbers using a third variable and without using a third variable.
with variable
#include < stdio.h >
int main() {
int a = 10, b = 20, temp;
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
without variable
#include< stdio.h>
int main() {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Design and test a C program to compute volume and surface area of a sphere.
#include< stdio.h>
#define PI 3.14159
int main() {
float radius = 3.0, volume, surface_area;
volume = (4.0/3.0) * PI * radius * radius * radius;
surface_area = 4 * PI * radius * radius;
printf("Volume: %.2f\nSurface Area: %.2f\n", volume, surface_area);
return 0;
}
Design and test a C program to convert temperature in Fahrenheit to Celsius and vice versa.
#include< stdio.h>
int main() {
float fahrenheit, celsius;
fahrenheit = 98.6;
celsius = (fahrenheit - 32) * 5/9;
printf("Fahrenheit to Celsius: %.2f\n", celsius);
Design and test at least 4 C programs to using enlisted operators: (1) Assignment (2) Arithmetic (3) Relational (4) Logical
#include < stdio.h >
int main() {
// Assignment and Arithmetic Operators
int a = 10, b = 5, result;
// Arithmetic operations
result = a + b; // Addition
printf("Addition (a + b): %d\n", result);
result = a - b; // Subtraction
printf("Subtraction (a - b): %d\n", result);
result = a * b; // Multiplication
printf("Multiplication (a * b): %d\n", result);
result = a / b; // Division
printf("Division (a / b): %d\n", result);
result = a % b; // Modulus
printf("Modulus (a %% b): %d\n", result);
result += b; // Add and assign
printf("Add and assign (result += b): %d\n", result);
result -= b; // Subtract and assign
printf("Subtract and assign (result -= b): %d\n", result);
result *= b; // Multiply and assign
printf("Multiply and assign (result *= b): %d\n", result);
result /= b; // Divide and assign
printf("Divide and assign (result /= b): %d\n", result);
// Relational Operators
printf("\nRelational Operators:\n");
printf("a == b: %d\n", a == b); // Equal to
printf("a != b: %d\n", a != b); // Not equal to
printf("a > b: %d\n", a > b); // Greater than
printf("a < b: %d\n", a < b); // Less than
printf("a >= b: %d\n", a >= b); // Greater than or equal to
printf("a <= b: %d\n", a <= b); // Less than or equal to
// Logical Operators
printf("\nLogical Operators:\n");
int x = 1, y = 0; // Logical operands
printf("x && y (Logical AND): %d\n", x && y); // Logical AND
printf("x || y (Logical OR): %d\n", x || y); // Logical OR
printf("!x (Logical NOT): %d\n", !x); // Logical NOT
return 0;
}
Design and test at least 5 C programs using the enlisted operators: (1) Bitwise (2) Increment and Decrement (3) Conditional (4) Comma (5) size of
#include < stdio.h>
int main() {
// Bitwise Operators
int a = 5, b = 3; // Binary: a = 0101, b = 0011
printf("Bitwise Operators:\n");
printf("a && b (AND): %d\n", a && b); // Result: 0001 (1)
printf("a | b (OR): %d\n", a | b); // Result: 0111 (7)
printf("a ^ b (XOR): %d\n", a ^ b); // Result: 0110 (6)
printf("~a (NOT): %d\n", ~a); // Result: 1010 (2's complement)
printf("a << 1 (Left Shift): %d\n", a << 1); // Result: 1010 (10)
printf("a >> 1 (Right Shift): %d\n", a >> 1); // Result: 0010 (2)
// Increment and Decrement Operators
printf("\nIncrement and Decrement Operators:\n");
int x = 10;
printf("Initial value of x: %d\n", x);
printf("Post-increment (x++): %d\n", x++); // x is returned first, then incremented
printf("After post-increment, x: %d\n", x);
printf("Pre-increment (++x): %d\n", ++x); // x is incremented first, then returned
printf("Post-decrement (x--): %d\n", x--); // x is returned first, then decremented
printf("After post-decrement, x: %d\n", x);
printf("Pre-decrement (--x): %d\n", --x); // x is decremented first, then returned
// Conditional (Ternary) Operator
printf("\nConditional (Ternary) Operator:\n");
int y = 20;
int max = (x > y) ? x : y; // If x > y, max = x; otherwise, max = y
printf("x = %d, y = %d\n", x, y);
printf("Max of x and y using ternary: %d\n", max);
// Comma Operator
printf("\nComma Operator:\n");
int z;
z = (x = 5, y = 15, x + y); // Evaluates expressions left to right, result of last expression is assigned to z
printf("Using comma operator: x = %d, y = %d, z = %d\n", x, y, z);
// sizeof Operator
printf("\nsizeof Operator:\n");
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of x: %zu bytes\n", sizeof(x));
return 0;
}
Design and test at least 3 C programs to test the operator precedence and their associativity, implicit and explicit type conversion.
#include < stdio.h>
int main() {
int a = 10, b = 5, c = 2, result;
printf("Demonstrating Precedence and Associativity of Operators:\n");
// Example 1: Arithmetic Operators (Left to Right Associativity)
result = a + b * c; // * has higher precedence than +
printf("a + b * c = %d\n", result); // Equivalent to: a + (b * c)
result = (a + b) * c; // Parentheses change the order of evaluation
printf("(a + b) * c = %d\n", result);
// Example 2: Relational and Logical Operators (Left to Right Associativity)
int x = 10, y = 20, z = 30;
int relResult = x < y && y < z; // Relational operators have higher precedence than logical AND
printf("x < y &&&& y < z = %d\n", relResult); // Equivalent to: (x < y) &&&& (y < z)
relResult = x < y || y > z; // Logical OR evaluated after relational operators
printf("x < y || y > z = %d\n", relResult); // Equivalent to: (x < y) || (y > z)
// Example 3: Assignment Operators (Right to Left Associativity)
int d;
d = a = b + c; // Right to left associativity
printf("d = a = b + c = %d\n", d); // Equivalent to: a = (b + c); d = a
// Example 4: Mixed Operators
result = a + b > c ? a : b; // Ternary has lower precedence than arithmetic and relational operators
printf("a + b > c ? a : b = %d\n", result); // Equivalent to: ((a + b) > c) ? a : b
// Example 5: Precedence of Unary vs Binary Operators
result = -a + b; // Unary '-' has higher precedence than binary '+'
printf("-a + b = %d\n", result); // Equivalent to: (-a) + b
// Example 6: Bitwise vs Relational Operators
result = a && b < c; // Relational (<) has higher precedence than bitwise AND (&&)
printf("a && b < c = %d\n", result); // Equivalent to: a && (b < c)
// Example 7: Explicit Grouping Using Parentheses
result = (a && b) < c; // Parentheses alter evaluation order
printf("(a && b) < c = %d\n", result);
return 0;
}
Design and test at least 3 C programs to show formatted and unformatted input and output.
#include < stdio.h>
int main() {
char name[50];
int age;
float height;
char ch;
printf("Demonstrating Formatted and Unformatted Input/Output:\n\n");
// Unformatted Input
printf("Enter a single character: ");
getchar(); // Consume the leftover newline character
ch = getchar(); // Reads a single character
// Unformatted Output
printf("\nYou entered the character: ");
putchar(ch); // Outputs a single character
putchar('\n'); // Adds a newline
return 0;
}
Design and test at least 2 C programs using decision making statements: (1) Simple if (2) if…else (3) Nested if (4) if…else ladder (5) switch (6) goto
10.1 Simple if Statement
#include < stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", & num);
if (num > 0) { // Simple if statement
printf("The number is positive.\n");
}
return 0;
}
10.2 if…else Statement
#include < stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", & num);
if (num % 2 == 0) { // Condition to check even
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
10.3 Nested if Statement
#include < stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", & num);
if (num > 0) {
if (num % 2 == 0) { // Nested if
printf("The number is positive and even.\n");
} else {
printf("The number is positive and odd.\n");
}
} else {
printf("The number is not positive.\n");
}
return 0;
}
10.4 if…else if…else Ladder
#include < stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", & marks);
switch (choice) {
case 1:
printf("You chose Addition.\n");
break;
case 2:
printf("You chose Subtraction.\n");
break;
case 3:
printf("You chose Multiplication.\n");
break;
case 4:
printf("You chose Division.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
10.6 goto Statement
#include < stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", & num);
if (num < 0) {
goto negative; // Jumps to the 'negative' label
}
printf("The number is positive.\n");
return 0;
negative:
printf("The number is negative.\n");
return 0;
}
Design and test at least C programs using the for loop.
Print Numbers from 1 to 10
#include < stdio.h>
int main() {
printf("Numbers from 1 to 10:\n");
for (int i = 1; i <= 10; i++) { // Loop from 1 to 10
printf("%d ", i);
}
printf("\n");
return 0;
}
Design and test at least C programs using the while loop.
Print Numbers from 1 to 10
#include < stdio.h>
int main() {
int i = 1;
printf("Numbers from 1 to 10:\n");
while (i <= 10) { // Loop continues while i <= 10
printf("%d ", i);
i++; // Increment i
}
printf("\n");
return 0;
}
Design and test at least C programs using do…while loop.
Print Numbers from 1 to 10
#include < stdio.h>
int main() {
int i = 1;
printf("Numbers from 1 to 10:\n");
do {
printf("%d ", i); // Print the number
i++; // Increment i
} while (i <= 10); // Continue as long as i <= 10
printf("\n");
return 0;
}
Design and test a C program using break and continue statements.
Using break: Exit a Loop (This program terminates the loop when a specific condition is met.)
#include < stdio.h>
int main() {
int num;
printf("Enter numbers (enter -1 to stop):\n");
while (1) { // Infinite loop
scanf("%d", & num);
if (num == -1) { // Exit condition
break; // Exit the loop
}
printf("You entered: %d\n", num);
}
printf("Loop terminated because you entered -1.\n");
return 0;
}
Using continue: Skip an Iteration(This program skips even numbers and only processes odd numbers.)
#include < stdio.h>
int main() {
printf("Odd numbers between 1 and 10:\n");
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Design and test at least pattern programs using loop structures.
Print a Pattern (Triangle of Stars)
#include < stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", & rows);
for (int i = 1; i <= rows; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for columns
printf("* ");
}
printf("\n"); // Move to the next row
}
return 0;
}
Design and test at least 5 C programs using one dimensional array.
Find the Largest Element in an Array(This program finds the largest element in a one-dimensional array.)
#include < stdio.h>
int main() {
int n, largest;
printf("Enter the number of elements: ");
scanf("%d", & n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", & arr[i]);
}
largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("The largest element is: %d\n", largest);
return 0;
}
Design and test at least C programs using two dimensional arrays. Matrix Addition (This program adds two matrices of the same size.)
#include < stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", & rows, & cols);
int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", & matrix2[i][j]);
}
}
// Adding the matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Sum of the matrices:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Design and test at least C programs using strings.
#include < stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Design and test at least C programs using pointers.
#include < stdio.h>
int main()
{
int* pc, c;
c = 223;
printf("Address of c: %p\n", & c);
printf("Value of c: %d\n\n", c); // 223
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 223
c = 113;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 113
*pc = 23;
printf("Address of c: %p\n", & c);
printf("Value of c: %d\n\n", c); // 23
return 0;
}
Design and test a C program using the concept of pointer to pointer.
Design and test at least C programs using user defined functions.
#include < stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",& n1,& n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
Design and test at least C programs by applying the recursion concept.
#include < stdio.h>
int sum(int n);
int main() {
int number, result;
printf("Enter a positive integer: ");
scanf("%d", & number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Design and test a C program to test various inbuilt string functions.
#include < stdio.h>
#include < string.h>
int main() {
char str1[100], str2[100];
int length;
// 1. strlen() - Find the length of the string
length = strlen(str1);
printf("Length of first string: %d\n", length);
// 2. strcpy() - Copy string
strcpy(str2, str1);
printf("Second string after copying from first: %s\n", str2);
// 3. strcat() - Concatenate strings
strcat(str1, str2);
printf("First string after concatenation with second: %s\n", str1);
// 4. strcmp() - Compare two strings
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result < 0) {
printf("First string is less than second string.\n");
} else {
printf("First string is greater than second string.\n");
}
// 5. strchr() - Find first occurrence of a character in a string
char *pos = strchr(str1, 'a');
if (pos != NULL) {
printf("First occurrence of 'a' in first string: %s\n", pos);
} else {
printf("Character 'a' not found in first string.\n");
}
// 6. strrchr() - Find last occurrence of a character in a string
pos = strrchr(str1, 'a');
if (pos != NULL) {
printf("Last occurrence of 'a' in first string: %s\n", pos);
} else {
printf("Character 'a' not found in first string.\n");
}
// 7. strstr() - Find substring in a string
char *sub = strstr(str1, "test");
if (sub != NULL) {
printf("Substring 'test' found: %s\n", sub);
} else {
printf("Substring 'test' not found.\n");
}
// 8. strtok() - Tokenize a string (split by spaces)
printf("Tokenizing first string:\n");
char tempStr[100];
strcpy(tempStr, str1); // Copy string because strtok modifies the original string
char *token = strtok(tempStr, " ");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ");
}
// 9. strrev() - Reverse a string
// Note: strrev is a non-standard function, but is available in some compilers like Turbo C or GCC
#ifdef _MSC_VER
// For Microsoft compilers (e.g., Turbo C or older MS compilers), strrev can be used
strrev(str1);
printf("Reversed first string: %s\n", str1);
#endif
return 0;
}
Design and test a C program to demonstrate various inbuilt math functions.
#include < stdio.h>
#include < math.h>
int main() {
double num1, num2, result;
// Input numbers
printf("Enter the first number: ");
scanf("%lf", & num1);
printf("Enter the second number: ");
scanf("%lf", & num2);
// 1. sqrt() - Square root
result = sqrt(num1);
printf("Square root of %.2f: %.2f\n", num1, result);
// 2. pow() - Power
result = pow(num1, num2);
printf("%.2f raised to the power %.2f: %.2f\n", num1, num2, result);
// 3. fabs() - Absolute value
result = fabs(num1);
printf("Absolute value of %.2f: %.2f\n", num1, result);
// 4. ceil() - Round up to the nearest integer
result = ceil(num1);
printf("Ceiling value of %.2f: %.2f\n", num1, result);
// 5. floor() - Round down to the nearest integer
result = floor(num1);
printf("Floor value of %.2f: %.2f\n", num1, result);
// 6. fmod() - Remainder of division
result = fmod(num1, num2);
printf("Remainder of %.2f divided by %.2f: %.2f\n", num1, num2, result);
// 7. sin() - Sine of an angle (in radians)
result = sin(num1);
printf("Sine of %.2f: %.2f\n", num1, result);
// 8. cos() - Cosine of an angle (in radians)
result = cos(num1);
printf("Cosine of %.2f: %.2f\n", num1, result);
// 9. tan() - Tangent of an angle (in radians)
result = tan(num1);
printf("Tangent of %.2f: %.2f\n", num1, result);
// 10. log() - Natural logarithm (base e)
result = log(num1);
printf("Natural logarithm of %.2f: %.2f\n", num1, result);
// 11. log10() - Logarithm base 10
result = log10(num1);
printf("Logarithm (base 10) of %.2f: %.2f\n", num1, result);
// 12. exp() - Exponential function (e^x)
result = exp(num1);
printf("Exponential of %.2f (e^x): %.2f\n", num1, result);
// 13. tanh() - Hyperbolic tangent
result = tanh(num1);
printf("Hyperbolic tangent of %.2f: %.2f\n", num1, result);
return 0;
}
Design and test a C program to demonstrate storage classes.
#include < stdio.h>
// Global variable with 'static' storage class
static int staticVar = 10;
// 'auto' storage class (default for local variables)
void demoAuto() {
auto int autoVar = 20; // 'auto' is the default for local variables
printf("Auto variable: %d\n", autoVar);
}
// 'register' storage class (used for variables stored in CPU registers for faster access)
void demoRegister() {
register int regVar = 30; // 'register' suggests storing the variable in CPU register
printf("Register variable: %d\n", regVar);
}
// Function with 'extern' storage class to access global variable from another file (or section of code)
extern int externVar;
void demoExtern() {
externVar = 50; // Modifying the global variable declared outside this function
printf("Extern variable: %d\n", externVar);
}
int externVar = 40; // Definition of the extern variable
int main() {
// Demonstrating different storage classes
int localVar = 5; // 'auto' by default for local variables
// Display local variable
printf("Local variable (auto): %d\n", localVar);
// Static variable retains its value between function calls
printf("Static variable before function call: %d\n", staticVar);
staticVar++;
demoAuto();
Design and test a C program to demonstrate usage of enum and typeset.
#include < stdio.h>
// Enum definition for days of the week
enum Day {
Sunday, // 0
Monday, // 1
Tuesday, // 2
Wednesday, // 3
Thursday, // 4
Friday, // 5
Saturday // 6
};
// Typedef to create a new name for the enum type
typedef enum Day DayOfWeek;
// Function to print the name of the day based on the enum value
void printDay(DayOfWeek day) {
switch (day) {
case Sunday: printf("Sunday\n"); break;
case Monday: printf("Monday\n"); break;
case Tuesday: printf("Tuesday\n"); break;
case Wednesday: printf("Wednesday\n"); break;
case Thursday: printf("Thursday\n"); break;
case Friday: printf("Friday\n"); break;
case Saturday: printf("Saturday\n"); break;
default: printf("Invalid day\n");
}
}
int main() {
// Declare and initialize enum variables using the typedef type
DayOfWeek today = Wednesday;
DayOfWeek tomorrow = Friday;
// Print the days
printf("Today is: ");
printDay(today);
printf("Tomorrow will be: ");
printDay(tomorrow);
return 0;
}
Design and test at least C programs on structures and unions.
C Program Using Structures:
#include < stdio.h>
// Define a structure to store information about a student
struct Student {
char name[50];
int roll_no;
float marks;
};
int main() {
// Declare and initialize a structure variable
struct Student student1;
// Input data for student1
printf("Enter name of the student: ");
fgets(student1.name, sizeof(student1.name), stdin);
student1.name[strcspn(student1.name, "\n")] = 0; // Remove the trailing newline
printf("Enter roll number: ");
scanf("%d", & student1.roll_no);
// Display the data
printf("\nStudent Information:\n");
printf("Name: %s\n", student1.name);
printf("Roll Number: %d\n", student1.roll_no);
printf("Marks: %.2f\n", student1.marks);
return 0;
}
C Program Using Unions:
#include < stdio.h>
// Define a union to store data of different types
union Data {
int i;
float f;
char str[20];
};
int main() {
// Declare a union variable
union Data data;
// Input integer value
data.i = 10;
printf("Data as Integer: %d\n", data.i);
// Input float value
data.f = 220.5;
printf("Data as Float: %.2f\n", data.f);
// Input string value
strcpy(data.str, "Hello, Union!");
printf("Data as String: %s\n", data.str);
// Display all values (the union will store the last written value)
printf("\nAfter writing all values, the last value written is stored:\n");
printf("Integer: %d\n", data.i); // Will show garbage value
printf("Float: %.2f\n", data.f); // Will show the float value (last written)
printf("String: %s\n", data.str); // Will show the string value (last written)
return 0;
}
Design and test at least C programs using file operations.
C Program to Write and Read from a File:
#include < stdio.h>
// Writing to a file
file = fopen(filename, "w"); // Open file in write mode
if (file == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
printf("Enter data to write to the file: ");
fgets(data, sizeof(data), stdin); // Read data from user
fprintf(file, "%s", data); // Write data to file
fclose(file); // Close the file after writing
// Reading from a file
file = fopen(filename, "r"); // Open file in read mode
if (file == NULL) {
printf("Error opening file for reading.\n");
return 1;
}
printf("\nData read from the file:\n");
while (fgets(data, sizeof(data), file)) { // Read line by line
printf("%s", data); // Print the data read from the file
}
fclose(file); // Close the file after reading
return 0;
}
C Program to Append Data to a File:
#include < stdio.h>
// Open file in append mode
file = fopen(filename, "a");
if (file == NULL) {
printf("Error opening file for appending.\n");
return 1;
}
printf("Enter data to append to the file: ");
fgets(data, sizeof(data), stdin); // Read data from user
fprintf(file, "%s", data); // Append data to file
fclose(file); // Close the file after appending
printf("Data has been appended to the file successfully.\n");
Comments
Post a Comment