Program to print Sum of all numbers in a string - My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Saturday, January 25, 2020

Program to print Sum of all numbers in a string - My CS Tutorial

C program to print or find sum of numbers that are available in a string.
Sum of numbers in a string in C.
In this tutorial we will learn how to write a c program to find or print all numbers that are available in a string and sum of these numbers.
In the first user will enter a string with numerical digits through the keyboard. After it compiler will print the numbers and sum of all numbers.


C program code


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int i,j=0,temp,sum=0,n;
char str1[100],str2[100]={0};
clrscr();

printf("\n ENTER A STRING OR SENTENCE WITH NUMERICAL DIGITS:\n ");
printf("\n ");
gets(str1);

for(i=0;i<strlen(str1);i++)
{
    if(str1[i]>='0'&&str1[i]<='9')
    {
        str2[j]=str1[i];
        j++;
    }

}
temp=atoi(str2);
printf("\n THAT DIGITS PRESENT IN THIS STRING IS:\n %d\n",temp);
while(temp>0)
{
    n=temp%10;
    sum+=n;
    temp=temp/10;
}
printf("\n SUM OF ALL DIGITS THAT PRESENT IN THIS STRING IS: \n %d\n",sum);

getch();
}


OUTPUT:





Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment