Write a shell program to find the sum of alternate digits in a given 5-digit number | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Write a shell program to find the sum of alternate digits in a given 5-digit number

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-29-2006
banta banta is offline
Registered User
 
Join Date: Oct 2006
Last Activity: 29 October 2006, 4:29 PM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Write a shell program to find the sum of alternate digits in a given 5-digit number

Hi Can any one please post the answer for the above program..................

Last edited by banta; 10-29-2006 at 04:28 PM.. Reason: Wrong question
Sponsored Links
    #2  
Old 10-29-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 2 September 2014, 12:07 AM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts
banta.sh 28462

banta.sh:

Code:
#!/bin/ksh

a="${1}"

typeset -i i=1

while (( i <= 5 ))
do
  ((num=$(echo "${a}" | cut -c "${i}") ))
  if (( i == 1 )); then
     printf "$num"
  else
     printf "+$num"
  fi
  ((sum=sum + num))
  ((i=i+2))
done

printf "=${sum}\n"

Sponsored Links
    #3  
Old 10-29-2006
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
Python alternative


Code:
#!/usr/bin/python
a = 12345
b = list(str(a))
print sum( map(int,b[::2]) )

output:

Code:
9

    #4  
Old 10-30-2006
rajnikanth.1912 rajnikanth.1912 is offline
Registered User
 
Join Date: Oct 2006
Last Activity: 6 June 2007, 5:50 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
You need to initialise the variables num and sum

#!/bin/ksh

a=12346
b=`expr length $a`
echo " length : $b"
typeset -i i=1
num=0
sum=0
while (( i <= $b ))
do
((num=$(echo "${a}" | cut -c "${i}") ))
if (( i == 1 )); then
printf "$num"
else
printf "+$num"
fi
((sum=sum + num))
((i=i+2))
done
printf "=${sum}\n"
~

Answer
-------
length : 5
1+3+6=10
Sponsored Links
    #5  
Old 10-30-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
AFK
 
Join Date: Dec 2004
Last Activity: 28 July 2014, 9:39 AM EDT
Location: UK
Posts: 2,351
Thanks: 0
Thanked 6 Times in 6 Posts
Er... obvious example of homework? Thread closed.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find filenames with three digits and add zeros to make five digits Buzzman25 Shell Programming and Scripting 2 01-18-2012 09:53 PM
Use match() in nawk to find digits in number shekhar2010us Shell Programming and Scripting 6 08-16-2011 12:03 PM
Write an automated shell program(s) that can create, monitor the log files and report the issues for itian2010 Shell Programming and Scripting 1 08-12-2010 06:11 AM
Find out 2^n+1 , where n is a 3 digit number agrawal.prachi Programming 7 08-09-2010 10:01 PM
Urgent!! How to write a shell program to execute command to access internet? firebirdonfire UNIX for Dummies Questions & Answers 2 04-26-2001 11:16 AM



All times are GMT -4. The time now is 02:44 AM.