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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 05:28 PM.. Reason: Wrong question
Sponsored Links
    #2  
Old Unix and Linux 10-29-2006
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 5 March 2015, 5:08 PM EST
Location: Foxborough, MA
Posts: 7,721
Thanks: 164
Thanked 612 Times in 573 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 Unix and Linux 10-29-2006
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
Python alternative


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

output:

Code:
9

    #4  
Old Unix and Linux 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 Unix and Linux 10-30-2006
blowtorch's Unix or Linux Image
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 10:53 PM
Use match() in nawk to find digits in number shekhar2010us Shell Programming and Scripting 6 08-16-2011 01: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 07:11 AM
Find out 2^n+1 , where n is a 3 digit number agrawal.prachi Programming 7 08-09-2010 11: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 12:16 PM



All times are GMT -4. The time now is 06:29 PM.