If you have any queries please leave a message here
Your Message
×


SHELL PROGRAMMING


OPERATORS

OpetatorC, C++, Java ProgrammingShell Programming
Greater than
>
-gt
Less than
<
-lt
Greater than or Equal to
>=
-ge
Less than or Equal to
<=
-le
Equal to
==
-eq
Not equal to
!=
-ne
AND
&&
-a
OR
||
-o
NOT
!
!






SIMPLE IF-ELSE

C, C++ ProgrammingShell Programming
Syntax :

if(condition)
Statement ;
else
Statement ;



Example : Input a number and check whether the number is odd or even.

#include<stdio.h>
void main() {
int n;
printf("Enter the number...");
scanf("%d",&n);
if(n%2==0)
printf("Even Number");
else
printf("Odd Number");
}
Syntax :

if test condition
then
Statement
else
Statement
fi



Example : Input a number and check whether the number is odd or even.

echo "Enter the number..."
read n
if test `expr $n % 2` -eq 0
then
echo "Even Number"
else
echo "Odd Number"
fi


or


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






IF-ELSE LADDER

C, C++ ProgrammingShell Programming
Syntax :

if(condition)
Statement ;
else
if(condition)
Statement ;
else
if(condition)
Statement ;



Example : Input a marks out of 100 and calculate grade.

#include<stdio.h>
void main() {
int n;
printf("Enter the marks...");
scanf("%d",&n);
if(n>100 || n<0)
printf("Invalid Marks");
else
if(n>=90 && n<=100)
printf("Grade A+");
else
if(n>=75 && n<90)
printf("Grade A");
else
if(n>=60 && n<75)
printf("Grade B");
else
if(n>=50 && n<60)
printf("Grade C");
else
if(n>=40 && n<50)
printf("Grade D");
else
if(n<40)
printf("Fail");
}
Syntax :

if test condition
then
Statement
elif test condition
then
Statement
elif test condition
then
Statement
fi



Example : Input a marks out of 100 and calculate grade.

echo "Enter the marks..."
read n
if test $n -gt 100 -o $n -lt 0
then
echo "Invalid Marks"
elif test $n -ge 90 -a $n -le 100
then
echo "grade A+"
elif test $n -ge 75 -a $n -lt 90
then
echo "grade A"
elif test $n -ge 60 -a $n -lt 75
then
echo "grade B"
elif test $n -ge 50 -a $n -lt 60
then
echo "grade C"
elif test $n -ge 40 -a $n -lt 50
then
echo "grade D"
elif test $n -lt 40
then
echo "Fail"
fi






SWITCH CASE

C, C++ Programming
Example : Celcius to farenheit and farenheit to celcius calculation

#include<stdio.h>
void main()
{
int c,f,choice;
printf("\n Press 1 for Celcius to Farenheit Conversion");
printf("\n Press 2 for Farenheit to Celcius Conversion");
printf("\n Enter your choice...");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the temperature in celcius...");
scanf("%d",&c);
f=((9*c)+160)/5;
Printf("\n Farenheit Temperature = %d",f);
break;

case 2:
printf("\n Enter the temperature in farenheit...");
scanf("%d",&f);
c=((5*f)-160)/9;
Printf("\n Celcius Temperature = %d",c);
break;

default:
Printf("\n Wrong Input");
}
}
Shell Programming
Example : Celcius to farenheit and farenheit to celcius calculation

echo "Press 1 for Celcius to Farenheit Conversion"
echo "Press 2 for Farenheit to Celcius Conversion"
echo "Enter your choice....."
read choice
case $choice in

1)
echo "Enter the temperature in celcius..."
read c
temp=$((9 * $c + 160))
f=$(($temp / 5))
echo "Farenheit Temperature = $f"
;;

2)
echo "Enter the temperature in farenheit..."
read f
temp=$((5 * $f - 160))
c=$(($temp / 9))
echo "Farenheit Temperature = $c"
;;

*)
echo "Wrong Input"
esac






FOR LOOP

C, C++ ProgrammingShell Programming
Syntax :

for(Initialization; Condition; Increment/Decrement)
{
Statement ;
}



Example : Calculate the sum of first n number.

#include<stdio.h>
void main()
{
int n,i,sum=0;
printf("Enter the number...");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Answer = %d",sum);
}
Example : Calculate the sum of first n number.

echo "Enter the number..."
read n
sum=0
for i in {1..$n}
do
sum=`expr $sum + $i`
done
echo "Answer = $sum"


or


echo "Enter the number..."
read n
sum=0
for((i=1;i<=n;i++))
do
sum=`expr $sum + $i`
done
echo "Answer = $sum"






WHILE LOOP

C, C++ ProgrammingShell Programming
Syntax :

Initialization ;
while(Condition)
{
Statement ;
Increment/Decrement ;
}



Example : Reverse a number.

#include<stdio.h>
void main()
{
int n,r,num=0;
printf("Enter the number...");
scanf("%d",&n);
while(n>0)
{
r=n%10;
num=((num*10)+r);
n=n/10;
}
Printf("Answer = %d",num);
}
Syntax :

Initialization
while test Condition
do
Statement
Increment/Decrement
done


or

Initialization
while [ Condition ]
do
Statement
Increment/Decrement
done



Example : Reverse a number.

echo "Enter the number..."
read n
num=0
while test $n -gt 0
do
r=$(expr $n % 10)
num=$(($num * 10 + $r))
n=$(expr $n / 10)
done
echo "Answer = $num"






EXAMPLES

C, C++ ProgrammingShell Programming
1) Input a number from the user and determine whether the number is prime or not.

#include<stdio.h>
void main()
{
int n,i,mid,count=0;
printf("Enter the number...");
scanf("%d",&n);
mid=n/2;
for(i=2;i<=mid;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==0)
Printf("Prime Number");
else
Printf("Not Prime");
}
1) Input a number from the user and determine whether the number is prime or not.

echo "Enter the number....."
read n
mid=`expr $n / 2`
count=0
for i in `seq 2 $mid`
do
if test `expr $n % $i` -eq 0
then
count=`expr $count + 1`
fi
done
if test $count -eq 0
then
echo "Prime Number"
else
echo "Not Prime"
fi
2) Input a number from the user and determine whether the number is perfect number or not.

#include<stdio.h>
void main()
{
int n,i,mid,sum=0;
printf("Enter the number...");
scanf("%d",&n);
mid=n/2;
for(i=2;i<=mid;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
Printf("Perfect Number");
else
Printf("Not a Perfect Number");
}
2) Input a number from the user and determine whether the number is perfect number or not.

echo "Enter the number....."
read n
mid=`expr $n / 2`
for((i=1;i<=$mid;i++))
do
if [ `expr $n % $i` -eq 0 ]
then
sum=`expr $sum + $i`
fi
done
if [ $sum -eq $n ]
then
echo "Perfect Number"
else
echo "Not a Perfect Number"
fi
3) Fibonacci Series

#include<stdio.h>
void main()
{
int n,a,b,c,i;
printf("Enter the number of terms...");
scanf("%d",&n);
printf("Enter the first number...");
scanf("%d",&a);
printf("Enter the second number...");
scanf("%d",&b);
printf("%d \t %d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf("\t %d",c);
a=b;
b=c;
}
}
3) Fibonacci Series

echo "Enter the number of terms..."
read n
echo "Enter the first number..."
read a
echo "Enter the second number..."
read b
echo "$a"
echo "$b"
for i in {3..$n}
do
c=`expr $a + $b`
echo "$c"
a=$b
b=$c
done
4)Bubble Sort

#include<stdio.h>
void main()
{
int a[5],i,j,temp;
for(i=0; i<5; i++)
{
printf("\n Enter the number...");
scanf("%d",&a[i]);
}
for(i=1;i<5;i++)
{
for(j=0;j<5-i;j++)
{
if(a[j]>a[j+1]
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
printf("\t%d",a[i]);
}
4)Bubble Sort

for i in {0..4}
do
echo "Enter the number..."
read a[$i]
done
for i in {1..4}
do
for j in {0..`expr 4 - $i`}
do
if test ${a[j]} -gt ${a[`expr $j + 1`]}
then
temp=${a[j]}
${a[j]}=${a[`expr $j + 1`]}
${a[`expr $j + 1`]}=$temp
fi
done
done
for i in {0..4}
do
echo "${a[i]}"
done




RELATED TOPIC



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us