Thursday, August 26, 2010

unix shell programming

# Program to count no of occurence of vowel charaters
echo Enter String
read string
Len=`expr length $string`
count=0
i=1
while [ $i -le $Len ]
do
char=`expr substr $string $i 1`
if [ $char = a -o $char = e -o $char = i -o $char = o -o $char = u ]
then count=`expr $count + 1`
fi
i=`expr $i + 1`
done
echo Vowel Occurence = $count
Output
——
Enter String
jishnu
Vowel Occurence = 2



# Program to count number of occurance of a character in a string

echo "Enter String"
read string
echo "Enter Character"
read char
count=0
i=1
len=`expr length $string`
echo "String Length : $len"
while [ $i -le $len ]
do
cchar=`expr substr $string $i 1`
if [ $cchar = $char ]
then
count=`expr $count + 1`
fi
i=`expr $i + 1`
done
echo "No of Occurence of '$char' : $count"

Output
——-

Enter String
asdasasdasd
Enter Character
a
String Length : 11
No of Occurence of 'a' : 4
# Program to find grade,score & modulus of marks using 5 marks of a student
echo "Enter 5 Marks"
read m1 m2 m3 m4 m5
total=`expr $m1 + $m2 + $m3 + $m4 + $m5`
echo "Total Marks = $total"
percent=`expr $total \* 100 / 500`
echo "Percentage of Marks = $percent"
if [ $percent -lt 40 ]
then
echo "D Grade"
elif [ $percent -ge 40 -a $percent -lt 65 ]
then
echo "C Grade"
elif [ $percent -ge 65 -a $percent -lt 75 ]
then
echo "B Grade"
elif [ $percent -ge 75 -a $percent -lt 90 ]
then
echo "A Grade"
else
echo "A++ Grade"
fi
OUTPUT
——
Enter 5 Marks
67 98 89 78 89
Total Marks = 421
Percentage of Marks = 84
A Grade
Enter 5 Marks
56 67 45 63 45
Total Marks = 276
Percentage of Marks = 55
C Grade
# Program to find fibonacci numbers within a given range
echo Enter Limit
read n
f1=0
f2=1
echo $f1
echo $f2
n=`expr $n – 2`
while [ $n -ne 0 ]
do
f3=`expr $f1 + $f2`
f1=$f2
f2=$f3
echo $f3
n=`expr $n – 1`
done
OUTPUT
——
Enter Limit
6
0
1
1
2
3
5
# Menu driven program to perform arithmetic operations
echo "Enter 2 Numbers"
read a b
opt=0
while [ $opt -ne 6 ]
do
echo "Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit"
read opt
case $opt in
1)
echo "$a +"
echo "$b"
echo "—-"
echo $a + $b|bc;;
2)
echo "$a -"
echo "$b"
echo "—-"
echo $a – $b|bc;;
3)
echo "$a *"
echo "$b"
echo "—-"
echo $a \* $b|bc;;
4)
echo "$a /"
echo "$b"
echo "—-"
echo $a / $b|bc;;
5)
echo "$a %"
echo "$b"
echo "—-"
echo $a % $b|bc;;
6)
;;
*)
echo "wrong option"
esac
done
OUTPUT
——
Enter 2 Numbers
4.4 2.2
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
1
4.4 +
2.2
—-
6.6
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
2
4.4 -
2.2
—-
2.2
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
3
4.4 *
2.2
—-
9.6
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
4
4.4 /
2.2
—-
2
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
5
4.4 %
2.2
—-
0
Menu
1. Additon
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
6
# Program to check whether given number is armstrong number or not
echo "Enter Number"
read num
sum=0
item=$num
while [ $item -ne 0 ]
do
rem=`expr $item % 10`
cube=`expr $rem \* $rem \* $rem`
sum=`expr $sum + $cube`
item=`expr $item / 10`
done
if [ $sum -eq $num ]
then
echo "$num is an Amstrong Number"
else
echo "$num is not an Amstrong Number"
fi
OUTPUT
——
Enter Number
123
123 is not an Amstrong Number
Enter Number
1
1 is an Amstrong Number
Enter Number
153
153 is an Amstrong Number


# shell program to create a menu which displays
# the output for the curresponding options
# 1. Current Directory
# 2. Todays date
# 3. List of users logged in
echo Menu
echo 1. Current Directory
echo 2. Todays date
echo 3. List of users logged in
echo enter your choice :
read opt
case $opt in
1)
echo "Current Directory"
pwd;;
2)
echo "Todays Date"
date;;
3)
echo "Users Logged in"
who;;
*)
echo "wrong option"
esac

