185253 Computer Practice Laboratory –II EXPECTED QUESTIONS --SHELL PROGRAM, C PROGRAM, BASIC UNIX COMMANDS



COMPUTER PRACTICE LAB II (2010-2011)
 EXAM EXPECTED QUESTIONS

SHELL PROGRAM
1)      A. Write a Shell program to check the given number is even or odd
B.     Write a Shell program to check the given year is leap year or not
2)      Write a Shell program to find the area and circumference of a circle
3)      Write a Shell program to check the given number is palindrome or not
4)      Write a Shell program to check the given integer is Armstrong number or not
5)      Write a Shell program to check the given integer is prime or not
6)      Write a Shell program to find the sum of square of individual digits of a number
7)      Write a Shell program to find the largest among three numbers
8)      Write a Shell program to generate Fibonacci series
9)      Write a Shell program to find the factorial of a number
10)  Write a Shell program to execute various UNIX commands using case statements
11)  Write a Shell program to find the number of digits in an integer
12)  Write a Shell program to find the sum of digits in an integer
13)  Write a Shell program to perform the arithmetic operation using case statements
14)  Write a Shell program to swap the two integers ( i ) with temporary variable and    ( ii ) without using temporary variable.
15)  Write a Shell program to find the sum of all numbers between 50 and 100, which are divisible by 3 and not divisible by 5.

C PROGRAM
1)      Write a C program using Dynamic Memory Allocation to sort ‘n’ numbers in ascending order.
2)      Write a C program using Dynamic Memory Allocation to sort ‘n’ numbers in descending order.
3)      Write a C program using Dynamic Memory Allocation to perform addition of two matrices.
4)      Write a C program using Dynamic Memory Allocation to perform subtraction of two matrices.
5)      Write a C program using Dynamic Memory Allocation to perform Transpose of two matrices.
6)      Write a C program using Dynamic Memory Allocation to perform Multiplication of two matrices.
7)      Write a C program using Dynamic Memory Allocation to find the minimum and maximum of a set of numbers
8)      Write a C program to find the sum of ‘n’ numbers using function
9)      Write a C program to find the sum of digits of a number using function
10)  Write a C program to find the sum of square of a individual digits of a number using function
11)  Write a C program to check whether the given number and its reverse are same,  using function
12)  Write a C program using pointers to swap two numbers
13)  Write a C program to find the sum of cube of individual digits of a number
14)   Write a C program using DMA to perform the linear search
15)   Write a C program to perform file copy operation


BASIC UNIX COMMANDS

1) Write and execute the following UNIX Commands
  • Create two files with the name of name.txt, which contains only names, and reg.txt with the content of register number respectively.
  • Combine the two files in the form of register number followed by name
  • Sort the two files in ascending order
  • Count the number of lines in the files
  • Rename the two files

2) Write and execute the following UNIX Commands
  • Create two files
  • Combine the two files
  • Search a specific word from any one of the file
  • Search a specific file from a directory
  • Display the common and distinct line of contents from a file.

3) Write and execute the following UNIX Commands
  • Create two files
  • Display the contents of both the files
  • Count the number of characters in both the files
  • Rename the two files
  • Combine the two files without duplicate

4) Write and execute the following UNIX Commands
  • To view all the files and directories page by page
  • To view only the directories page by page
  • To view only the files in a directory
  • Display the working directory
  • Display the complete path of the working directory

5) Write and execute the following UNIX Commands
  • Display the calendar
  • Execute more than one command at a time (using semi-colon ;)
  • Display the user’s full details
  • Display the user-id / Group-id
  • Execute more than one command at a time ( using logical – AND && ).


6) Write and execute the following UNIX Commands
  • Demonstrate pipe command
  • Demonstrate tee command
  • Use more than one command at a time ( who and date )
  • Create a file called create.txt
  • Move the file create.txt to move.txt

7) Write and execute the following UNIX Commands
  • Create a file
  • Print the specified number of lines of a file from start to end of the file
  • Display the file content with line number
  • Update the file content with I/O redirection
  • Rename the file

8) Write and execute the following UNIX Commands
  • Create a directory
  • Create a file
  • Rename the directory
  • Rename the file
  • View the file


