Lab 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

INDEX

S.NO CONTENT PAGE SIGNATURE


No.
1. Write a program to find divisor or
factorial of a number.
2. Write a program to find the sum of a
geometric series.
3. Write a recursive program for tower
of Hanoi problem.
4. Write a recursive program to find first
n Fibonacci numbers.
5. Write a menu driven program for
matrices to perform the following
operation depending on whether the
operation requires one or two
matrices.
6. Write a program in c to find grade of
a student using switch statement.
7. Write a program in c which records
of integers.Each element of the array
is replaced by its square.Print back
the array.
8. Write a program to perform the
following operations on a string
without using string functions.
9. Write a program to copy one file to
other, using command line
arguments.
10. An array of record contains
information of managers and
workers of a company. Print all the
data of managers and workers in
separate files.
OUTPUT.1
PROGRAM.1

#include <stdio.h>
int main()
{
int a;
printf("Press 1 to find divisors of a number:");
printf("\n");
printf("Press 2 to find factorial of a number:");
printf("\n");
printf("Enter your choice:");
scanf("%d",&a);
if(a<1&&a>2)
{
printf("Wrong choice entered:");
}
switch(a)
{
case 1:
int n;
printf("Enter the number whose divisor is to be found:");
scanf("%d",&n);
printf("Divisors of the given number are:");
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
printf(",%d",i);
}
}
break;

case 2:
int x;
int fact=1;
printf("Enter a number whose factorial is to be found:");
scanf("%d",&x);
for(int i=1;i<=x;i++)
{
fact=fact*i;
}
printf("Factoria of given number is : %d",fact);
break;

default:
printf("wrong choice");
break;
}
return 0;
}
OUTPUT.2
PROGRAM.2

#include <stdio.h>
int main()
{
float a,r;
int n;
float sum=0;
printf("Enter the number of terms in GP series:");
scanf("%d",&n);

printf("Enter the first term of the GP series:");


scanf("%f",&a);

printf("Enter the common ratio of GP series:");


scanf("%f",&r);

sum = (float)((a*(1-pow(r,n+1)))/(1-r));

printf("Sum of the given GP series is: %f",sum);

return 0;
}
OUTPUT.3
PROGRAM.3

#include <stdio.h>
int main()
{
int n;
printf("Enter the number of disk:");
scanf("%d",&n);
char start='L',inter='C',dest='R';
towerofhanoi(n,start,inter,dest);
}

void towerofhanoi(int n,char sndl, char indl, char dndl)


{
if(n!=0)
{
towerofhanoi(n-1,sndl,dndl,indl);
printf("\nMove disk %d from %c to %c\n",n,sndl,dndl);
towerofhanoi(n-1,indl,sndl,dndl);
}
return 0;
}
OUTPUT.4
PROGRAM.4

#include <stdio.h>
int fib(int n)
{
if (n==1||n==0)
{
return n;
}
else
{
return fib(n-1) + fib(n-2);
}
}
int main()
{
int x;
int term;
printf("Enter a limt:");
scanf("%d",&x);
printf("Fibonacci series is:0");
for(int i=1;i<x;i++)
printf(",%d",fib(i));
return 0; }
OUTPUT.5
PROGRAM.5

#include <stdio.h>
int main()
{
int m,n,a,i,j;
int mat1[10][10];
int mat2[10][10];
int mat3[10][10];
printf("Enter 1 for Addition of two matrices:");
printf("\n");
printf("Enter 2 for Subtraction of two matrices:");
printf("\n");
printf("Enter 3 for finding upper and lower triangular matrix:");
printf("\n");
printf("Enter 4 for transpose of matrix:");
printf("\n");
printf("Enter 5 for Product of two matrices:");
printf("\n");
printf("Enter the number of rows:");
scanf("%d",&m);
printf("Enter the number of columns:");
scanf("%d",&n);
printf("Enter your choice:");
scanf("%d",&a);

switch(a)
{
case 1:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements of 2nd matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("Addition of two matrices is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
break;

case 2:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements of 2nd matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
}
printf("Subtraction of two matrices is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
break;

case 3:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Upper triangular elements are:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
printf("0 ");
}
else
{
printf("%d\t",mat1[i][j]);
}
}
printf("\n");
}
printf("lower triangular elements are:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
printf("0");
}
else
{
printf("%d\t",mat1[i][j]);
}
}
printf("\n");
}
break;