Output
————
Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
1
Current Directory
/rhome/it07022/s4/pe

Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
2
Todays Date
Mon Feb 16 11:06:05 EST 2009

Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
3
Users Logged in
it07022 :0 2009-02-16 10:09
it07022 pts/0 2009-02-16 10:42 (:0.0)

Menu
1. Current Directory
2. Todays date
3. List of users logged in
enter your choice :
4
wrong option
# Shell program to find sum of squares of 10 numbers
sum=0
i=0
while [ $i -ne 10 ]
do
echo "Enter Number"
read num
sum=`expr $sum + $num \* $num`
i=`expr $i + 1`
done
echo Sum of squares of numbers = $sum
Output
——-
Enter Number
1
Enter Number
2
Enter Number
3
Enter Number
4
Enter Number
5
Enter Number
6
Enter Number
7
Enter Number
8
Enter Number
9
Enter Number
10
Sum of squares of numbers = 385


# Shell program to find sum of odd numbers in a series of 10 numbers
sum=0
i=0
while [ $i -ne 10 ]
do
echo "Enter Number"
read num
if [ `expr $num % 2` -ne 0 ]
then
sum=`expr $sum + $num`
fi
i=`expr $i + 1`
done
echo Sum of odd numbers = $sum

Output
————-

Enter Number
1
Enter Number
2
Enter Number
3
Enter Number
4
Enter Number
6
Enter Number
8
Enter Number
3
Enter Number
1
Enter Number
1
Enter Number
1
Sum of odd numbers = 10


# Shell program to reverse a given number
echo "Enter Number"
read num
num2=0
while [ $num -ne 0 ]
do
num2=`expr $num % 10 + $num2 \* 10`
num=`expr $num / 10`
done
echo $num2

Output
————

Enter Number
1234
4321

Enter Number
32453
35423


# Program to find sum of digits of a number
echo “Enter Number”
read num
sum=0
while [ $num -ne 0 ]
do
sum=`expr $sum + $num % 10`
num=`expr $num / 10`
done
echo Sum of Digits = $sum

# Output
# Enter Number
# 234
# Sum of Digits = 9

# Enter Number
# 23
# Sum of Digits = 5


Shell Program check whether a given number is odd or even

echo "Enter Number"
read num
if [ $num -eq 0 ]
then
echo "Neither Even nor Odd"
elif [ `expr $num % 2` -eq 0 ]
then
echo "Number is Even"
else
echo "Number is Odd"
fi

#Output

# Enter Number
# 2
# Number is Even

# Enter Number
# 3
# Number is Odd

# Enter Number
# 0
# Neither Even nor Odd

c plus plus notes

Introduction to Object Oriented Programming

The term Object Oriented Programming is a relatively new concept in the world of programming languages. Earlier the only style of programming was known as Sequential or Linear or Procedural Programming. Every program has two parts: Code and Data. The code part consists of the statements that are executed to perform the job. The data part consists of the variables which hold the necessary values to perform the job. In Sequential programming style more emphasis is placed on the code part and less on the data part. The code statements are executed one by one, sequentially, meaning that you can execute statement number n if you have already executed statement number n-1, and the next statement to execute is statement number n+1.

In Object Oriented Programming the style is changed. Basically, more emphasis is placed on the data part and emphasis placed on the code part is secondary. Everything you have to consider must be viewed as an object. The definition of an object is like the following.

An object is a collection of a set of data and a set of code. The data part is called attributes (formed as variables) and the code part is called functions (formed as executable statements). The relationship between the attributes and the functions is such that if a function is executed, the values of some attribute can be accessed or changed. In other words, to access or change the value of some attribute, we must execute the proper function.

Let us explain with an example. Consider ‘chair’ to be an object. We can say that any chair in the world must have a set of attributes like height, width, breadth, colour, material, weight, price and location. To validate our case we illustrate three different chairs of different types.

Chair1 Chair2 Chair3
Height 3 ft 2.5 ft 3.5 ft
Width 2 ft 2.5 ft 3 ft
Breadth 3 ft 2.25 ft 3.5 ft
Colour Red Green Blue
Material Wood Plastic Iron
Weight 5 kg 2 kg 10 kg
Price 500/- 250/- 1000/-
Location Centre West North East

The attribute set described above defines the data part of the three chair objects. You can see that all the three chairs have the identical attribute set. It may happen that the value within the individual attribute could be different for the three objects. For example the three objects have different values for the material attribute, but all of them have the material attribute in their set of attributes.



But as they are objects, the chairs must also have a set of functions (meaning what we can do with these chairs). The set of functions could be like the following.

Buy ( );
Sell ( );
Repair ( );
Paint ( );
Move ( );

