-->

program to search location of a number in a list

using codeblocks
---------------------------------------------------------------
//program to print all locations of a number  array. //comment of program
#include <stdio.h>            //header file
int main()                   //main function
{
    int *p;                   //pointer declaration
    int a[20],i,k,num;          //variable declaration
    int total;
    printf("enter total number\n");
    scanf("%d",&total);            // gets total value of array.
    p=a;                       //stores base address
    for(i=0;i<total;i++)      //runs from 0 to one less than total
    {
        scanf("%d",p+i);   //gets value for given address. This input goes upto last value of address
    }
    printf("values are\n");
    for(i=0;i<total;i++)
    {
        printf("%d\n",*(p+i));//displays its value with the help of address
    }
    printf("enter a number to be searched\n");
    scanf("%d",&num);                    //gets a number to be searched
    for(i=0;i<total;i++)
    {
            if(*(p+i)==num)               //comparison being done  with its value
            {
                 printf("num=%d and its location=%d\n",num,i);//prints value
            }
    }
    return 0;
}

------------------------------------------------------------------------------------------
using turboc++
----------------------------------------------------------------------
//program to print all locations of a number  array. //comment of program
#include <stdio.h>            //header file
#include<conio.h>
int main()                   //main function
{
    int *p;                   //pointer declaration
    int a[20],i,k,num;          //variable declaration
    int total;
    printf("enter total number\n");
    scanf("%d",&total);            // gets total value of array.
    p=a;                       //stores base address
    for(i=0;i<total;i++)      //runs from 0 to one less than total
    {
        scanf("%d",p+i);   //gets value for given address. This input goes upto last value of address
    }
    printf("values are\n");
    for(i=0;i<total;i++)
    {
        printf("%d\n",*(p+i));//displays its value with the help of address
    }
    printf("enter a number to be searched\n");
    scanf("%d",&num);                    //gets a number to be searched
    for(i=0;i<total;i++)
    {
            if(*(p+i)==num)               //comparison being done  with its value
            {
                 printf("num=%d and its location=%d\n",num,i);//prints value
            }
    }
   getch();
    return 0;
}

No comments:

Post a Comment