9) Write and execute the following UNIX Commands
  • Create a file
  • Update the file
  • Display the file content
  • Display the file with line number
  • Count the number of words in the file

10) Write and execute the following UNIX Commands
  • Create a file
  • Rename a file
  • Change the mode of the file to read only
  • View the content of a file
  • Count the number of words in file

11) Write and execute the following UNIX Commands
  • Create a Directory called main
  • Create a sub-directory called sub in the main directory
  • Create a file in the main directory
  • Create the file to the sub directory
  • Delete the file in the main directory



12) Write and execute the following UNIX Commands
  • Display the calendar
  • Display the date and time
  • Display the present working directory
  • Display your user name
  • Create a file using cat command

13) Write and execute the following UNIX Commands
  • Create two directories called CS1101 and CS1102
  • Create a file called CS1101.txt in the CS1101 directory
  • Create the file CS1101.txt from CS1101 to CS1102
  • Update the file content of CS1101.txt in the CS1102 directory
  • Rename the file in the CS1102 with CS1102.txt

14) Write and execute the following UNIX Commands
  • Create a directory with the name of Exam
  • Change the Exam directory as the working directory
  • Create a file called exam.txt in the Exam directory
  • View the content of the exam.txt file
  • Rename the file exam.txt to test.txt

15) Write and execute the following UNIX Commands
  • Create a file
  • View only files from a directory
  • Rename a file
  • View all files starting with a specific character
  • View all the files which has a specific extension


 1)      A. Write a Shell program to check the given number is even or odd

echo "Enter the Number"
read n
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo "$n is Even number"
else
echo "$n is Odd number"
fi

OUTPUT

[nehru@localhost shellexam]$ sh oddeven
Enter the Number
35
35 is Odd number
[nehru@localhost shellexam]$ sh oddeven
Enter the Number
42
42 is Even number


B.     Write a Shell program to check the given year is leap year or not

echo "Enter the year"
read y
b=`expr $y % 4`
if [ $b -eq 0 ]
then
echo "$y is a leap year"
else
echo "$y is not a leap year"
fi

OUTPUT

[nehru@localhost shellexam]$ sh leapyear
Enter the year
2000
2000 is a leap year
[nehru@localhost shellexam]$ sh leapyear
Enter the year
2010
2010 is not a leap year
2)      Write a Shell program to find the area and circumference of a circle

echo "Enter the radius"
read r
area=`expr "3.14 * $r * $r" | bc`
cir=`expr "2 * 3.14 * $r" | bc`
echo "The area is " $area
echo "The circumference is " $cir

OUTPUT

[nehru@localhost shellexam]$ sh areacircumference
Enter the radius
3
The area is  28.26
The circumference is  18.84

3)      Write a Shell program to check the given number is palindrome or not

echo "Enter a number"
read a
d=$a
c=0
b=1
while [ $a -gt 0 ]
do
b=`expr $a % 10`
c=`expr $c \* 10 + $b`
a=`expr $a / 10`
done
if [ $d -eq $c ]
then
echo "PALINDROME"
else
echo "NOT PALINDROME"
fi

OUTPUT
[nehru@localhost shellexam]$ sh palindromeno
Enter a number
12321
PALINDROME
[nehru@localhost shellexam]$ sh palindromeno
Enter a number
12345
NOT PALINDROME
4)      Write a Shell program to check the given integer is Armstrong number or not

echo "Enter a Number"
read num
x=$num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
done
if [ $x -eq $sum ]
then
echo "$x is an armstrong Number"
else
echo "$x is not an armstrong Number"
fi

OUTPUT


[nehru@localhost shellprogram]$ sh armstrong
Enter a Number
123
123 is not an armstrong Number
[nehru@localhost shellprogram]$ sh armstrong
Enter a Number
153
153 is an armstrong Number















5)      Write a Shell program to check the given integer is prime or not

echo "Enter a Number"
read n
i=`expr $n - 1`
t=0
while [ $i -ge 2 ]
do
p=`expr $n % $i`
if [ $p -eq 0 ]
then
t=`expr $t + 1`
fi
i=`expr $i - 1`
done
if [ $t -gt 0 ]
then
echo "The Number $n is not a Prime Number"
else
echo "The Number $n is a Prime Number"
fi

