0% found this document useful (0 votes)
30 views

PSC LAB Programs 4-15 Print

Uploaded by

kripalaalal4
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)
30 views

PSC LAB Programs 4-15 Print

Uploaded by

kripalaalal4
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/ 35

Task 4: Simple computational problems using arithmetic

expressions
PROGRAM:
#include <stdio.h>
int main()
{
float u,a,t,v,s;
printf("Enter the value of u:");
scanf("%f", &u);
printf("Enter the value of a:");
scanf("%f", &a);
printf("Enter the value of t:");
scanf("%f", &t);
v=u+a*t;
s=u*t + 0.5* a*t*t;
printf("Velocity is %f", v);
printf("Displacement is %f", s);
return 0;
}

OUTPUT:
TASK 5 : Problems based on switch case control structure
PROGRAM:
#include<stdio.h>
int main()
{
char op;
int a,b,c;
printf("Enter an operator(+,-,*,/)");
scanf("%c",&op);
printf("Enter two values:");
scanf("%d%d",&a,&b);
switch(op)
{
case '+':
c=a+b;
printf("Result=%d",c);
break;
case '-':
c=a-b;
printf("Result=%d",c);
break;
case '*':
c=a*b;
printf("Result=%d",c);
break;
case '/':
c=a/b;
printf("Result=%d",c);
break;
default:
printf("operator is not correct");
}
return 0;
}

OUTPUT:
Task 6: Iterative problems such as sum of series, count and sum
of digits
PROGRAM:
#include <stdio.h>
int main()
{
int number, total = 0,count = 0, sum_digits = 0, digit;
// Input the value of n
printf("Enter a number: ");
scanf("%d", &number);

// Calculate the sum of the series


for (int i = 1; i <= number; i++)
{
total += i;
}
int temp = number;

// Calculate the sum of digits and count the digits


while (temp > 0)
{
digit = temp % 10; // Get the last digit
sum_digits += digit; // Add the digit to the sum
count++; // Increment the count
temp = temp / 10; // Remove the last digit
}

// Output the result


printf("The sum of the first %d natural numbers is: %d\n", number, total);
printf("The number of digits is: %d\n", count);
printf("The sum of the digits is: %d\n", sum_digits);
return 0;
}

OUTPUT:
Enter a number: 15
The sum of the first 15 natural numbers is: 120
The number of digits is: 2
The sum of the digits is: 6
Task 7(a): Design and develop a C program to perform Linear
search using one-dimensional arrays
PROGRAM:
#include <stdio.h>
void main()
{
int arr[20],i,n,search,flag=0;
printf("Enter total no of elements: ");
scanf("%d",&n);
printf("Enter the elements one by one: ");
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&search);
for(i=1;i<=n;i++)
{
if(arr[i]==search)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\n The element is present at index: %d", i);
}
else
{
printf("Element not found!!!");
}
}

OUTPUT:
Task 7(b): Design and develop a C program to perform bubble
sort using one-dimensional arrays
PROGRAM:
#include <stdio.h>
int main()
{
int arr[100]; // Assuming a maximum size of 100 for the array
int n, i, j, temp;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Array before sorting:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
// Bubble sort implementation
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Array after sorting:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

OUTPUT:
Enter the number of elements in the array: 5
Enter the elements of the array:
20
90
100
10
5
Array before sorting:
20 90 100 10 5
Array after sorting:
5 10 20 90 100
Task 8(a): Design and develop a C program to perform matrix
addition using multi-dimensional arrays
PROGRAM:
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],sum[5][5],i,j,n;
printf("\n\n Enter the order of square matrix:\n");
scanf("%d",&n);
printf("\nEnter the values of 1st matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the values of 2nd matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
printf("The Addition of Two matrix is\n ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d\t",sum[i][j]);
}
printf("\n");
}
}

