Naveen

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

PRACTICAL FILE

OF
PPS LAB PROGRAMS

SUBMITTED BY :- SUBMITTED
TO :-
Naveen Dhiman MR. SUMIT
DAHIYA
24001003081
ECE-B 1st year
INDEX
S.N program date SIGnature
o.
1 WAP to find the largest of three
numbers. (if-else-then)

2 WAP to find the largest number of ten


numbers. (for statement)

3 WAP to find roots of quadratic


equation using functions and switch
statements.

4 WAP using arrays to find the largest


and second largest no. out of given 50
no.’s.

5 WAP to multiply two matrices.

6 WAP to find the factorial of a given


number using recursion.

7 WAP to find the Fibonacci series using


recursion.

8 WAP to convert a given decimal


number into binary number.

9 WAP to find the sum of digits of a


number.

10 WAP to check that the input


string/number is a palindrome or not.

11 WAP to concatenate two strings.

12 Wap which manipulates


structures(write, read and update
records).

13 WAP to illustrate the use of


enumerators.

14 WAP to demonstrate the use of


Unions.

15 WAP which creates a file and writes


into it supplied input.

16 WAP which manipulates structures


into files(write, read and update
records).

Program No. 1
Aim- Write a program to find the largest of three numbers.
Source Code:-
#include<stdio.h>
int main()
{
int a,b,c;
printf(“Enter three numbers:”);
scanf("%d%d%d",&a,&b,&c);
if (a>=b && a>=c) {
printf("%d is GREATEST",a);
}
else if (b>=c && b>=a) {
printf("%d is GREATEST",b);
}
else {
printf("%d is GREATEST",c);
}
return 0;
}
Output-

Program No. 2
Aim- Write a program to find the largest number out of ten
numbers. (for statement)
Source Code-
#include <stdio.h>
int main() {
int a[10];
int i;
int greatest;
printf("Enter ten values:");
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
}}
printf("Greatest of ten numbers is %d", greatest);
return 0;
}
Output-

Program No. 3
Aim- Write a program to find roots of quadratic equation using
functions and switch statements.
Source Code-
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary, discriminant;
printf("Enter value of 'a': ");
scanf("%f", &a);
printf("Enter value of 'b': ");
scanf("%f",&b);
printf("Enter value of 'c': ");
scanf("%f",&c);
discriminant = (b * b) - (4 * a * c);
switch(discriminant > 0)
{
case 1:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are : %.2f and %.2f",
root1, root2);
break;
case 0:
switch(discriminant < 0)
{
case 1:
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Imaginary Roots Exist");
break;
case 0:
root1 = root2 = -b / (2 * a);
printf("Roots are : %.2f and %.2f", root1, root2);
break;
}
}
return 0;
}

Output-
Program No. 4
Aim- Write a program using arrays to find largest and second
largest no. out of given 50 numbers.
Source Code-
#include <stdio.h>

int main() {
int a[50], n;
int largest1, largest2, i;

printf("enter number of elements you want in array");


scanf("%d", &n);
printf("enter elements");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
largest1 = a[0];
for (i = 0; i < n; i++) {
if (a[i] > largest1) {
largest1 = a[i];
}
}
largest2 = a[0];
for (i = 1; i < n; i++) {
if (a[i] > largest2 && a[i] < largest1)
largest2 = a[i];
}
printf("First and second largest number is %d and %d ",
largest1, largest2);
}

Output-
Program No. 5
Aim- Write a program to multiply two matrices.
Source Code-
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10][10],b[10][10], mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&a[i][j]);
}}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&b[i][j]);
}}
printf("multiply 1 and 2 matrix=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
mul[i][j]=0;
for(k=0;k<c;k++) {
mul[i][j]+=a[i][k]*b[k][j];
}}}
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
Output –
Program No. 6
Aim- Write a program to find factorial of a number.
Source Code-
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

Output-

Program No. 7
Aim- Write a program to find the Fibonacci series using
recursion.
Source Code-
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
int main() {
int n, i;
printf("Enter the number of element you want in series :\n");
scanf("%d",&n);
printf("fibonacci series is: \n");
for(i=0;i<n;i++) {
printf("%d ", fibonacci(i));
}
getch();
}
int fibonacci(int i) {
if(i==0) return 0;
else if(i==1) return 1;
else
return (fibonacci(i-1)+fibonacci(i-2));
}

Output-
Program No. 8
Aim- Write a program to convert a given decimal number into
binary number.
Source Code-
#include<stdio.h>
int main()
{
long int decimalNumber,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0)
{
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("\nBinary number of %ld is: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
{
printf("%d",binaryNumber[j]);
}
return 0;
}

Output-
Program No. 9
Aim- Write a program to find the sum of digits of a number.
Source Code-
#include<stdio.h>
void main() {
int n, sum = 0, r;
printf("Enter Any Number:");
scanf("%d", &n);
while (n>0) {
r = n % 10;
sum = sum + r;
n = n/10;
}
printf("Sum Of Digits is:%d", sum);
}

Output-
Program No. 10
Aim:- Write a program to check that the input string/number is
a palindrome or not.
Source Code-
#include<stdio.h>
int main() {
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0) {
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
return 0;
}

Output-
Program No.11
Aim- Write a program to concatenate two strings.
Source Code-
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Programming ";
const char str2[] = "Is Awesome";
strcat(str1, str2);
printf("%s", str1);
return 0;
}

