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

B.Tech. Mech. Practical File Questions

The document contains 6 programs written in C programming language to perform various string and matrix operations: 1. The first program calculates the sum of terms in a geometric progression series. 2. The second program solves the Tower of Hanoi recursive puzzle. 3. The third program prints the Fibonacci series up to a specified range. 4. The fourth program performs matrix addition, subtraction, multiplication, transpose and symmetry operations on 3x3 matrices. 5. The fifth program copies the contents from one file to another using command line arguments. 6. The sixth program implements various string operations like length, concatenation, reverse, and copy.

Uploaded by

kunal
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

B.Tech. Mech. Practical File Questions

The document contains 6 programs written in C programming language to perform various string and matrix operations: 1. The first program calculates the sum of terms in a geometric progression series. 2. The second program solves the Tower of Hanoi recursive puzzle. 3. The third program prints the Fibonacci series up to a specified range. 4. The fourth program performs matrix addition, subtraction, multiplication, transpose and symmetry operations on 3x3 matrices. 5. The fifth program copies the contents from one file to another using command line arguments. 6. The sixth program implements various string operations like length, concatenation, reverse, and copy.

Uploaded by

kunal
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Program 1: WAP to find Sum of GP series in c programming language

#include<stdio.h>
#include<math.h>

int main(){

    float a,r,i,tn;
    int n;
    float sum=0;

    printf("Enter the first number of the G.P. series: ");


    scanf("%f",&a);

    printf("Enter the total numbers in the G.P. series: ");


    scanf("%d",&n);

    printf("Enter the common ratio of G.P. series: ");


    scanf("%f",&r);

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


       tn = a * (1 -pow(r,n-1));

    printf("tn term of G.P.: %f",tn);


    printf("\nSum of the G.P.: %f",sum);

    return 0;
}

Sample output:

Enter the first number of the G.P. series: 1


Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.000000

Definition of geometric progression (G.P.):

A series of numbers in which ratio of any two consecutive numbers is always a same number that is
constant. This constant is called as common ratio.

Example of G.P. series:


                                             
2 4 8 16 32 64
Here common difference is 2 since ratio any two consecutive numbers for example 32 / 16 or 64/32 is
2.
Sum of G.P. series:
Sn =a(1–rn+1)/(1-r)

Tn term of G.P. series:

Tn = arn-1          

Sum of infinite G.P. series:

Sn = a/(1-r)  if 1 > r


   = a/(r-1)  if r > 1

Program 2: WAP for Tower of hanoi


#include <stdio.h>

void towers(int,char,char,char);

void towers(int n,char frompeg,char topeg,char auxpeg)


{ /* If only 1 disk, make the move and return */
if(n==1)
{ printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg);
return;
}
/* Move top n-1 disks from A to B, using C as auxiliary */
towers(n-1,frompeg,auxpeg,topeg);
/* Move remaining disks from A to C */
printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg);
/* Move n-1 disks from B to C using A as auxiliary */
towers(n-1,auxpeg,topeg,frompeg);
}
main()
{ int n;
printf("Enter the number of disks : ");
scanf("%d",&n);
printf("The Tower of Hanoi involves the moves :\n\n");
towers(n,'A','C','B');
return 0;
}

The recursive solution involves the following steps :


