Lab 1
Lab 1
Lab 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);
sum = (float)((a*(1-pow(r,n+1)))/(1-r));
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);
}
#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");
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>
scanf("%c", &another);
}
}
ft = fopen("temp.txt", "wb");
rewind(fp);
fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt", "data.txt");
fp = fopen("data.txt", "rb+");
printf("\n========================="
"==========================="
"======");
printf("\nNAME\t\tAGE\t\tSALARY\t\t"
"\tID\n",
e.name, e.age,
e.salary, e.id);
printf("==========================="
"==========================="
"====\n");
rewind(fp);
// Driver code
int main()
{
int choice;
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:
case 2:
case 3:
case 5:
fclose(fp);
exit(0);
break;
default:
printf("\nINVALID CHOICE...\n");
}
}
return 0;
}