OUTPUT:
Task 8(b): Design and develop a C program to perform matrix
subtraction using multi-dimensional arrays
PROGRAM :
#include<stdio.h>
int main()
{
int a[5][5],b[5][5],sub[5][5],i,j,n;
printf("\n\n Enter the order of square matrix:\n");
scanf("%d",&n);
printf("\nEnter the values of 1st matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the values of 2nd matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
sub[i][j]=a[i][j]-b[i][j];
}
}
printf("The Subtraction of Two matrix is\n ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d\t",sub[i][j]);
}
printf("\n");
}
}

OUTPUT:
Task 9: Design and develop a C program to generate Fibonacci
series using recursion function
PROGRAM:

#include<stdio.h>
int main()
{
int n;
void fibonacci(int);
printf("Enter the number of terms:");
scanf("%d", &n);
printf("Fibonacci series:");
printf(" %d %d", 0,1);
fibonacci(n-2);
return 0;
}

void fibonacci(int n)
{
static int t1=0, t2=1, nextTerm;
if(n>0)
{
nextTerm=t1+t2;
printf(" %d", nextTerm);
t1=t2;
t2=nextTerm;
fibonacci(n-1);
}
}
OUTPUT:
Enter the number of terms: 8
Fibonacci series: 0 1 1 2 3 5 8 13
Task 10: Problem using structures
PROGRAM:
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
}t;
void main()
{
printf("\n Enter Hour : ");
scanf("%d",&t.hour);
printf("\n Enter Minute: ");
scanf("%d",&t.minute);
printf("\n Enter Second : ");
scanf("%d",&t.second);
printf("\n Time %d:%d:%d",t.hour%24,t.minute%60,t.second%60);
}

OUTPUT:
Enter Hour: 8
Enter Minute: 50
Enter Second: 32
Time 8:50:32
Task 11: Problems using arrays of structures
PROGRAM:
#include <stdio.h>
// Define a structure to store student information
struct student
{
char name[50];
int id;
float GPA;
};

int main()
{
// Define an array of structures to hold 3 students
struct student s[30];
int n;
printf(" Enter the number of students: ");
scanf("%d",&n);

// Input data for each student


for (int i = 0; i < n; i++)
{
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", s[i].name);
printf("id: ");
scanf("%d", &s[i].id);
printf("Grade Point Average: ");
scanf("%f", &s[i].GPA);
printf("\n");
}

// Display the details of each student


printf("Student Information:\n");
printf("Name \t id \t GPA\n");
for (int i = 0; i < n; i++)
{
printf("%s\t%d\t%0.2f\n", s[i].name,s[i].id,s[i].GPA);
}
}

OUTPUT:
Enter the number of students: 3
Enter details for student 1:
Name: Alice
id: 101
Grade Point Average: 8.5

Enter details for student 2:


Name: Bob
id: 102
Grade Point Average: 7.8

Enter details for student 3:


Name: Charlie
id: 103
Grade Point Average: 8
Student Information:
Name id GPA
Alice 101 8.50
Bob 102 7.80
Charlie 103 8.00
TASK 12(a): C PROGRAM TO ILLUSTRATE THE USE OF PONTERS IN
ARITHMETIC OPERATIONS
PROGRAM:
#include<stdio.h>
void main()
{
int a, b,*p1,*p2, x, y, z;
printf(“Enter the values of a,b:”);
scanf(“%d%d”, &a,&b);
p1=&a;
p2=&b;
x= *p1 * *p2 – 6;
y= 4* (-*p2) / (*p1) + 10;
printf(“address of a= %u\n”, p1);
printf(“ address of b = %u\n”, p2);
printf(“\n”);
printf(“a=%d, b=%d\n”, a, b);
printf(“x=%d, y=%d\n”, x, y);
*p2 = *p2 + 3;
*p1 = *p2 – 5;
z = *p1 * *p2 – 6;
printf(“\n a=%d, b=%d”, a,b);
printf(“ z=%d\n”, z);
}
OUTPUT:
TASK 12(b): DEVELOP A C PROGRAM TO PERFORM CALL BY
REFERENCE USING POINTERS
PROGRAM:
#include <stdio.h>
int main()
{
int a,b;
void swap(int *, int *);
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
printf("Numbers Before Swapping: a=%d and b=%d\n", a, b);
swap(&a,&b);
printf("Numbers After Swapping: a=%d and b=%d", a, b);
return 0;
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}