Output-

Program No.12
Aim- Write a program which manipulates structures ( write,
read and update records ).
Source Code-
#include <stdio.h>
#include <string.h>
#define MAX 100
struct Student {
long long id;
char name[50];
float marks;
};
void writeRecord(struct Student students[], int *count);
void readRecords(struct Student students[], int count);
void updateRecord(struct Student students[], int count);
int main() {
struct Student students[MAX];
int count = 0;
int choice;
do {
printf("\n--- Student Records Menu ---\n");
printf("1. Write Record\n");
printf("2. Read Record\n");
printf("3. Update Record\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
writeRecord(students, &count);
break;
case 2:
readRecords(students, count);
break;
case 3:
updateRecord(students, count);
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
void writeRecord(struct Student students[], int *count) {
if (*count >= MAX) {
printf("Cannot add more records. Maximum limit reached.\n");
return;
}
printf("Enter ID: ");
scanf("%d", &students[*count].id);
printf("Enter Name: ");
scanf(" %[^\n]", students[*count].name);
printf("Enter Marks: ");
scanf("%f", &students[*count].marks);
(*count)++;
printf("Record added successfully.\n");
}
void readRecords(struct Student students[], int count) {
if (count == 0) {
printf("No records to display.\n");
return;
}
printf("\n--- Student Records ---\n");
for (int i = 0; i < count; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n", students[i].id, students[i].name, students[i].marks);
}
}
void updateRecord(struct Student students[], int count) {
if (count == 0) {
printf("No records to update.\n");
return;
}
int id, found = 0;
printf("Enter ID to update: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
found = 1;
printf("Enter new Name: ");
scanf(" %[^\n]", students[i].name);
printf("Enter new Marks: ");
scanf("%f", &students[i].marks);
printf("Record updated successfully.\n");
break;
}
}
if (!found) {
printf("Record with ID %d not found.\n", id);
}
}
Output-
Program No.13
Aim- Write a program to illustrate the use of enumerators.
Source Code-
#include<stdio.h>
enum Day {
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
int main() {
int day;
printf("Enter day (1-7) = ");
scanf("%d", &day);
switch (day) {
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;
case SUNDAY:
printf("Sunday\n");
break;
default:
printf("Invalid day!\n");
}
return 0;
}
Output-

Program No. 14
Aim- Write a program to demonstrate the use of Unions.
Source Code-
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 12;
printf("data.i = %d\n", data.i);
data.f = 127;
printf("data.f = %.2f\n", data.f);
strcpy(data.str, "Hello, DCRUSTians!");
printf("data.str = %s\n", data.str);
printf("\nAfter overriding values:\n");
printf("data.i = %d\n", data.i); // The value of data.i is now garbage
printf("data.f = %.2f\n", data.f); // The value of data.f is now garbage
printf("data.str = %s\n", data.str); // The last assigned value
return 0;
}

Output-

Program No.15
Aim- Write a program which creates a file and writes into it
supplied input.
Source Code-
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char filename[100];
char input[1000];
printf("Enter the filename to create and write to: ");
scanf("%s", filename);
file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}
printf("Enter the text you want to write into the file:\n");
getchar();
fgets(input, sizeof(input), stdin);
fprintf(file, "%s", input);
fclose(file);
printf("Data has been written to the file '%s'.\n", filename);
return 0;
}

Output-

Output test file -


Program No. 16
Aim- Write a program which manipulates structures into files
( write, read, and update records).
Source Code-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 100
typedef struct {
int id;
char name[50];
int age;
} Record;
void updateRecord(const char *filename, int id, const char
*newName, int newAge) {
FILE *file = fopen(filename, "r+");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX_LINE_LENGTH];
long pos;
int found = 0;
while (fgets(line, sizeof(line), file) != NULL) {
pos = ftell(file);
Record record;
sscanf(line, "%d %s %d", &record.id, record.name,
&record.age);
if (record.id == id) {
found = 1;
fseek(file, pos - strlen(line), SEEK_SET);
fprintf(file, "%d %s %d\n", record.id, newName,
newAge);
break;
}
}
if (!found) {
printf("Record with ID %d not found.\n", id);
}

fclose(file);
}
int main() {
const char *filename = "records.txt";
int id;
char newName[50];
int newAge;
printf("Enter the ID of the record to update: ");
scanf("%d", &id);
printf("Enter the new name: ");
scanf("%s", newName);
printf("Enter the new age: ");
scanf("%d", &newAge);
updateRecord(filename, id, newName, newAge);
return 0;
}
Output-
Record file before executing the program:-

Executing the program:-

Record file after executing the program:-


1

11

You might also like