case 4:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Original matrix is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is:");
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",mat1[j][i]);
}
printf("\n");
}
break;

case 5:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements of 2nd matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat3[i][j]=0;
for(int k=0;k<n;k++)
{
mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
printf("product of two matrices is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
break;

default:
printf("Enter a valid choice");
break;
}
return 0;
}
OUTPUT.6
PROGRAM.6

#include <stdio.h>
int main()
{
int sum=0;
int marks[6];
for(int i=0;i<6;++i)
{
printf("Enter the marks of student for subject code 00597%d:-\t",i+1);
scanf("%d",&marks[i]);
printf("\n");
}
for(int i=0;i<6;++i)
{
sum=sum+marks[i];
}
int average=(sum)/60;
switch(average)
{
case 9:
printf("Your grade is A+");
break;
case 8:
printf("Your grade is A");
break;

case 7:
printf("Your grade is B+");
break;

case 6:
printf("Your grade is B");
break;

case 5:
printf("Your grade is C+");
break;

case 4:
printf("Your grade is C");
break;

case 3:
printf("Your grade is D");
break;

case 2:
printf("You failed , try again");
break;

default:
printf("Wrong data");
break;
}
return 0;
}
OUTPUT.7
PROGRAM.7

#include <stdio.h>
int main()
{
int i,n;
int arr[10];
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the elements of array:");
printf("\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Array afer updating is:");
printf("\n");
for(i=0;i<n;i++)
{
arr[i]=arr[i]*arr[i];
printf("%d",arr[i]);
printf("\n");
}
return 0; }
OUTPUT.8
PROGRAM.8

#include <stdio.h>
int main()
{
char string1[50];
char string2[50];
int a;
int i;
int j;
printf("Enter 1 to find length of the string:");
printf("\n");
printf("Enter 2 to find concatenate of two strings:");
printf("\n");
printf("Enter 3 to find reverse of a string:");
printf("\n");

printf("Enter your choice:");


scanf("%d",&a);

switch(a)
{
case 1:
int length=0;
printf("Enter the string:");
printf("\n");
scanf("%s",&string1);

for(i=0;string1[i]!='\0';i++)
{
length++;
}
printf("Length of the string is:");
printf("\n");
printf("%d",length);
break;

case 2:
printf("Enter the first string:");
printf("\n");
scanf("%s",&string1);
printf("Enter the second string:");
printf("\n");
scanf("%s",&string2);

for(i=0;string1[i]!='\0';++i);
for(j=0;string2[j]!='\0';++j,++i)
{
string1[i]=string2[j];
}
string1[i]='\0';
printf("Concatenated string is:");
printf("\n");
printf("%s",string1);
break;

case 3:
printf("Enter a string to be reversed:");
printf("\n");
scanf("%s",&string1);

i=0;
while(string1[i]!='\0')
i++;
i--;

j=0;
while(i>=0)
{
string2[j]=string1[i];
j++;
i--;
}
string2[j]='\0';
printf("Reversed string is:");
printf("\n");
printf("%s",string2);
break;

default:
printf("Enter a valid choice.");
break;
}
return 0;
}
OUTPUT.9
PROGRAM.9

#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}

while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}

fclose(fs);
fclose(ft);
return 0;
}
OUTPUT.10
PROGRAM.10

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

// Structure of the employee


struct emp {
char name[50];
float salary;
int age;
int id;
};
struct emp e;

// size of the structure


long int size = sizeof(e);

// In the start coordinates


// will be 0, 0
COORD cord = { 0, 0 };
// function to set the
// coordinates
void gotoxy(int x, int y)
{
cord.X = x;
cord.Y = y;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE),
cord);
}

FILE *fp, *ft;

// Function to add the records


void addrecord()
{
system("cls");
fseek(fp, 0, SEEK_END);
char another = 'y';

while (another == 'y') {


printf("\nEnter Name : ");
scanf("%s", e.name);

printf("\nEnter Age : ");


scanf("%d", &e.age);
printf("\nEnter Salary : ");
scanf("%f", &e.salary);

printf("\nEnter EMP-ID : ");


scanf("%d", &e.id);

fwrite(&e, size, 1, fp);

printf("\nWant to add another"


" record (Y/N) : ");
fflush(stdin);

scanf("%c", &another);
}
}

