Wednesday 12 September 2012

To find the sum of individual digits of a given number


Description:
Sum of the individual digits means adding all the digits of a number
Ex: 123      sum of digits is 1+2+3=6

Algorithm:

Step 1: start
Step 2: read n
Step 3: initialize the s=0
Step 4: if n<0 goto Step 7
Step 5: if n!=0 goto Step 6  else goto step 7
Step 6: store n%10 value in p
                        Add p value to s
                        Assign n/10 value to n
                        Goto Step 5
Step 7: print  s
Step 8:stop
 
Program:
#include<stdio.h>
main()
 {
   int n,s,p;
   clrscr();
   printf("enter the vaue for n:\n");
   scanf("%d",&n);
   s=0;
   if(n<0)
   printf("The given number is not valid");
   else
    {
      while(n!=0)     /* check the given value =0 or not */
            {
              p=n%10;
              n=n/10;
              s=s+p;
            }
      printf("sum of individual digits is %d",s);
    }
  getch();
}
Output:
1.Enter the value for n: 333
    Sum of individual digits is 9
2.Enter the value for n: 4733
    Sum of individual digits is 17
3. Enter the value for n: -111
   The given number is not valid
Conclusion : The program is error free


VIVA QUESATIONS:


1) What is the mean of sum of the individual digits?
                        Ans: Sum of the individual digits means adding each digit in a number
                 
                  2)  What is positive integer?
Ans:   if the integer value is grater than zero then it is called positive integer
3) Define preprocessor ?
Ans: Before compiling a process called preprocessing is done on the source code by a program called the preprocessor.

No comments:

Post a Comment