48 Questions Answers (1) - C lang

download 48 Questions Answers (1) - C lang

of 27

Transcript of 48 Questions Answers (1) - C lang

  • 8/10/2019 48 Questions Answers (1) - C lang

    1/27

    1. find the immediate next prime number

    for eg: input:9

    output:11

    hint was given: input will not exceed 100

    #include

    #include

    void main(){

    int n,i,j,flag=0,out;

    clrscr();

    printf("enter the num\n");

    scanf("%d",&n);for(i=n+1;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    2/27

    void main()

    {

    int n,i,a[100],j,k,op=0,count=0;

    clrscr();

    printf("enter the range ");scanf("%d",&n);

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    3/27

    #include

    void main()

    {

    int n,a[20],i,j,t;

    printf("enter the size of array\n");scanf("%d",&n);

    printf("enter the elements\n");

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    4/27

    }

    sum=prode+prodo;

    printf("sum is:%d",sum);

    }

    6. reverse the number

    #include

    void main(){

    int n,rem,rev=0;

    printf("enter the number\n");

    scanf("%d",&n);

    while(n){

    rem=n%10;

    rev=rev*10+rem;

    n=n/10;

    }printf("reversed no. is:%d",rev);

    }

    7. .decimal to bimary

    #include

    #include

    int main()

    {

    int a[20];

    int dec,i=0;

    printf("Enter the decimal number to find its binary number\n");

    scanf("%d",&dec);

    while(dec>0){

    a[i]=dec%2;i++;

    dec=dec/2;

    }

    printf("Binary number of %d is = ",dec);

    for(int j=i-1;j>=0;j--)

    {printf("%d",a[j]);

  • 8/10/2019 48 Questions Answers (1) - C lang

    5/27

    }

    getch();

    }

    8. input1:{1,2,2,4,3,4}-{key,value,key....}input2:6(array size)

    input3:4(value)

    output:2,3( print all the keys for which the value is given in input3)

    BR1: if the input3 is not in input1 store op=-1BR2: if input2

  • 8/10/2019 48 Questions Answers (1) - C lang

    6/27

    {

    printf("%d",out[0]=-1);

    }

    else

    {

    for(i=1;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    7/27

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    8/27

    }

    }

    getch();

    }

    12. wap to find factorial of individual digits in a given input number and store it

    in an array

    eg:123

    output={1,4,9}

    #include

    void main()

    {int n,k=0,rem,fact,out[20];

    printf("enter the no.\n");

    scanf("%d",&n);

    while(n!=0)

    {rem=n%10;

    fact=1;

    while(rem!=0)

    {

    fact=fact*rem;rem--;

    }

    out[k]=fact;

    k++;

    n=n/10;

    }

    k--;

    for(;k>=0;k--)

    printf("%d\t",out[k]);}

    13. wap to print only non intersecting elements from the two given arrays. size of

    both input arrays is equal.

    eg:input1={1,2,3,4,5}

    input2={2,3,6,7,8}output={1,4,5,6,7,8}

  • 8/10/2019 48 Questions Answers (1) - C lang

    9/27

    #include

    #include

    void main()

    {

    int n,a[100],b[100],c[100],i,j,k=0,flag,fg;clrscr();

    printf("enter the range\n");

    scanf("%d",&n);

    printf("enter the first array\n");for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    10/27

    {

    fg=1;

    break;

    }

    }

    if(fg==0)

    {c[k]=b[i];

    k++;

    }

    }

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    11/27

    while(rem!=0)

    {

    fact=fact*rem;

    rem--;

    }sum+=fact;

    n=n/10;

    }

    if(t==sum)printf("strong");

    else

    printf("nt strong");

    }

    }

    16.

    17. armstrong between two ranges and find largest one in that.br: if any range -ve store -1

    #include

    void main(){

    int l,u,i,t,sum,rem,out;

    printf("enter the lower limit\n");

    scanf("%d",&l);

    printf("enter the upper limit\n");

    scanf("%d",&u);

    if(l

  • 8/10/2019 48 Questions Answers (1) - C lang

    12/27

    sum=sum+rem*rem*rem;

    t=t/10;

    }

    if(sum==i)

    out=i;

    }

    printf("highest armstrong no. in given range is :%d",out);

    }}

    18.reverse the array elements in array,if range is odd using mid=n/2 formula

    br: store -2 if elemnt is even or range -ve,store -1 if any one element is negative

    #include

    void main()

    {

    int n,mid,out[20],a[20],temp,flag=0,i;printf("enter array size\n");

    scanf("%d",&n);

    mid=n/2;

    if(n

  • 8/10/2019 48 Questions Answers (1) - C lang

    13/27

    }

    }

    }

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    14/27

    {

    a[k]=a[k+1];

    }

    n--;}

    else

    {

    sum=sum+a[i];i++;

    }

    }

    out=sum;

    }}

    printf("op is :%d",out);

    }

    20.

    21.

    22. smallest of four numbers.Br: If input132767 op=-2

    #include

    //#include

    void main(){

    int a,b,c,d,op;

    //clrscr();printf("enter four nums");

    scanf("%d %d %d %d",&a,&b,&c,&d);if(a

  • 8/10/2019 48 Questions Answers (1) - C lang

    15/27

    printf("%d",op);

    }

    24. Add multiples of "input1" till input2 and store in output1

    input1=3input2=15

    output1=45

    #include

    #include

    void main()

    {

    int a,b,c=0,i ;clrscr();

    printf("enter two nums");

    scanf("%d %d",&a,&b);

    for(i=a;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    16/27

    {

    sum=sum+a[i];

    count++;

    }

    avg=sum/3;printf("%d",avg);

    getch();

    }

    26. input1={12365,12396,12125,12256}

    input2=4(arraysize)

    input3=123(find this pattern in array and print next numbers in output array)

    output1={65,96}br:input1[i]

  • 8/10/2019 48 Questions Answers (1) - C lang

    17/27

    getch();

    }

    28. extract the even digit

    for eg: input:4512

    output:42

    input:1315ouput:0

    #include

    #include

    void main()

    {

    int n,r,flag=0,sum=0,su=0;

    printf("enter number");

    scanf("%d",&n);

    while(n!=0){

    r=n%10;

    if(r%2==0)

    {

    sum=sum*10+r;

    flag=1;

    }

    n=n/10;

    }if(flag==0)

    {

    printf("%d",0);

    }else

    {

  • 8/10/2019 48 Questions Answers (1) - C lang

    18/27

    while(sum!=0)

    {

    r=sum%10;

    su=su*10+r;

    sum=sum/10;}

    printf("%d",su);

    }

    getch();

    }

    29. find the diff between consecutive elements in array . assign the largest

    difference to the output

    eg: ip={4,10,6,5,3}

    op=6;(store the difference between consecutive elements in a seperate array)

    #include

    #include

    void main()

    {int i,n,a[100],op,max=0,dif;

    clrscr();

    printf("enter the range");

    scanf("%d",&n);

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    19/27

    30.

    (REFER 22)

    31. & 33. &35.(look intersecting element in array soln.)

    37. remove the negative elements in a array and sort the remaining elements

    #includevoid main()

    {

    int a[20],n,j,i,k,t;

    clrscr();

    printf("enter the size of array\n");scanf("%d",&n);

    printf("enter the elements\n");

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    20/27

    {

    for(j=i+1;ja[j])

    {t=a[i];

    a[i]=a[j];

    a[j]=t;

    }}

    }

    printf("resultant array is\n");

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    21/27

    39. input3 must be the size of two arrays input1 and input2 compare the two

    arrays and store the dissimilar elements in the output array.

    example:

    input3=4[array size]

    input1={2,4,7,8}input2={3,4,8,9}

    output1={2,7,3,9}

    (SAME AS NON-INTERSECTING)

    40.

    (SSAME AS 8)

    41. find the largest armstrong number among the given input range.

    example: range is, input1 =300 between input2=500

    output1=407

    #include

    #include

    void main()

    {int l,u,i,t,rem,sum=0,max=0;

    printf("enter lowe followed by upper range\n");

    scanf("%d %d",&l,&u);

    for(i=l;i0)

    {

    rem=t%10;sum=sum+(rem*rem*rem);

    t=t/10;}

    if(sum==i)

    max=i;

    }

    printf("largest armstr. is:%d",max);getch();

    }

  • 8/10/2019 48 Questions Answers (1) - C lang

    22/27

    42. second smallest number in a array

    #include

    #include

    void main(){

    int n,i,j,a[20],temp;

    clrscr();

    printf("enter the size\n");scanf("%d",&n);

    printf("enter the elemenets\n");

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    23/27

    44.

    (SAMe AS 22)

    45. number of numbers repeating in an array

    #include#include

    void main()

    {

    int a[20],n,i,j,k,count,c=0;

    clrscr();printf("enter the size of array\n");

    scanf("%d",&n);

    printf("enter the elements\n");

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    24/27

    c++;

    }

    printf("no. of numbers repea is:%d",c);

    /*printf("array after deleting duplicate is:\n");

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    25/27

    else

    j++;

    }

    // printf("%d repeats %d times \n",a[i],count);if(count>0)

    {

    out[i]=a[i];

    l++;}

    }

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    26/27

    {

    while(n>0)

    {

    fact=fact*n;

    n--;}

    op=fact;

    }

    printf("fact is %d",op);}

    48. print the digits count in the following format.

    example:

    input1={1,23,2,34,456,67}output1={1,no of single digits,2, no of 2 digits and so on}

    output1={1,2,2,3,3,1}

    #include#include

    void main()

    {

    int n,i,a[100],b[100],one=0,two=0,three=0,j;

    clrscr();printf("enter the range");

    scanf("%d",&n);

    for(i=0;i

  • 8/10/2019 48 Questions Answers (1) - C lang

    27/27

    }

    b[1]=one;

    b[3]=two;

    b[5]=three;

    for(i=0;i