PSC LAB Programs 4-15 Print
PSC LAB Programs 4-15 Print
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);
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);
OUTPUT:
Enter the number of students: 3
Enter details for student 1:
Name: Alice
id: 101
Grade Point Average: 8.5
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;
// 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