// Function to delete the records


void deleterecord()
{
system("cls");
char empname[50];
char another = 'y';

while (another == 'y') {


printf("\nEnter employee "
"name to delete : ");
scanf("%s", empname);

ft = fopen("temp.txt", "wb");
rewind(fp);

while (fread(&e, size,


1, fp)
== 1) {
if (strcmp(e.name,
empname)
!= 0)
fwrite(&e, size, 1, ft);
}

fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt", "data.txt");
fp = fopen("data.txt", "rb+");

printf("\nWant to delete another"


" record (Y/N) :");
fflush(stdin);
another = getche();
}
}

// Function to display the record


void displayrecord()
{
system("cls");

// sets pointer to start


// of the file
rewind(fp);

printf("\n========================="
"==========================="
"======");
printf("\nNAME\t\tAGE\t\tSALARY\t\t"
"\tID\n",
e.name, e.age,
e.salary, e.id);
printf("==========================="
"==========================="
"====\n");

while (fread(&e, size, 1, fp) == 1)


printf("\n%s\t\t%d\t\t%.2f\t%10d",
e.name, e.age, e.salary, e.id);
printf("\n\n\n\t");
system("pause");
}

// Function to modify the record


void modifyrecord()
{
system("cls");
char empname[50];
char another = 'y';

while (another == 'y') {


printf("\nEnter employee name"
" to modify : ");
scanf("%s", empname);

rewind(fp);

// While File is open


while (fread(&e, size, 1, fp) == 1) {
// Compare the employee name
// with ename
if (strcmp(e.name, empname) == 0) {
printf("\nEnter new name:");
scanf("%s", e.name);
printf("\nEnter new age :");
scanf("%d", &e.age);
printf("\nEnter new salary :");
scanf("%f", &e.salary);
printf("\nEnter new EMP-ID :");
scanf("%d", &e.id);

fseek(fp, -size, SEEK_CUR);


fwrite(&e, size, 1, fp);
break;
}
}

// Ask for modifying another record


printf("\nWant to modify another"
" record (Y/N) :");
fflush(stdin);
scanf("%c", &another);
}
}

// Driver code
int main()
{
int choice;

// opening the file


fp = fopen("data.txt", "rb+");

// showing error if file is


// unable to open.
if (fp == NULL) {
fp = fopen("data.txt", "wb+");
if (fp == NULL) {
printf("\nCannot open file...");
exit(1);
}
}

system("Color 3F");
printf("\n\n\n\n\t\t\t\t============="
"============================="
"===========");
printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~~~~~");
printf("\n\t\t\t\t==================="
"============================="
"=====");
printf("\n\t\t\t\t[|:::>:::>:::>::> "
"EMPLOYEE RECORD <::<:::<:::"
"<:::|]\t");
printf("\n\t\t\t\t==================="
"============================="
"=====");
printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~~"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~~~");
printf("\n\t\t\t\t====================="
"==============================\n");
printf("\n\n\n\t\t\t\t\t\t\t\t\t\t"
"Developer : @Mayank_katiyar"
"\n\n\t\t\t\t");

system("pause");

while (1) {
// Clearing console and asking the
// user for input
system("cls");
gotoxy(30, 10);
printf("\n1. ADD RECORD\n");
gotoxy(30, 12);
printf("\n2. DELETE RECORD\n");
gotoxy(30, 14);
printf("\n3. DISPLAY RECORDS\n");
gotoxy(30, 16);
printf("\n4. MODIFY RECORD\n");
gotoxy(30, 18);
printf("\n5. EXIT\n");
gotoxy(30, 20);
printf("\nENTER YOUR CHOICE...\n");
fflush(stdin);
scanf("%d", &choice);

// Switch Case
switch (choice) {
case 1:

// Add the records


addrecord();
break;

case 2:

// Delete the records


deleterecord();
break;

case 3:

// Display the records


displayrecord();
break;
case 4:

// Modify the records


modifyrecord();
break;

case 5:
fclose(fp);
exit(0);
break;

default:
printf("\nINVALID CHOICE...\n");
}
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy