Вы находитесь на странице: 1из 27

1.

find the immediate next prime number


for eg: input:9
output:11
hint was given: input will not exceed 100

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,flag=0,out;
clrscr();
printf("enter the num\n");
scanf("%d",&n);
for(i=n+1;i<=100;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("next prime is:%d",i);
break;
}
}
getch();
}
2. find the number of even numbers repeating
for eg: input:{2,2,3,3,4,4}
output:2
check for this input also ....output for {8,8,4,4,4,2,5,5} is 2
even numbers 8 and 4 are repeating.. so output is 2
#include<stdio.h>
#include<conio.h>

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<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
count=0;
if(a[i]%2==0)
{
for(j=i+1;j<n;)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
count++;
}
}
}
if(count>0)
{
op++;
}
}
printf("%d",op);
getch();
}

3. descending order
#innclude<stdio.h>

#include<conio.h>
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<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
5. input ll be a number
find the product of odd digits and even digits seperately
sum the products
if all the digits are odd or even add 1 to the sum

#include<stdio.h>
void main()
{
int n,prode=1,prodo=1,rem,sum=0;
printf("enter the no.\n");
scanf("%d",&n);
while(n)
{
rem=n%10;
if(rem%2==0)
prode*=rem;
else
prodo*=rem;
n/=10;

}
sum=prode+prodo;
printf("sum is:%d",sum);
}
6. reverse the number
#include<stdio.h>
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<stdio.h>
#include<conio.h>
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]);

}
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=-1
BR2: if input2<0 op=-3
BR3:if any element in input1<0 op=-2

#include<stdio.h>
#include<conio.h>
void main()
{
int n,out[20],i,flag=0,value,k=0,a[20];
clrscr();
printf("enter size\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
{
flag=1;
}
}
printf("enter the value\n");
scanf("%d",&value);
if(n<0)
{
out[0]=-3;
printf("%d",out[0]);
}
else if(flag==1)
{
printf("%d",out[0]=-2);
}
else if(value<0)

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

for(i=1;i<n;i+=2)
{
if(a[i]==value)
{
out[k]=a[i-1];
k++;
}
}

for(i=0;i<k;i++)
printf("\t%d",out[i]);
}
getch();
}

9.
10. input:{24,45,11,14,23}
ouput:{20,1,6}
hint: find the odd numbers in the array{45,11,23}
multiply the digits of each number
4*5=20
1*1=1
2*3=6

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[20],i,prod,rem,k=0,out[20];
printf("enter array size\n");
scanf("%d",&n);
printf("enter the elements\n");

for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
prod=1;
if(a[i]%2!=0)
{
while(a[i])
{
rem=a[i]%10;
prod=prod*rem;
a[i]=a[i]/10;
}

out[k]=prod;
k++;
}
}
for(i=0;i<k;i++)
printf("\t%d",out[i]);
getch();
}
11. write a program to print second largest divisor of the given number
eg:input=21 divisors are {1,3,7,21}
output=7

nclude<stdio.h>
#include<conio.h>
void main()
{
int n,i;
printf("enter the no.\n");
scanf("%d",&n);
for(i=n-1;i>0;i--)
{
if(n%i==0)
{
printf("2nd largest divisor is:\t%d",i);
break;

}
}
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<stdio.h>
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}

#include<stdio.h>
#include<conio.h>
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<n;i++)
scanf("%d",&a[i]);
printf("enter the second array\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
fg=0;
for(j=0;j<n;j++)
{
if(b[i]==a[j])

{
fg=1;
break;
}
}
if(fg==0)
{
c[k]=b[i];
k++;
}
}
for(i=0;i<k;i++)
printf("%d\n",c[i]);
getch();
}
14.
(SAME AS 13)

15. wap to find strong number or not


br=if element is -ve op=-1

void main()
{
int n,t,rem,fact,sum=0;
printf("enter the no\n");
scanf("%d",&n);
t=n;
if(n<0)
printf("-1");
else
{
while(n)
{
rem=n%10;
fact=1;

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<stdio.h>
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<0||u<0)
printf("%d",out=-1;
else
{
for(i=l;i<=u;i++)
{
t=i;
sum=0;
while(t!=0)
{
rem=t%10;

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<stdio.h>
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<0||n%2==0)
out[0]=-2;
else
{
printf("enter elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
{
flag=1;
out[0]=-1;
}}
if(flag==0)
{
for(i=0;i<mid;i++)
{
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;

}
}
}
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
19. del -ve elemts in array nd sum positive ones
br: store -2 if all are -ve elemts,store -1 if size -ve

#include<stdio.h>
void main()
{
int n,out,i,a[20],flag=0,sum=0,k;
printf("enter array size\n");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
if(n<0)
out=-1;
else
{
for(i=0;i<n;i++)
{
if(a[i]>=0)
{
flag=1;
break;
}
}
if(flag==0)
out=-2;
else
{
for(i=0;i<n;)
{
if(a[i]<0)
{
for(k=i;k<n;k++)

{
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 input1<0 op=-1
input1>32767 op=-2

#include<stdio.h>
//#include<conio.h>
void main(){
int a,b,c,d,op;
//clrscr();
printf("enter four nums");
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a<=b && a<=c && a<=d)
op=a;
else if(b<=a && b<=c && b<=d)
op=b;
else if(c<=a && c<=b && c<=d)
op=c;
else
op=d;

printf("%d",op);
}
24. Add multiples of "input1" till input2 and store in output1
input1=3
input2=15
output1=45

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c=0,i ;
clrscr();
printf("enter two nums");
scanf("%d %d",&a,&b);
for(i=a;i<=b;i=i+a)
c=c+i;
printf("%d",c);
getch();
}

25. input1={1,3,2,3,3,6,4,6,5,9}(id,value,id,....)
input2=10(array size)
add values next to odd id and find average.
output1=18/3=6
br:input1[i]<0 op=-1
input2<0 op=-2
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100],sum=0,avg;
clrscr();
printf("enter the range");
scanf("%d ",&n);
for(i=0;i<n;i++)
scanf("%d",a[i]);
for(i=1;i<n;i=i+4)

{
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]<0 op=-1
input2<0 op=-2

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,op[100],a[100],b,c,d;
clrscr();
printf("enter the range\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter the first three values\n");
scanf("%d",&b);
c=b*100;
d=c+100;
for(i=0;i<n;i++){
if(a[i]>=c && a[i]<d)
{
op[k]=a[i]-c;
k++;
}
}
for(i=0;i<k;i++)
printf("%d",op[i]);

getch();
}

28. extract the even digit


for eg: input:4512
output:42
input:1315
ouput:0

#include<stdio.h>
#include<conio.h>
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
{

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<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[100],op,max=0,dif;
clrscr();
printf("enter the range");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
dif=0;
dif=a[i]-a[i+1];
dif=dif*-1;
if(dif>max){
op=dif;
max=dif;
}
}
printf("%d",op);
getch();
}

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

#include<conio.h>
void 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<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;)
{
if(a[i]<0)
{
for(k=i;k<n;k++)
a[k]=a[k+1];
n--;
}
else
i++;

}
for(i=0;i<n;i++)

{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("resultant array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
38. .consecutive elements for a given array
example:
input2=4[array size]
input1={2,4,7,8}
output1={3,5,8,9}

include<stdio.h>
void main()
{
int n,a[20],i,out[20];
printf("enter size of array\n");
scanf("%d",&n);
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
out[i]=a[i]+1;
for(i=0;i<n;i++)
printf("%d\t",out[i]);
}

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<stdio.h>
#include<conio.h>
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;i<=u;i++)
{ t=i;
sum=0;
while(t>0)
{
rem=t%10;
sum=sum+(rem*rem*rem);
t=t/10;
}
if(sum==i)
max=i;
}
printf("largest armstr. is:%d",max);
getch();
}

42. second smallest number in a array


#include<stdio.h>
#include<conio.h>
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<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
x: if(a[0]==a[1])
{
for(j=1;j<n;j++)
a[j]=a[j+1];
goto x;
}
printf("2nd smallest elemnet is %d",a[1]);
getch();
}

43.
(SAME AS 11)

44.
(SAMe AS 22)

45. number of numbers repeating in an array

#include<stdio.h>
#include<conio.h>
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<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
{
count=0;
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
n--;
count=count+1;

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

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

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


for(i=0;i<n;i++)
printf("\t %d",a[i]);*/
getch();
}
46. store the repeated elements in a array and sort them in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,k,count,l=0,out[20],t;
clrscr();
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++)
{
count=0;
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
n--;
count=count+1;

else
j++;
}
// printf("%d repeats %d times \n",a[i],count);
if(count>0)
{
out[i]=a[i];
l++;
}
}
for(i=0;i<l;i++)
{
for(j=i+1;j<l;j++)
{
if(out[i]>out[j])
{
t=out[i];
out[i]=out[j];
out[j]=t;
}
}
}
printf("resultant array is\n");
for(i=0;i<l;i++)
printf("%d\t",out[i]);
getch();
}

47. .factorial for a given number. if number is greater than 7 output should be -1
#include<stdio.h>
void main()
{
int n,fact=1,op;
printf("enter number\n");
scanf("%d",&n);
if(n>7)
op=-1;
else

{
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<stdio.h>
#include<conio.h>
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<n;i++)
scanf("%d",&a[i]);
j=1;
for(i=0;i<=5;i=i+2)
{
b[i]=j;
j++;
}
for(i=0;i<n;i++)
{
if(a[i]<10)
one=one+1;
else if(a[i]<100)
two=two+1;
else
three=three+1;

}
b[1]=one;
b[3]=two;
b[5]=three;
for(i=0;i<=5;i++)
printf("\t%d",b[i]);
getch();
}

Вам также может понравиться