OUTPUT:
TASK 13: FILE OPERATIONS USING TEXT FILE
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void createFile()
{
FILE *file = fopen("line.txt", "w");
if (file == NULL)
{
puts("Error creating file");
exit(EXIT_FAILURE);
}
// Sample text to write to the file
fprintf(file, "Hello, World!\nThis is a sample text file.\nIt contains multiple
lines,\nwords,
and characters.\n");
fclose(file);
}

void countFileContents()
{
FILE *file = fopen("line.txt", "r");
if (file == NULL)
{
puts("Error opening file");
exit(EXIT_FAILURE);
}
int ch, words = 0, lines = 0, characters = 0;
int inWord = 0;

while ((ch = fgetc(file)) != EOF)


{
characters++;
if (ch == '\n')
{
lines++;
}
if (isspace(ch))
{
inWord = 0;
} else
{
if (inWord == 0)
{
words++;
inWord = 1;
}
}
}

// If the last line doesn't end with a newline


if (characters > 0 && ch == EOF)
{
lines++;
}
fclose(file);

// Display results
printf("Number of words: %d\n", words);
printf("Number of characters: %d\n", characters);
printf("Number of lines: %d\n", lines);
}
int main()
{
createFile();
countFileContents();
return 0;
}
INPUT FILE: (line.txt)

OUTPUT:
TASK 14: FILE OPERATIONS USING BINARY FILE
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct product
{
int ID;
char name[30];
char address[10];
}precord;
int main()
{
int i,n;
FILE *fptr;
int found=0, roll;
/*open binary file in Write mode*/ fptr=fopen("line.dat","wb");
if(fptr==NULL)
{
printf("File could not open");
exit(0);
}
printf("enter no of records");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
/*Read a Product details from user through Keyboard*/
printf("Enter Product ID:");
scanf("%d", &precord.ID);
printf("Enter Product name:");
scanf("%s", precord.name);
printf("Enter Product Address: ");
scanf("%s", precord.address);
//writing Product details to binary file
fwrite(&precord,sizeof(precord),1,fptr);
printf("\nRecord has been added successfully added.");
}
fclose(fptr);
fptr=fopen("line.dat","rb");
printf("\n \n Enter ID of Product to Search Record=");
scanf("%d",&roll);
while((fread(&precord,sizeof(precord),1,fptr)==1))
{
if(precord.ID==roll)
{
found=1;
printf("\nRecord of Product With ID %d is as follows:",roll);
printf("\n:");
printf("\nRoll No: %d", precord.ID); printf("\nName:%s", precord.name);
printf("\nAddress:%s", precord.address);
break;
}
}
if(found==0)
printf("\n Record Not found in this file!");
fclose(fptr);
return 0;
}
OUTPUT:
TASK 15: SIMPLE GRAPHICS PROGRAMS

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
float sx,sy;
void menu()
{
printf("menu\n");
printf("1.Translation\n");
printf("2. Rotation\n");
printf("3. Scaling\n");
printf("4.exit\n");
printf("enter the choice:");
scanf("%d",&option);
switch(option)
{
case 1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4:
exit(0);
break;
}
}
void input()
{
printf("enter the number of vertices:" );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the coordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}

void output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void translation()
{
output();
printf("enter the tranformation vertex tx,ty:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
output();
delay(10);
menu();
}
void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}
void scaling()
{
output();
printf("enter the scaling factor\n");
scanf("%f%f",&sx,&sy);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
delay(10);
menu();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
menu();
getch();
}
OUTPUT:
Menu

1. Translation
2. Rotation
3. Scaling
4. Exit

Enter the choice : 1


Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150

Enter the translation vector Tx, Ty : 90 60

Enter the choice : 2


Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150
Enter the Rotating Angle : 90
Enter the Pivot Point : 100 200

Enter the choice: 3


Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
Enter the coordinates : 60 200 30 150
Enter the scaling Factor : 0.3 0.4
Enter the Fixed Point : 100 200

You might also like