As you can understand, if we execute these functions, then one or some of the attribute values will be accessed or changed. For example, to Buy ( ) or Sell ( ) the chair, its price attribute will be changed. To Repair ( ) a chair, we may change its dimensions, weight or material. To Paint ( ) a chair its colour property will be changed. To Move ( ) a chair, we change its location.

The concept of Class

If some objects are found to have a similar set of attributes and a similar set of functions, then these objects can be grouped together into a class. A class is a collection of declaration of the attribute variables and the definition of the functions. Once the class has been defined, we can create as many objects of a class as we need just as we declare variables of a data type. In the light of a class, an object can also be defined as an instance or occurrence of a class.

We may declare the CHAIR class as having declared variables like height, width, breadth, colour, material, weight, price and location and defined the functions like Buy ( ), Sell ( ), Repair ( ), Paint ( ) and Move ( ). Now, in order to declare a new object Chair4, all we need to do is write the following statement

CHAIR Chair4; (Equivalent to int n;)

And the job is done.

Each object has its own space in memory where it can store its own attribute set, because each object is expected to have a unique attribute set of its own. However, there is no need to store separate copies of the functions for each individual objects, as the coding for the functions need not be changed for each object.

Writing the actual program in an object oriented system is fairly easy. All we have to do is to execute the appropriate function with the appropriate object. For example, if we want to sell Chair2, then we may write

Chair2.Sell ( );

And if we want to move Chair3 then we may write

Chair3.Move ( );

Below we briefly mention some of the important features of the object oriented programming. We shall discuss then in detail in the later chapters.



Encapsulation

Encapsulation is the technique of putting together the data part (attributes) and the code part (functions) that can operate on the data part into the same single container called a class.


Data Abstraction or Data hiding

This is the technique of making some part of the class accessible to the outside world while keeping some sensitive part inaccessible. Generally the attributes of a class are defined with a keyword called private, which means that these attributes cannot be accessed from outside the class by any means. The functions of the class are generally declared with a keyword called public, which means that they can be accessed from outside the class if called using an object of the same class.


Polymorphism

This means the quality of the same thing to exist in different forms. This is an integral feature of object oriented programming that is not found in sequential programming.


Inheritance

This is also another unique feature of object oriented programming. This means the creation of a new class out of an existing class. Inheritance supports the concept of code reuse and saves a lot of time and effort to create a new class.

Monday, November 23, 2009

some extereme helpfull c program n notes



/*** Program to Print Transpose of a Matrix ***/


#include


main()
{
int a[10][10], i, j, row, col;


printf("\nEnter no. of rows & columns: ");
scanf("%d %d", &row, &col);


printf("\nEnter elements of Matrix:\n");
for (i=0; i for (j=0; jscanf("%d", &a[i][j]);


printf("\n\nElements of Matrix:\n\n");
for (i=0; i {
for (j=0; jprintf("\t%d", a[i][j]);
printf("\n\n");
}


printf("\n\nTranspose of Matrix:\n\n");
for (i=0; i {
for (j=0; jprintf("\t%d", a[j][i]);
printf("\n\n");
}


getch();
}


program to multiply to matrix

#include


void main()
{
int a[10][10], b[10][10], c[10][10], i, j, k, r1, r2, c1, c2;


back:
printf("\nEnter no. of rows and columns of Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("\nEnter no. of rows and columns of Matrix B: ");
scanf("%d %d", &r2, &c2);


if (c1 != r2)
{
printf("\n\nMultiplication is not possible\n");
goto back;
}


printf("\n\nEnter elements of Matrix A:\n");
for (i=0; i for (j=0; jscanf("%d", &a[i][j]);


printf("\nEnter elements of Matrix B:\n");
for (i=0; i for (j=0; jscanf("%d", &b[i][j]);


printf("\n\nElements of Matrix A:\n\n");
for (i=0; i {
for (j=0; jprintf("\t%d", a[i][j]);
printf("\n\n");
}


printf("\n\nElements of Matrix B:\n");
for (i=0; i {
for (j=0; jprintf("\t%d", b[i][j]);
printf("\n\n");
}


for (i=0; i for (j=0; j {
c[i][j] = 0;
for (k=0; kc[i][j] = c[i][j] + a[i][k] * b[k][j];
}


printf("\n\nMultiplication of Matrices:\n\n");
for (i=0; i {
for (j=0; jprintf("\t%d", c[i][j]);
printf("\n\n");
}


getch();
}

perfect no check

void main()
{
int i,no,sum=0;

clrscr();

printf("Enter no : ");
scanf("%d",&no);

for (i=1; i {
if (no%i==0)
sum=sum+i;
}

if (no==sum)
printf("\n %d is a Perfect Number",no);
else
printf("\n %d is not a Perfect Number",no);

getch();
}