OUTPUT

[nehru@localhost shellprogram]$ sh primeno
Enter a Number
7
The Number 7 is a Prime Number
[nehru@localhost shellprogram]$ sh primeno
Enter a Number
12
The Number 12 is not a Prime Number














6)      Write a Shell program to find the sum of square of individual digits of a number

echo "enter the number"
read a
b=1
c=0
while [ $a -gt 0 ]
do
b=`expr $a % 10`
c=`expr $c + $b \* $b`
a=`expr $a / 10`
done
echo "The sum of square of digits is" $c



OUTPUT

[nehru@localhost shellexam]$ sh sumsquare
enter the number
234
The sum of square of digits is 29



7)      Write a Shell program to find the largest among three numbers

echo "Enter Three Numbers"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is Greater"
elif [ $b -gt $c ]
then
echo "$b is Greater"
else
echo "$c is Greater"
fi


OUTPUT

[nehru@localhost shellexam]$ sh great3no
Enter Three Numbers
22 55 33
55 is Greater
8)      Write a Shell program to generate Fibonacci series

echo "Enter the Limit"
read n
i=2
echo "Fibonacci Series"
echo "----------------"
a=0
b=1
echo $a
echo $b
while [ $i -lt $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done


OUTPUT

[nehru@localhost shellexam]$ sh fibonacci
Enter the Limit
10
Fibonacci Series
----------------
0
1
1
2
3
5
8
13
21
34








9)      Write a Shell program to find the factorial of a number

echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"


OUTPUT

[nehru@localhost shellexam]$ sh factorial
Enter a Number
6
The Factorial of the given Number is 720


10)  Write a Shell program to execute various UNIX commands using case statements


echo "          MENU"
echo "1. List of files"
echo "2. Today's date"
echo "3. Display the Present month of a Calendar"
echo "4. Logged in users"
echo "5. Display in current working directory"
echo "6. Quit"
echo -n "Enter your choice: "
        read ch
        case $ch in
                1) ls;;
                2) date;;
                3) cal;;
                4) who;;
                5) pwd;;
                6) exit;;
                *) echo "Invalid choice"
             exit;;
        esac


OUTPUT


[nehru@localhost shellexam]$ sh casecommand1
          MENU
1. List of files
2. Today's date
3. Display the Present month of a Calendar
4. Logged in users
5. Display in current working directory
6. Quit

Enter your choice: 1
areacircumference  factorial  nodigits      reverseno  sumsquare
arithcase          fibonacci  oddeven       sumcube    swaptemp
casecommand        great3no   palindromeno  sumdigits  swapwouttemp
casecommand1       leapyear   prime         sumnnos
[nehru@localhost shellexam]$ sh casecommand1

Enter your choice: 2
Tue Apr 26 14:46:37 IST 2011


Enter your choice: 3
     April 2011
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Enter your choice: 4
root     :0           2011-04-26 07:58
root     pts/1        2011-04-26 07:58 (:0.0)
nehru    pts/2        2011-04-26 13:48 (180.170.40.43)


Enter your choice: 5
/home/nehru/shellexam


Enter your choice: 11
Invalid choice


11)  Write a Shell program to find the number of digits in an integer

echo "Enter a Number"
read a
c=0
while [ $a -ne 0 ]
do
c=`expr $c + 1`
a=`expr $a / 10`
done
echo "The Number of Digits in the Integer is $c"

OUTPUT

[nehru@localhost shellexam]$ sh nodigits
Enter a Number
12345
The Number of Digits in the Integer is 5
[nehru@localhost shellexam]$ sh nodigits
Enter a Number
1000
The Number of Digits in the Integer is 4


12)  Write a Shell program to find the sum of digits in an integer


echo "Enter a Number"
read num
sum=0
while [ $num -gt 0 ]
do            
y=`expr $num % 10`
sum=`expr $sum + $y`
num=`expr $num / 10`
done
echo "The Sum of the Digits of the Integer is $sum"

OUTPUT

[nehru@localhost shellexam]$ sh sumdigits
Enter a Number
12345
The Sum of the Digits of the Integer is 15




13)  Write a Shell program to perform the arithmetic operation using case statements


