Thursday, September 15, 2011

#write a program to print all the prime numbers from 1 to 300 (hit use nested loops,break and continue)

#!/bin/bash
#set -x
#write a program to print all the prime numbers from 1 to 300 (hit use nested loops,break and continue)
start_number=1
end_number=300
for((i=$start_number;i<=$end_number;i++))
do
if [ $i -eq 1 ]
then
continue
else
for((j=2;j<$i;j++))
do
a=`expr $i % $j`
if [ $a -eq 0 ]
then
break
else
if [ $j -eq `expr $i - 1` ]
then
echo -e "$i\c"
echo -e " \c"
fi
fi
done
fi
done

Output:
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293

#############################################################################################
get the time of execution

start_time=$(date +%s)
.
.
.
finish_time=$(date +%s)
echo "Time duration: $((finish_time - start_time)) secs."

#############################################################################################
#!/bin/bash
#Write a program to generate alla combinations of 1,2,3 using for loops
n1=1
n2=2
n3=3
for((i=1;i<=3;i++))
do
for((j=1;j<=3;j++))
do
for((k=1;k<=3;k++))
do
echo -e "$i$j$k"
done
done
done
echo
output:
11
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333

##############################################################################################

#!/bin/bash
#Write a shell script which deletes all lines containing the word unix in the files supplied as arguments to this shell script
sed -i '/unix/d' $1

############################################################################################

2 comments:

  1. Write a C program to print all prime number between 1 to 300 using for loop.

    Code of given problem:

    int main()
    {
    int a,b;
    printf("List of prime numbers between 1 to 300:\n");
    printf("%d\t",a=2);
    for(a=1;a<=300;a++)
    {
    for(b=2;b<=a-1;b++)
    if(a%b==0)
    break;
    else if(b==a-1)
    {
    printf("%d\t",a);
    }
    }
    return 0;
    }

    Very informative blog!
    Please take some time to visit my blog @
    Top 10 programming examples in C

    Thanks!

    ReplyDelete
  2. Thank you so much 😌😎

    ReplyDelete