Skip to content

Instantly share code, notes, and snippets.

@canokay
Created January 14, 2019 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canokay/29fec9c6cf30e78496d002baf7e9fb98 to your computer and use it in GitHub Desktop.
Save canokay/29fec9c6cf30e78496d002baf7e9fb98 to your computer and use it in GitHub Desktop.

Algoritmik Bash problemler-2:

  1. Desimal sayıyı binary ceviren programı yazın:
#!/bin/bash
read -p "sayi girin" sayi
while [ $sayi -gt 0 ];
do
kalan=$((sayi%2))
sayi=$((sayi/2))
sonuc=$kalan$sonuc
done 
echo $sonuc
  1. 1den girilen sayıya kadar olan sayıların karelerin yazan programı yazın.
#!/bin/bash
read -p "Sayi giriniz" sayi
toplam=0
for (( i=1;i<=$sayi;i++ )); {
toplam=$(($toplam+($i*$i)))
}
echo $toplam

3.Klavyeden kilo ve boy(ondalıklı metre cinsi 1.65gibi) alıp, Vucut indexini (BMI) hesaplayan scripti yazın. BMI=kilo/(boy*boy)

#!/bin/bash
# Body Mass Index (BMI) calculator –Vücut index Hesaplama
#kilo boy okut
# ondalıklı girmek için nokta (.) isareti kullanın
read -p "kilonuzu girin" kilo
read -p "boyunuzu girin" boy
#bmi hesapla
bmi=$(scale=2; echo "($kilo/($boy*$boy))" | bc)
#echo $bmi
#karsılastırma yap
if [ $bmi -le 18 ]; then
   echo "cok zayif"
elif [ $bmi -le 24 ]; then
   echo "normal"
elif [ $bmi -le 29 ]; then
   echo "egzersiz zamanı"
else
   echo "takma kafana"
fi

4.Aşağıdaki program parçası ne yapar?

#!/bin/bash

5.Ekrandan girilen sayı kadar rasgele sayı üreten programı yazınız.
#!/bin/bash
read -p "kac sayý ureteceksiniz" sayi
i=0
while [ $i -le $sayi ];
do 
echo 
$RANDOM
rasgele=$rasgele"   "$RANDOM
i+=1
done
echo $rasgele

6.Desimal (0-200 arası) sayıyı roma rakamına ceviren programı yaın.

#!/bin/bash
# Arabic number to Roman numeral conversion
# Range: 0 − 200
# Usage: roman number−to−convert
LIMIT=200
E_ARG_ERR=65
E_OUT_OF_RANGE=66
if [ −z "$1" ]
then
 echo "kullanım: `basename $0`  sayi"
 exit $E_ARG_ERR
fi 
num=$1
if [ "$num" −gt $LIMIT ]
then
 echo "Sınır dışı!"
 exit $E_OUT_OF_RANGE
fi 
to_roman () # fonksiyon yazma
{
number=$1
factor=$2
rchar=$3
let "remainder = number − factor"
while [ "$remainder" −ge 0 ]
do
 echo −n $rchar
 let "number −= factor"
 let "remainder = number − factor"
done 
return $number
}
to_roman $num 100 C   #fonksiyon cagrılıyor
num=$?
to_roman $num 90 LXXXX     	#fonksiyon cagrılıyor
num=$?                                     	 # fonksiyondan donen değerleri okur
to_roman $num 50 L              	 #fonksiyon cagrılıyor  
num=$?                      		# fonksiyondan donen değerleri okur
to_roman $num 40 XL           	#fonksiyon cagrılıyor
num=$?			# fonksiyondan donen değerleri okur
to_roman $num 10 X           	 #fonksiyon cagrılıyor
num=$?			# fonksiyondan donen değerleri okur
to_roman $num 9 IX             	#fonksiyon cagrılıyor
num=$?			# fonksiyondan donen değerleri okur
to_roman $num 5 V            	 #fonksiyon cagrılıyor
num=$?			# fonksiyondan donen değerleri okur
to_roman $num 4 IV            	 #fonksiyon cagrılıyor
num=$?			# fonksiyondan donen değerleri okur
to_roman $num 1 I                	#fonksiyon cagrılıyor

7.Faktoriel hesabı yapan programı yazın.

#!/bin/bash
		echo "Enter n: "
		read n                               
		output=1
		i=1
 		while [ $i -le $n ]                  
		do
			output=`expr $output \* $i
			i=`expr $i + 1`                
		done                                 
		echo "The factorial of $n is $output

8.Arguman olarak girilen sayıların toplamını bulan scripti yazın , program su sekilde calısır: hesapla 1 2 3

#!/bin/bash
sum=0;
echo -n "Girilen  $# sayiların toplamı :  "
while [ $1 ]
do
        sum=`expr $sum + $1`
        shift
done
echo "$sum"

9.Girilen sayının negative veya pozitif olduğunu yazan programı yazın.

#! /bin/bash
echo -n “enter a number: “
read number
if [ $number -lt 0 ] # if test $number –lt 0
then                 # if ! ([ $number –gt 0 ] || [ $number –eq 0 ])
 echo negative
elif [ $number -eq 0 ]
then
 echo zero
else
 echo positive
fi

10.Fahrenhayti(Fahrenheit) santigrada(celcius) ceviren programı yazın.

# Get the value to convert
read -p “Fahrenheit derecesi girin: “ FTEMP
# ceviri yap
CTEMP=”(5*(FTEMP-32) )/9”
Echo  “ Santigrad derecesi:” “$CTEMP

11.Klavyeden “Anapara,faizoranı ve yıl” girilecek sekilde, Mortgage hesabı yapan programı yazın. Formul: (( odeme = (anapara*oran)/(1.-pow(1.+oran,-ay)) ))

#Mortgage Calculator
read -r 'faiz oranı girin:' oran
read -r 'anapara girin:' anapara
read -r 'yil miktarı girin' yil
(( ay = yil*12 ))
(( oran /= 1200. ))
(( odeme = (anapara*oran)/(1.-pow(1.+rate,-ay)) ))
printf "\ Aylık odemeler \t%8.2f\n\n" "$odeme" 
print '\tYıllar   Bakiye'
print '\t======   ======='
for   	(( ay=0; anapara > 0; ay++))
do	(( anapara *= (1.+oran) ))
        (( anapara -= odeme ))
	if      (( ((ay+1)%12) == 0 ))
	then    printf "\t%d\t%8.2f\n" ay/12 "$anapara"
	fi
done

12.Iki sayının maximumunu bulan program yazın.

#!/bin/bash
# max.sh: Sayıların Maximumu .

if [ -z "$2" ]      #parametre kontrolu
then
  echo “kullanım hatası, max 34 67 seklinde kullan
fi

if [ "$1" -eq "$2" ]
then
  return $EQUAL
else
  if [ "$1" -gt "$2" ]
  then
    return $1
  else
    return $2
  fi
fi

13.Buble sort yapan program.

#!/bin/bash
# SCRIPT: bubblesort.sh
# LOGIC:
# Bubble sort is a simple sorting, it works by repeatedly stepping
# through the list to be sorted, comparing two items at a time and
# swapping them if they are in the wrong order. If you are sorting
# the data in Ascending order, at the end of the first pass, the
# "heaviest" element has move to bottom. In the second pass, the
# comparisons are made till the last but one position and now second
# largest element is placed at the last but one position. And so
# forth.
#
#####################################################################
#                      Define Functions Here                        #
#####################################################################

printnumbers()
{
echo ${ARRAY[*]}
#You can also use bellow code
#for ((i=0;i<count;i++))
#do
#echo -n " ${ARRAY[i]} "
#done
}
exchange()
{
temp=${ARRAY[$1]}
ARRAY[$1]=${ARRAY[$2]}
ARRAY[$2]=$temp
}

sortnumbers()
{
for (( last=count-1;last>0;last--))
do
    for((i=0;i<last;i++))
    do
      j=$((i+1))
      if [ ${ARRAY[i]} -gt ${ARRAY[j]} ]
      then
       exchange $i $j
      fi
    done
done
}

#####################################################################
#                       Variable Initialization                     #
#####################################################################

echo "Enter Numbers to be Sorted"
read -a ARRAY
count=${#ARRAY[@]}

#####################################################################
#                       Main Script Starts Here                     #
#####################################################################

echo "--------------------------------------------------------------"
echo "Numbers Before Sort:"
printnumbers
echo
sortnumbers
echo "Numbers After Sort: "
printnumbers
echo "--------------------------------------------------------------"



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment