2. Sum of Individual Digits

2. Sum of Individual Digits 

Aim:

To write a C program to calculate the sum of individual digits of a positive integer entered by the user.


Program :
#include<stdio.h>

#include<conio.h>

void main()

{

    int n, sum=0;

    printf("Enter a +ve integer : "); // enter a integer value

    scanf("%d",&n);

    while(n>0)  // checks the condition

    {

        sum=sum+n%10;   // sum + remainder value

        n=n/10;

 

    }

    // prints the sum of individual digits

    printf("Sum of individual digits of a positive integer is %d",sum); 

No comments:

Post a Comment

P24CA3P5 - Core Practical V: SMART DEVICES PROGRAMMING LAB

  Core Practical V: SMART DEVICES PROGRAMMING LAB SEMESTER: III                                 CODE: P24CA3P5 CREDITS: 2                  ...