1. if n==1, move the single disk from A to C and stop.
2. Move the top n-1 disks from A to B, using C as auxiliary.
3. Move the remaining disk from A to C.
4. Move the n-1 disks from B to C, using A as auxiliary.
Program 3: WAP for Fibonacci
#include<stdio.h>
void printFibonacci(int);
int main()
{
    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");


    scanf("%d",&n);

    printf("Fibonacci Series: ");


    printf("%d %d ",0,1);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    static long int first=0,second=1,sum;

    if(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         printFibonacci(n-1);
    }

Sample output:

Enter the range of the Fibonacci series: 10


Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89
Program 4: Program to find matrix addition, subtraction, multiplication, transpose and
symmetric operations in C Programming

# include<stdio.h>
void display(int [][3]);
void main()
{
int c;
void func1();
void func2();
void func3();
void func4();
void func5();
clrscr();
printf("\n- : Matrix Manipulation Functions (for 3 X 3 Matrix) : -");
printf("\n-------------------------------------");
printf("\n Matrix Addition : 1");
printf("\n Matrix Subtraction : 2");
printf("\n Matrix Multiplication : 3");
printf("\n Find Transpose Matrix : 4");
printf("\n Matrix is Symmetric or not : 6");
printf("\n Enter Your Choice : ");
scanf("%d",&c);
switch(c)
{
case 1:
func1();
break;
case 2:
func2();
break;
case 3:
func3();
break;
case 4:
func4();
break;
case 5:
func5();
break;
default:
printf("\nInvalid Choice");
}
getch();
}
void func1()
{
int x[3][3],y[3][3],z[3][3];
void getmatrix(int [][3]);
void addition(int [][3],int [][3],int [][3]);
clrscr();
getmatrix(x);
getmatrix(y);
addition(x,y,z);
printf("\n - : Matrix 1: - \n");
display(x);
printf("\n - : Matrix 2: - \n");
display(y);
printf("\n - : Matrix Addition (Result): - \n");
display(z);
}
void getmatrix(int t[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element [%d][%d] : ",i,j);
scanf("%d",&t[i][j]);
}
}
}
void addition(int p[][3],int q[][3],int r[][3])
{ int i,j;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
r[i][j]=p[i][j]+q[i][j];
}
}
void func2()
{
int x[3][3],y[3][3],z[3][3];
void getmatrix(int [][3]);
void subtraction(int [][3],int [][3],int [][3]);
clrscr();
getmatrix(x);
getmatrix(y);
subtraction(x,y,z);
printf("\n - : Matrix 1: - \n");
display(x);
printf("\n - : Matrix 2: - \n");
display(y);
printf("\n - : Matrix Subtraction (Result): - \n");
display(z);
}
void subtraction(int p[3][3],int q[3][3],int r[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
r[i][j]=p[i][j]-q[i][j];
}
}
void func3()
{
int x[3][3],y[3][3],z[3][3];
void getmatrix(int [][3]);
void multiplication(int [][3],int [][3],int [][3]);
clrscr();
getmatrix(x);
getmatrix(y);
multiplication(x,y,z);
printf("\n - : Matrix 1: - \n");
display(x);
printf("\n - : Matrix 2: - \n");
display(y);
printf("\n - : Matrix Multiplication (Result): - \n");
display(z);
}
void multiplication(int p[][3],int q[3][3],int r[3][3])
{
int i,j,k;
for(i=0;i<3;i++)
//condition i< total row of matrix1
{
for(j=0;j<3;j++)
//condition i< total col of matrix1 or//condition i< total row of matrix2
{
r[i][j]=0;
for(k=0;k<3;k++) //condition i< total col of matrix2
r[i][j]=r[i][j]+(p[i][j]*q[j][k]);
}
}
}
void func4()
{
int x[3][3],y[3][3];
void getmatrix(int [][3]);
void transpose(int [][3],int [][3]);
clrscr();
getmatrix(x);
transpose(x,y);
printf("\n - : Matrix 1: - \n");
display(x);
printf("\n - : Transpose Matrix : - \n");
display(y);
}
void transpose(int p[][3],int q[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
q[i][j]=p[j][i];
}
}
void func5()
{
int x[3][3],y[3][3];
void getmatrix(int [][3]);
void transpose(int [][3],int [][3]);
int symmetric(int [][3],int [][3]);
clrscr();
getmatrix(x);
transpose(x,y);
if(symmetric(x,y)==1)
printf("\nMatrix is Symmetric");
else
printf("\nMatrix is Not Symmetric");
}
int symmetric(int p[][3],int q[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(q[i][j]!=p[i][j])
return 0;
}
}
return 1;
}
void display(int m[][3])
{
int i,j;
printf("\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",m[i][j]);
printf("\n");
}
}
Program 5: Write a ‘C’ program to copy content of one file into another file using command
line argument

#include<stdio.h>
#include<conio.h>
main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char ch;
clrscr();
if(argc!=3)
{
printf("\n insufficient argument ");
exit(0);
}
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
if(fp1==NULL || fp2==NULL)
{
printf("\n unable to open file ");
exit(0);
}
while(!feof(fp1))
{
ch=fgetc(fp1);
fputc(ch,fp2);
}
printf("\n file successfully copied ");
fclose(fp1);
fclose(fp2);
getch();
}

Program 6: WAP for string operations

#include <stdio.h>
int main()
{
char s1[100], s2[100], temp;
int ch, i, j ;

printf(“ \t\t MENU\n 1.String Length\n 2. String concatenation \n 3. String Reverse \n 4. String
Copy\n)”;
printf(“Ënter your choice(1-4)\n”);
scanf(“”%d”, &ch);

switch(ch)
{
case 1:
printf("Enter a string: ");
scanf("%s",s1);

for(i=0; s1[i]!='\0'; ++i);


printf("Length of string: %d", i);
break;

case 2:
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);

for(i=0; s1[i]!='\0'; ++i); /* i contains length of string s1. */

for(j=0; s2[j]!='\0'; ++j, ++i)


{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
break;
case 3:
printf("\nEnter the string :");
   gets(s1);
 
i = 0;
j = strlen(s1) - 1;
 
while (i < j)
{
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
       i++;
     j--;
   }
 
   printf("\nReverse string is :%s", s1);
break;

case 4:
printf("Enter string s1: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
break;
default:
printf(“Invalid choice”);

return 0;
}

You might also like