Code Competitions

Coding competition

Tuesday, 28 May 2013

program to find the postion of digit in a number

#include<iostream.h>
#include<conio.h>
void main()
{
    long int n,d,pos=0,m,flag=0,flag1=0;
    clrscr();
    cout<<"enter the number n ";
    cin>>n;
    cout<<"\nenter the digit d ";
    cin>>d;
    m=n;
    while(m>0)
    {
        m=m/10;
        pos++;
    }
    while(n>0)
    {
        m=n%10;
        n=n/10;
        if(m==d)
        {
            flag=1;
            break;
        }
        flag1++;
    }
    if(flag==1)
    {
    cout<<"\nelement is present at location "<<pos-flag1;
    }
    else
    {
        cout<<"element is not present in number ";
    }
    getch();
}

OUTPUT:
 

No comments:

Post a Comment