echo "Enter Two Numbers"
read a b
echo "What do you want to do? (1 to 5)"
echo "1) Sum"
echo "2) Difference"
echo "3) Product"
echo "4) Quotient"
echo "5) Remainder"
echo "Enter your Choice"
read n
case "$n" in
1) echo "The Sum of $a and $b is `expr $a + $b`";;
2) echo "The Difference between $a and $b is `expr $a - $b`";;
3) echo "The Product of the $a and $b is `expr $a \* $b`";;
4) echo "The Quotient of $a by $b is `expr $a / $b`";;
5) echo "The Remainder of $a by $b is `expr $a % $b`";;
esac

OUTPUT

[nehru@localhost shellexam]$ sh arithcase
Enter Two Numbers
22 11
What do you want to do? (1 to 5)
1) Sum
2) Difference
3) Product
4) Quotient
5) Remainder

Enter your Choice
3
The Product of the 4 and 3 is 12









14)  Write a Shell program to swap the two integers

 ( i ) with temporary variable

 echo "Enter Two Numbers"
read a b
temp=$a
a=$b
b=$temp
echo "after swapping"
echo $a $b

OUTPUT

[nehru@localhost shellexam]$ sh swaptemp
Enter Two Numbers
22 33
after swapping
33 22

( ii ) without using temporary variable.


echo "Enter Two Numbers"
read a b
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo "after swapping of values: $a $b"


OUTPUT


[nehru@localhost shellexam]$ sh swapouttemp
Enter Two Numbers
22 333
after swapping of values: 333 22







15)  Write a Shell program to find the sum of all numbers between 50 and 100, which are divisible by 3 and not divisible by 5.

sum=0
i=50
b=1
c=1
while [ $i -le 100 ]
do
b=`expr $i % 3`
c=`expr $i % 5`
if [ $b -eq 0 ] && [ $c -ne 0 ]
then
sum=`expr $sum + $i`
fi
i=`expr $i + 1`
done
echo "The sum of all numbers between 50 and 100 which are divisible by 3 and not
 divisible by 5 is" $sum


OUTPUT

[nehru@localhost shellexam]$ sh sumbetween50to100
The sum of all numbers between 50 and 100 which are divisible by 3 and not
   divisible by 5 is 1050




















C PROGRAM

1)      Write a C program using Dynamic Memory Allocation to sort ‘n’ numbers in ascending order.

#include
/*to sort the given number in ascending order using arrays*/
#include
#include
main()
{
 int i,j,n,*a,temp;
 printf("Enter total number of items--->");
 scanf("%d",&n);
 a=(int *)malloc(n*sizeof(int));
 printf("\nEnter the elements one by one:");
 for(i=0;i
   scanf("%d",&a[i]);
 for(i=0;i
  for(j=i+1;j
  {
   if(a[i]>a[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 for(i=0;i
    printf("%d\t",a[i]);
}

OUTPUT

[nehru@localhost cexam]$ cc dmaascen.c
[nehru@localhost cexam]$ ./a.out
Enter total number of items--->5

Enter the elements one by one:22 55 33 44 11
11      22      33      44      55     






2)      Write a C program using Dynamic Memory Allocation to sort ‘n’ numbers in descending order.

#include
#include
main()
{
 int i,j,n,*a,temp;
 printf("Enter total number of items--->");
 scanf("%d",&n);
 a=(int *)malloc(n*sizeof(int));
 printf("\nEnter the elements one by one:");
 for(i=0;i
   scanf("%d",&a[i]);
 for(i=0;i
  for(j=i+1;j
  {
   if(a[i]
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 for(i=0;i
    printf("%d\t",a[i]);
}

OUTPUT

[nehru@localhost cexam]$ cc dmadescen.c
[nehru@localhost cexam]$ ./a.out
Enter total number of items--->5

Enter the elements one by one:22 54 33 11 88
88      54      33      22      11     










3)      Write a C program using Dynamic Memory Allocation to perform addition of two matrices.

MATRIX ADDITION USING DMA

#include
main()
{
int **a,**b,**c,i,j,m,n,p,q;
printf("Enter the row and column of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter the row and column of second matrix:");
scanf("%d%d",&p,&q);
if((m==p)&&(n==q))
{
printf("Matrix addition is possible:\n");
printf("Enter the values of matrix a\n");
a=(int **)malloc(m*sizeof(int*));
for(i=0;i
a[i]=(int *)malloc(n*sizeof(int));
b=(int **)malloc(m*sizeof(int*));
for(i=0;i
b[i]=(int *)malloc(n*sizeof(int));
c=(int **)malloc(m*sizeof(int*));
for(i=0;i
c[i]=(int *)malloc(n*sizeof(int));
for(i=0;i
{
 for(j=0;j
 {
  scanf("%d",&a[i][j]);
 }
}
printf("Enter the values of matrix b:\n");
for(i=0;i
{
 for(j=0;j
 {
  scanf("%d",&b[i][j]);
 }
}
for(i=0;i
{
 for(j=0;j
 {
  c[i][j]=a[i][j]+b[i][j];
 }
}
printf("the resultant matrix is:\n");
for(i=0;i
{
 for(j=0;j
 {
  printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
 printf("Add is not possible, row and column of both matrix should be same:");
}



OUTPUT


[nehru@localhost cexam]$ cc dmaaddmatrix.c
dmaaddmatrix.c: In function ΓÇÿmainΓÇÖ:
dmaaddmatrix.c:14: warning: incompatible implicit declaration of built-in functi
on ΓÇÿmallocΓÇÖ
[nehru@localhost cexam]$ ./a.out
Enter the row and column of first matrix:3 3
Enter the row and column of second matrix:3 3
Matrix addition is possible:
Enter the values of matrix a
1 2 3
4 5 6
7 8 9
Enter the values of matrix b:
9 8 7
6 5 4
3 2 1
the resultant matrix is:
10      10      10
10      10      10
10      10      10
[nehru@localhost cexam]$




4)      Write a C program using Dynamic Memory Allocation to perform subtraction of two matrices.

MATRIX SUBTRACTION USING DMA

#include
#include
main()
{
int **a,**b,**c,i,j,m,n,p,q;
printf("Enter the row and column of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter the row and column of second matrix:");
scanf("%d%d",&p,&q);
if((m==p)&&(n==q))
{
printf("Matrix subtraction is possible:\n");
printf("Enter the values of matrix a\n");
a=(int **)malloc(m*sizeof(int*));
for(i=0;i
a[i]=(int *)malloc(n*sizeof(int));
b=(int **)malloc(m*sizeof(int*));
for(i=0;i
b[i]=(int *)malloc(n*sizeof(int));
c=(int **)malloc(m*sizeof(int*));
for(i=0;i
c[i]=(int *)malloc(n*sizeof(int));
for(i=0;i
{
 for(j=0;j
 {
  scanf("%d",&a[i][j]);
 }
}
printf("Enter the values of matrix b:\n");
for(i=0;i
{
 for(j=0;j
 {
  scanf("%d",&b[i][j]);
 }
}
for(i=0;i
{
 for(j=0;j
 {
  c[i][j]=a[i][j]-b[i][j];
 }
}
printf("the resultant matrix is:\n");
for(i=0;i
{
 for(j=0;j
 {
  printf("%d\t",c[i][j]);
 }
printf("\n");
}
}
else
 printf("Subtraction is not possible, row & column of both matrix should be same
:");
}

OUTPUT

[nehru@localhost cexam]$ cc dmasubmatrix.c
dmasubmatrix.c: In function ΓÇÿmainΓÇÖ:
dmasubmatrix.c:14: warning: incompatible implicit declaration of built-in functi
on ΓÇÿmallocΓÇÖ
[nehru@localhost cexam]$ ./a.out
Enter the row and column of first matrix:3 3
Enter the row and column of second matrix:3 3
Matrix subtraction is possible:
Enter the values of matrix a
12 3 4
3 4 12
4 12 3
Enter the values of matrix b:
1 2 3
4 5 6
7 8 9
the resultant matrix is:
11      1       1
-1      -1      6
-3      4       -6
[nehru@localhost cexam]$





5)      Write a C program using Dynamic Memory Allocation to perform Transpose of two matrices.

    TRANSPOSE MATRIX


#include
main()
{
 int **a,**b,i,j,m,n;
 printf("Enter the Row and column of matrix\n");
 scanf("%d%d",&m,&n);
 a=(int **)malloc(m*sizeof(int*));
 for(i=0;i
 a[i]=(int *)malloc(n*sizeof(int));
 b=(int **)malloc(n*sizeof(int *));
 for(i=0;i
 b[i]=(int *)malloc(n*sizeof(int));
 printf("Enter the elements of matrix:\n");
 for(i=0;i
 for(j=0;j
   scanf("%d",&a[i][j]);
 printf("The given matrix is:\n");
 for(i=0;i
 {
  for(j=0;j
    printf("%6d",a[i][j]);
  printf("\n");
 }
 for(i=0;i
 for(j=0;j
    b[i][j]=a[j][i];
 printf("The Transpose of Matrix is:\n");
 for(i=0;i
 {
 for(j=0;j
    printf("%6d",b[i][j]);
 printf("\n");
 }
}






OUTPUT

[nehru@localhost cexam]$ cc dmatransmatrix.c
dmatransmatrix.c: In function ΓÇÿmainΓÇÖ:
dmatransmatrix.c:8: warning: incompatible implicit declaration of built-in funct
ion ΓÇÿmallocΓÇÖ



[nehru@localhost cexam]$ ./a.out
Enter the Row and column of matrix
3 3
Enter the elements of matrix:
3 6 9
1 3 6
6 9 1
The given matrix is:
     3     6     9
     1     3     6
     6     9     1
The Transpose of Matrix is:
     3     1     6
     6     3     9
     9     6     1






















6)      Write a C program using Dynamic Memory Allocation to perform Multiplication of two matrices.

#include
#include
main()
{
int **a,**b,**c,i,j,k,m,n,p,q;
printf("Enter the row and column of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter the row and column of second matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("Matrix multiplication is possible:\n");
printf("Enter the values of matrix a\n");
a=(int **)malloc(m*sizeof(int*));
for(i=0;i
a[i]=(int *)malloc(n*sizeof(int));
b=(int **)malloc(m*sizeof(int*));
for(i=0;i
b[i]=(int *)malloc(q*sizeof(int));
c=(int **)malloc(m*sizeof(int*));
for(i=0;i
c[i]=(int *)malloc(q*sizeof(int));
for(i=0;i
{
 for(j=0;j
 {
  scanf("%d",&a[i][j]);
 }
}
printf("Enter the values of matrix b:\n");
for(i=0;i
{
 for(j=0;j
 {
  scanf("%d",&b[i][j]);
 }
}
for(i=0;i
{
 for(j=0;j
 {
  c[i][j]=0;
  for(k=0;k
    c[i][j]=a[i][k]*b[k][j]+c[i][j];
 }
}
printf("the resultant matrix is:\n");
for(i=0;i
{
 for(j=0;j
 {
  printf("%d\t",c[i][j]);
 }
printf("\n");
}
}
else
 printf("Add is not possible, row and column of both matrix should be same:");
}


OUTPUT

[nehru@localhost cexam]$ cc dmamatmulti.c
dmamatmulti.c: In function ΓÇÿmainΓÇÖ:
dmamatmulti.c:14: warning: incompatible implicit declaration of built-in functio
n ΓÇÿmallocΓÇÖ

[nehru@localhost cexam]$ ./a.out
Enter the row and column of first matrix:3 3
Enter the row and column of second matrix:3 3
Matrix multiplication is possible:
Enter the values of matrix a
1 2 3
4 5 6
7 8 9
Enter the values of matrix b:
1 2 3
4 5 6
7 8 9
the resultant matrix is:
30      36      42
66      81      96
102     126     150





7)      Write a C program using Dynamic Memory Allocation to find the minimum and maximum of a set of numbers


/*TO FIND THE MAXIMUM AND MINIMUM VALUE USING DMA*/
#include
#include
main()
 {
  int *a,n,i,j,temp;
  printf("\nEnter the maximum limit:");
  scanf("%d",&n);
  a=(int *)malloc(n*sizeof(int));
  printf("Enter the %d values\n",n);
  for(i=0;i
   scanf("%d",&a[i]);
  for(i=0;i
  {
   for(j=i+1;j
   {
    if(a[i]>a[j])
    {
     temp=a[i];
     a[i]=a[j];
     a[j]=temp;
    }
   }
  }
  printf("\nThe smallest number is %d",a[0]);
  printf("\nThe biggest number is %d",a[n-1]);
}

OUTPUT
[nehru@localhost cexam]$ cc dmamaxmin.c
[nehru@localhost cexam]$ ./a.out

Enter the maximum limit:5
Enter the 5 values
56 3 2 41 8

The smallest number is 2
The biggest number is 56[nehru@localhost cexam]$





8)      Write a C program to find the sum of ‘n’ numbers using function


/* TO FIND THE SUM OF N NUMBERS USING FUNCTION */
#include
void sum(int[],int);
main()
{
 int a[20],i,n;
 printf("Enter the limit:\n");
 scanf("%d",&n);
 printf("Enter the elements:\n");
 for(i=0;i
    scanf("%d",&a[i]);
 sum(a,n);
 }

 void sum(int *a,int n)
 {
  int i,sum=0;
  for(i=0;i
    sum=sum+a[i];
  printf("\nThe Sum of the given numbers is %d",sum);
 }

OUTPUT
 [nehru@localhost cexam]$ cc funcarrsum.c
[nehru@localhost cexam]$ ./a.out
Enter the limit:
5
Enter the elements:
10 20 30 40 50

The Sum of the given numbers is 150[nehru@localhost cexam]$











9)      Write a C program to find the sum of digits of a number using function


/*TO FIND THE SUM OF DIGITS OF A NUMBER USING FUNCTION */
#include
void sum(int);
main()
{
 int no;
 printf("Enter your number\n");
 scanf("%d",&no);
 sum(no);
}

void sum(int num)
{
 int a,sum=0;
 while(num>0)
 {
  a=num%10;
  sum=sum+a;
  num=num/10;
 }
 printf("Sum=%d",sum);
}


OUTPUT

[nehru@localhost cexam]$ cc funsumdigit.c
[nehru@localhost cexam]$ ./a.out
Enter your number
12345
Sum=15
[nehru@localhost cexam]$











10)  Write a C program to find the sum of square of a individual digits of a number using function

/*TO FIND THE SUM OF SQUARE OF A NUMBER USING FUNCTION*/
#include
void sum(int);
main()
{
 int no;
 printf("Enter your number\n");
 scanf("%d",&no);
 sum(no);
}

void sum(int num)
{
 int a,sum=0;
 while(num>0)
 {
  a=num%10;
  sum=sum+(a*a);
  num=num/10;
 }
 printf("Sum of square of a number is %d",sum);
}



OUTPUT

[nehru@localhost cexam]$ cc funcsumsquare.c
[nehru@localhost cexam]$ ./a.out
Enter your number
12345
Sum of square of a number is 55











11)  Write a C program to check whether the given number and its reverse are same,  using function

#include
void reverse(int);
main()
{
 int no;
 printf("Enter the Number:\n");
 scanf("%d",&no);
 reverse(no);
}
void reverse(int num)
{
 int n,digit,rev=0;
 n=num;
 while(num>0)
 {
  digit=num%10;
  rev=rev*10+digit;
  num=num/10;
 }
 if(n==rev)
  printf("The given number %d and its reverse are same",n);
 else
  printf("The given number %d and its reverse are not same",n);
}

OUTPUT


[nehru@localhost cexam]$ cc revnofunc.c
[nehru@localhost cexam]$ ./a.out
Enter the Number:
12345
The given number 12345 and its reverse are not same











12)  Write a C program using pointers to swap two numbers


/* SWAP USING POINTERS */
#include
main()
{
 int a,b,*x,*y,t;
 x=&a;
 y=&b;
 printf("Enter the values for a and b:\n");
 scanf("%d%d",&a,&b);
 printf("\nBefore swapping\n");
 printf("The value of a is %d\n",*x);
 printf("The value of b is %d\n",*y);
 t=*x;
 *x=*y;
 *y=t;
 printf("\nAfter swapping\n");
 printf("The value of a is %d\n",*x);
 printf("The value of b is %d\n",*y);
}



OUTPUT


[nehru@localhost cexam]$ cc swapointer.c
[nehru@localhost cexam]$ ./a.out
Enter the values for a and b:
22 44

Before swapping
The value of a is 22
The value of b is 44

After swapping
The value of a is 44
The value of b is 22
[nehru@localhost cexam]$




13)  Write a C program to find the sum of cube of individual digits of a number


/*TO FIND THE SUM OF SQUARE OF A NUMBER USING FUNCTION*/
#include
void sum(int);
main()
{
 int no;
 printf("Enter your number\n");
 scanf("%d",&no);
 sum(no);
}

void sum(int num)
{
 int a,sum=0;
 while(num>0)
 {
  a=num%10;
  sum=sum+(a*a*a);
  num=num/10;
 }
 printf("Sum of Cube of a number is %d",sum);
}


OUTPUT

[nehru@localhost cexam]$ ./a.out
Enter your number
153
Sum of Cube of a number is 153[nehru@localhost cexam]$ cc funcsumcube.c
[nehru@localhost cexam]$ ./a.out
Enter your number
123
Sum of Cube of a number is 36[nehru@localhost cexam]$









14)  Write a C program using DMA to perform linear search

/*DMA TO PERFORM LINEAR SEARCH*/
#include
main()
{
 int i,j,n,*a,temp,x;
 printf("Enter total number of items--->");
 scanf("%d",&n);
 a=(int *)malloc(n*sizeof(int));
 printf("\nEnter the elements one by one:\n");
 for(i=0;i
   scanf("%d",&a[i]);
 printf("\nEnter the value to be searched:");
 scanf("%d",&x);
 for(i=0;i
 {
  if(x==a[i])
    {
    printf("\n%d is located at location %d",x,i+1);
    exit(0);
    }
 }
printf("\n the given number is not found");
}

OUTPUT

[nehru@localhost cexam]$ cc dmalinearserch.c
[nehru@localhost cexam]$ ./a.out

Enter total number of items--->5
Enter the elements one by one:
22 33 44 55 66

Enter the value to be searched:11
 the given number is not found

[nehru@localhost cexam]$ cc dmalinearserch.c
[nehru@localhost cexam]$ ./a.out

Enter total number of items--->5
Enter the elements one by one:
22 33 44 55 66
Enter the value to be searched:33
33 is located at location 2
15)   Write a C program to perform file copy operation

/* FILE COPY OPERATION FROM ONE FILE TO ANOTHER FILE DIRECTLY */

#include
main()
 {
  char ch;
  FILE *fp1,*fp2;
  fp1=fopen("one.txt","r");
  fp2=fopen("two.txt","w+");
  printf("The contents of one.txt file is\n");
  while((ch=getc(fp1))!=EOF)
  {
   putc(ch,fp2);
   printf("%c",ch);
  }
  printf("\n");
  printf("the file one.txt is copied into the file two.txt\n");
  printf("the contents of two.txt file is \n\n");
  fseek(fp2,0L,0);
  while((ch=getc(fp2))!=EOF)
  {
   printf("%c",ch);
  }
  fclose(fp1);
  fclose(fp2);
 }

OUTPUT

[nehru@localhost cexam]$ cc copyfile.c
[nehru@localhost cexam]$ ./a.out
The contents of one.txt file is
hai how are you
how is your life
what are you doing

the file one.txt is copied into the file two.txt
the contents of two.txt file is

hai how are you
how is your life
what are you doing
[nehru@localhost cexam]$


OR

/* FILE COPY OPERATION AS GIVEN ARGUMENT USING MAIN FUNCTION */


#include
main(int a, char *av[2])
 {
  char ch;
  FILE *fp1,*fp2;
  fp1=fopen(av[1],"r");
  fp2=fopen(av[2],"w");
  while((ch=getc(fp1))!=EOF)
  {
   putc(ch,fp2);
  }
  fclose(fp1);
  fclose(fp2);
 }

OUTPUT:

[civilbatch1@localhost valliammai]$cc copy.c
[civilbatch1@localhost valliammai]$cat>a
Vel Tech Engineering College
[civilbatch1@localhost valliammai]$./a.out a b
[civilbatch1@localhost valliammai]$cat b
Vel Tech Engineering College




Previous
Next Post »