0% found this document useful (0 votes)
11 views15 pages

PPS Practicals

The document contains a series of C programming experiments demonstrating various concepts such as variable declaration, arithmetic operations, conditional expressions, assignment operators, control statements (if-else, switch-case, loops), and array manipulation. Each experiment includes code snippets along with their expected outputs, showcasing practical applications of these programming concepts. The document serves as a comprehensive guide for learning fundamental C programming techniques.

Uploaded by

kml.stbcet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views15 pages

PPS Practicals

The document contains a series of C programming experiments demonstrating various concepts such as variable declaration, arithmetic operations, conditional expressions, assignment operators, control statements (if-else, switch-case, loops), and array manipulation. Each experiment includes code snippets along with their expected outputs, showcasing practical applications of these programming concepts. The document serves as a comprehensive guide for learning fundamental C programming techniques.

Uploaded by

kml.stbcet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Experiment no.

C program that demonstrates the declaration and initialization of variables


with different data types:
#include <stdio.h>
int main() {
int age = 30; // Integer data type
float salary = 60000.50; // Floating-point data type
double pi = 3.14159265359; // floating-point data type
char initial = 'J'; // Character data type
char name[] = "John Doe"; // String data type (array of characters)
int isEmployed = 1; // Boolean data type (represented by 1 for true
and 0 for false)

// Printing the values of the variables


printf("Age: %d\n", age);
printf("Salary: %f\n", salary);
printf("Pi: %lf\n", pi);
printf("Initial: %c\n", initial);
printf("Name: %s\n", name);
printf("Is Employed: %d\n", isEmployed);

return 0;
}
Output:
Age: 30
Salary: 60000.500000
Pi: 3.141593
Initial: J
Name: John Doe
Is Employed: 1

#include <stdio.h>
void main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
}
Output:

Size of int = 4 bytes


Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Experiment no. 2
Implement arithmetic relational and logical operations in c programs and display the results

#include <stdio.h>
int main()
{
int a = 9,b = 4, c, result;

c = a+b; // Working of arithmetic operators


printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);

// Working of relational operators


printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

// Working of logical operators


result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);
return 0;
}

Output:

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b = 1
9 == 4 is 0
9 == 1 is 0
9 > 4 is 1
9 > 1 is 1
9 < 4 is 0
9 < 1 is 0
9 != 4 is 1
9 != 1 is 1
9 >= 4 is 1
9 >= 1 is 1
9 <= 4 is 0
9 <= 1 is 0
(a == b) && (c > b) is 0
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 1
!(a != b) is 0
!(a == b) is 1
Experiment no. 3
c program to demonstrate the use of conditional expression and explain the order of evaluation

#include <stdio.h>

void main() {
int a = 10;
int b = 20;
int max;

// Conditional expression to find the maximum of a and b


max = (a > b) ? a : b;

printf("The maximum value is: %d\n", max);

// Demonstrating order of evaluation with side effects


int x = 5;
int y = 10;
int result;

result = (x++ < y) ? x : y++;

printf("Result: %d, x: %d, y: %d\n", result, x, y);

Output:
The maximum value is: 20
Result: 6, x: 6, y: 10

Explanation:
 The condition (x++ < y) is evaluated. Because x is 5 and y is 10, the condition is
true. x is incremented to 6 as a side effect of the post-increment operator ++.
 Since the condition is true, the expression x (which is now 6) is evaluated, and its value
is assigned to result. y++ is not evaluated.
 The output shows result is 6, x is 6, and y remains 10, demonstrating that y++ was not
executed.
Experiment no. 4
c program to demonstrate the use of assignment operator and evaluate expressions
involving them

#include <stdio.h>

void main() {
int a = 10;
int b = 5;
int c;

c = a;
printf("c = a: c = %d\n", c);

// Add and assign


a += b; // a = a + b
printf("a += b: a = %d\n", a);

a -= b; // a = a - b
printf("a -= b: a = %d\n", a);

a %= b; // a = a % b
printf("a %%= b: a = %d\n", a);

// Bitwise AND and assign


a &= b; // a = a & b
printf("a &= b: a = %d\n", a);

a |= b; // a = a | b
printf("a |= b: a = %d\n", a);

// Bitwise XOR and assign


a ^= b; // a = a ^ b
printf("a ^= b: a = %d\n", a);

a <<= 1; // a = a << 1
printf("a <<= 1: a = %d\n", a);

a >>= 1; // a = a >> 1
printf("a >>= 1: a = %d\n", a);
}
Output:
c = a: c = 10
a += b: a = 15
a -= b: a = 10
a %= b: a = 0
a &= b: a = 0
a |= b: a = 5
a ^= b: a = 0
a <<= 1: a = 0
a >>= 1: a = 0
Experiment no. 5
Program to demonstrate if-else statement
#include <stdio.h>
void main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);

if (number > 0) {
printf("%d is a positive number.\n", number);
} else if (number < 0){
printf("%d is a negative number.\n", number);
} else {
printf("The number is zero.\n");
}
}
Enter an integer: 6
6 is a positive number.

Program to demonstrate switch-case statement

// Program to create a simple calculator


#include <stdio.h>
void main() {
char operation;
double n1, n2;

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


scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);
switch(operation)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);
break;

// operator doesn't match any case constant +, -, *, /


default:
printf("Error! operator is not correct");
}
}
Output:
Enter an operator (+, -, *, /): -
Enter two operands: 67 34
67.0 - 34.0 = 33.0

Program to demonstrate while loop


#include <stdio.h>
void main() {
int i = 1;

// The loop continues as long as i is less than or equal to 5


while (i <= 5) {
printf("Value of i: %d\n", i);
i++; // Increment i to avoid an infinite loop
}

printf("Loop finished.\n");
}
Output:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Loop finished.

Program to demonstrate do while loop


// Program to add numbers until the user enters zero
#include <stdio.h>
int main() {
double number, sum = 0;

// the body of the loop is executed at least once


do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);
}
Output:

Enter a number: 2
Enter a number: 5.3
Enter a number: 9
Enter a number: -84
Enter a number: 0
Sum = -67.70
Program to demonstrate for loop

#include <stdio.h>

int main() {
int i;

// This for loop will iterate 10 times


// The loop variable i is initialized to 0
// The loop continues as long as i is less than 10
// After each iteration, i is incremented by 1
for (i = 0; i < 10; i++) {
printf("Iteration number: %d\n", i + 1);
}

return 0;
}

Output:
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5
Iteration number: 6
Iteration number: 7
Iteration number: 8
Iteration number: 9
Iteration number: 10
Experiment no. 6
c program to find factorial of a number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact =fact * i;
}
printf("Factorial of %d = %llu", n, fact);
}
}

Output:

Enter an integer: 5
Factorial of 5 = 120
Experiment no. 7
c program to print fibonacci series
#include <stdio.h>
void main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
}

Output:
Enter the number of terms: 9
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21
Experiment no. 8
Write a c program to initialize and display elements of a one-dimensional array

#include <stdio.h>

void main() {
// Initialize a one-dimensional integer array
int numbers[] = {10, 20, 30, 40, 50};

// Calculate the size of the array


int array_size = sizeof(numbers) / sizeof(numbers[0]);

printf("Elements of the array are:\n");


// Iterate through the array and print each element
for (int i = 0; i < array_size; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
}

Output:
Elements of the array are:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Experiment no. 9
Write a c program to demonstrate the use of pointers to access elements of array

#include <stdio.h>

void main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr;
int i;

ptr = arr; // ptr now points to the first element of arr

printf("Array elements accessed using pointers:\n");


for (i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i));
}

printf("\nArray elements accessed using pointer arithmetic:\n");


for (i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *ptr);
ptr++; // Move the pointer to the next element
}

Array elements accessed using pointers:


Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50

Array elements accessed using pointer arithmetic:


Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
Experiment no. 10
Write a c program to demonstrate the use of pointers to access elements of array
#include<stdio.h>
void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int num1,num2;

printf("Enter value of num1: ");


scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);

//displaying numbers before swapping


printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

//calling the user defined function swap()


swap(&num1,&num2);

//displaying numbers after swapping


printf("After Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

return 0;
}
Output:

Enter value of num1: 7


Enter value of num2: 9
Before Swapping: num1 is: 7, num2 is: 9
After Swapping: num1 is: 9, num2 is: 7
Experiment no 11
Structure Program for Student Details in C
#include <stdio.h>
#include <string.h> // For string operations

struct Student {
char name[50]; // Student's name (maximum 49 characters + null
terminator)
int rollNumber; // Roll number
float marks; // Marks (e.g., total marks, GPA)
int age; // Age
char address[100]; // Address
};
void main() {
struct Student student1; // Create a variable of type Student

printf("Enter student name: ");


scanf("%s", student1.name); // Use %s for reading strings

printf("Enter roll number: ");


scanf("%d", &student1.rollNumber);

printf("Enter marks: ");


scanf("%f", &student1.marks);

printf("Enter age: ");


scanf("%d", &student1.age);

printf("Enter address: ");


scanf(" %[^\n]", student1.address);
// Display the student details
printf("\nStudent Details:\n");
printf("Name: %s\n", student1.name);
printf("Roll Number: %d\n", student1.rollNumber);
printf("Marks: %.2f\n", student1.marks);
printf("Age: %d\n", student1.age);
printf("Address: %s\n", student1.address);

}
Enter student name: abc
Enter roll number: 11
Enter marks: 95.63
Enter age: 17
Enter address: zyz

Student Details:
Name: abc
Roll Number: 11
Marks: 95.63
Age: 17
Address: zyz
Experiment no 12
Program to read data from text file and write the results to another file

#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *inputFile, *outputFile;
char ch;
char inputFileName[100], outputFileName[100];

printf("Enter the name of the input file: ");


scanf("%s", inputFileName);
printf("Enter the name of the output file: ");
scanf("%s", outputFileName);

inputFile = fopen(inputFileName, "r");


if (inputFile == NULL) {
perror("Error opening input file");
return 1;
}

outputFile = fopen(outputFileName, "w");


if (outputFile == NULL) {
perror("Error opening output file");
fclose(inputFile);
return 1;
}

while ((ch = fgetc(inputFile)) != EOF) {


fputc(ch, outputFile);
}

printf("File copied successfully.\n");

fclose(inputFile);
fclose(outputFile);
}

Output:
Enter the name of the input file: abc
Enter the name of the output file: xyz
Error opening input file: No such file or directory

You might also like