You are on page 1of 81

1.

Write a program to find factorial of a number

Br:

1) If number is negative store -1 into the output1 variable

2) If number is greater than 7 store -2 into the output1 variable

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1,op;
clrscr();
printf("\nEnter Number:");
scanf("%d",&n);
if(n<0)
{
op=-1;
}
else if(n>7)
{
op=-2;
}
else
{
for(i=1;i<=n;i++)
fact=i*fact;
// printf("Factorial of %d is %d",n,fact);
op=fact;
}
printf("\nOutput is %d",op);
getch();

}
2. Write a program to check for Armstrong number

1) If number is negative store -1 into the output1 variable

2) If number is greater than 3 digit store -2 into the output1 variable

3) If it is Armstrong number store 1 into the output1 variable

4) If it is not store store 0 into the output1 variable

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,num,op,count=0,a;
clrscr();
printf("\nEnter number:");
scanf("%d",&num);
n=num;
if(num<0)
{
op=-1;
}
else
{
while(n!=0)
{
n=n/10;
count++;
}
n=num;
if(count>3)
op=-2;
else
{
if(count==1)
op=1;
if(count==2)
op=0;
if(count==3)
{
while(n!=0)
{
a=n%10;
sum=sum+(a*a*a);
n=n/10;
}
op=1;

}
}
}
printf("%d",op);
getch();
}
3. Write a program to find number of digits in a given number(ex: for 345 no of digits is
3)

1) if number is negative store -1 into the output1 variable

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=0,op;
clrscr();
printf("\nEnter n:");
scanf("%d",&n);
if(n<0)
{
op=-1;
}
else
{
while(n!=0)
{
n=n/10;
a++;
}
op=a;
}
printf("\n%d",op);
getch();
}
4. Write a program to find sum of even numbers and odd numbers in an array and avg
them

(ex: ar{1,2,3,4,5,6} output1=(12+9)/2

1) If any of the array element is negative store -1 into the output1 variable

2) If the size of an array is negative store -2 into the output1 variable

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[20],i,op,flag=0,sum=0;
clrscr();
printf("\nEnter n:");
scanf("%d",&n);
if(n<0)
op=-2;
else
{
printf("\nEnter elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<0)
flag=1;
}
if(flag==1)
op=-1;
else
{
for(i=0;i<n;i++)
sum=sum+a[i];
op=sum/2;
}
}

printf("\n%d",op);
getch();
}
5. Given two input arrays, sort first array into ascending order and second array into
descending order
And multiply the first element of first array with last element of second array and second
element of first array with second last of the second array and so on..
ex:Input1{1,2,3,4,5} and input2={9,8,7,6,5} and output1{5,12,21,32,45}
Br:
1) If any element of input arrays is negative store -1 into the first element of the output
array
2) If size of an array is negative store -2 into the first element of the output array
#include<stdio.h>
#include<conio.h>
void main()
{
int t,i,j,a[10],b[10],n,output1,c[20];
clrscr();
printf("ENTER THE ARRAY SIXZE");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n ENTER THE %d ELEMENT", i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\n ENTER THE %d ELEMENT", i);
scanf("%d",&b[i]);
}

if(n<0)
{
output1=-2;
printf("The Ans is:%d",output1);
}
else if(a[i]<0 && b[i]<0)
{
output1=-1;
printf("The Ans is:%d",output1);
}
else if(i>0)
{
for(i=0;i<n;i++)
{
for(j=0;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("THE SORTED FIRST ARRAY: %d\n",a[i]);
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i]>b[j])
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("THE SORTED SECOND ARRAY: %d\n",b[i]);
}

//PRINTING THE OUTPUT


printf("\n THE OUTPUT AFTER MULTIPLICATION \n");
for(i=0;i<n;i++)
{
c[i]= a[i] * b[n-1-i];
printf("%d \t",c[i]);
}
}
getch();
}
6. Given two arrays, compare two arrays and store the greater element of the arrays into
output array.
Br:
1) If any of the element in an array is negative store -1 into first element of the output
array
2) If the size of an array is negative then store -2 into the first element of the output
array

#include<stdio.h>
#include<conio.h>
void main()
{
int c[80],n ,a[30],b[30],i,j,k,temp,output1;
clrscr();
printf("ENTER THE ARRAY SIZE");
scanf("%d",&n);
printf("ENTER THE ARRAY ELEMENTS A:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("ENTER THE ARRAY ELEMENTS B:");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
if(n<0)
{
output1=-2;
printf("The Ans is:%d",output1);
}
else if(a[i]<0)
{
output1=-1;
printf("The Ans is:%d",output1);
}
else if (n>0&&a[i]>0)
{
for(i=1;i<=n;i++)
{
if(a[i]>b[i])
c[i]=a[i];
else
c[i]=b[i];
}
for(i=1;i<=n;i++)
{
printf("The Output Is:%d\n",c[i]);
}
}
getch();
}
7. Write a program to multiply the positive numbers in an array
Br:
1) If the size of an array is negative store -2 into the output variable

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],output1,temp=1;
clrscr();
printf("ENTER THE ARRAY SIZE");
scanf("%d",&n);
printf("ENTER THE ELEMENTS IN THE ARRAY");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n<0)
{
output1=-2;
printf("the ans is:%d",output1);
}
else
if(n>0)
{
for(i=0;i<n;i++)
{
if(a[i]>0)
temp=temp*a[i];
}
printf("the output is %d\n",temp);
}
getch();
}

8. Write a program to find the sum of the even digits in a given number
Br:
1) If number is negative store -1 into the output variable
2) If number is greater than 32767 store -2 into the output variable

#include<stdio.h>
#include<conio.h>
void main()
{
int n,output1,a,sum=0;
clrscr();
printf("ENTER A NUMBER");
scanf("%d",&n);
if(n<0)
{
output1=-1;
printf("The number is -ve=%d",output1);
}
else if(n>32767)
{
output1=-2;
printf("The number great than =%d",output1);
}
else if(n>0&&n<32767)
{
while(n>0)
{
a=n%10;
n=n/10;
if(a%2==0)
sum=sum+a;
}
printf("the sum is %d",sum);
}
getch();
}

14. First element of the array is replaced with last element of the array.Input2 is the array
size.
For example
Input1[]={12,34,56,17,2}
Input2=5
Output1[]={2,17,56,34,12}
Take mid=input2/2;
BR
1.If array contain any negative elements store -1 to the first position of the output
array
2.Store -2 if the given array size is negative
3.Store -3 to the first position of the output array if the given array size is even
#include<stdio.h>
#include<conio.h>
int main()
{
int input1[40],output[40],input;
int i,j,k,count=0,temp;
clrscr();
printf("Enter the size::\n");
scanf("%d",&input);
if(input<0)
{
output[0]=-2;
printf("%d",output[0]);
}
else if(input%2==0)
{
output[0]=-3;
printf("%d",output[0]);
}
else
{
printf("Enter the element into the array:\n");
for(i=0;i<input;i++)
scanf("%d",&input1[i]);
for(i=0;i<input;i++)
if(input1[i]<0)
{
count++;
}
if(count==0)
{
for(i=0;i<input/2;i++)
{
temp=input1[i];
input1[i]=input1[input-i-1];
input1[input-i-1]=temp;

}
printf("The resultant array is::\n");
for(i=0;i<input;i++)
{
output[i]=input1[i];
printf("%d\n",output[i]);
}
}
else
{
output[0]=-1;
printf("%d\n",output[0]);
}
}
getch();
return 0;
}
15. Add the first element of the array input1 with the last element of the array input2..
Input3 is size of the array
Input1[]={21,23,41,4}
Input2[]={3,4,1,5}
Input3=4;
OUTPUT[]={26,24,45,7}
BR
1.Store -1 to the first position of the output array if any input array elements
contains negative
value
2.Store -2 to the first position of the output array if array size is negative

#include<stdio.h>
#include<conio.h>
int main()
{
int input1[40],input2[40],output[40],input;
int i,j,k,count=0;
clrscr();
printf("Enter the size::\n");
scanf("%d",&input);
if(input<0)
{
output[0]=-2;
printf("%d",output[0]);
}
else
{
printf("Enter the element into the first array:\n");
for(i=0;i<input;i++)
scanf("%d",&input1[i]);
printf("Enter the element into the 2nd array:\n");
for(i=0;i<input;i++)
scanf("%d",&input2[i]);
for(i=0;i<input;i++)
if(input1[i]<0 || input2[i]<0)
{
count++;
}
if(count==0)
{
for(i=0;i<input;i++)
{
for(k=0;k<input;k++)
{
output[k]=input1[i]+input2[i];
}
printf("%d\t",output[i]);
}
}
else
{
output[0]=-1;
printf("%d\n",output[0]);
}
}
getch();
return 0;
}
16. Remove the duplicate elements store it into the output array.Input2 is size of the
array
Input1[]={1,2,2,3,3}
Input2[]=5;
Output[]={1,2,3}
BR
1.Store -1 to the first position of the output array if any input array elements
contains negative
value
2.Store -2 to the first position of the output array if array size is negative
#include<stdio.h>
#include<conio.h>
int main()
{
int input1[25],input2,output[25],i,j,k,count=0;
clrscr();
printf("enter limit::\n");
scanf("%d",&input2);
if(input2<0)
{
output[0]=-1;
printf("%d",output[0]);
}
else
{
printf("enter elements::\n");
for(i=0;i<input2;i++)
scanf("%d",&input1[i]);
for(i=0;i<input2;i++)
if(input1[i]<0)
{
count++;
}
if(count==0)
{
for(i=0;i<input2;i++)
{
for(j=i+1;j<input2;)
{
if(input1[i]==input1[j])
{
for(k=j;k<input2;k++)
input1[k]=input1[k+1];
input2--;
}
else
j++;
}
}
printf("resultant array\n" );
for(i=0;i<input2;i++)
printf("\t%d",input1[i]);
}
else
{
output[0]=-2;
printf("%d",output[0]);
}
}
getch();
return 0;
}
17. Fahrenheit to Celsius conversion
BR-
1) if the number is negative, store -1 to output
#include<stdio.h>
#include<conio.h>
int out;
void temp(int fahren);
void main()
{
int fahren;
clrscr();
printf("enter the temperature in fahrenheit:");
scanf("%d",&fahren);
temp(fahren);
printf("output=%d",out);
getch();
}
void temp(int fahren)
{
if(fahren<0)
out=-1;
else
{
out=(fahren-32)*5/9;
}
}
18. Check whether the given number is perfect or not
BR-
1) if the number is perfect number, store 1 to output
2) If the number is not negative, store -1 to output
3) If the number is negative, store -2 to output

#include<stdio.h>
#include<conio.h>
void perfect(int input);
int out;
void main()
{
int input;
clrscr();
printf("enter the u want to check");
scanf("%d",&input);
perfect(input);
printf("output=%d",out);
getch();
}
void perfect(int input)
{
int sum=0,i=1;
if(input<0)
out=-2;
else
{
while(i<input)
{
if(input%i==0)
sum=sum+i;
i++;
}
if(sum==input)
out=1;
else
out=-1;
}
}
19. find the sum of cube of prime numbers upto n natural numbers(case study type)
BR-
1) if the limit(n) is negative, store -1 to output
2) if the n value is greater than 7, store -1 to output

#include<stdio.h>
#include<conio.h>
void primesum(int inut);
int out;
void main()
{
int input;
clrscr();
printf("enter the range");
scanf("%d",&input);
primesum(input);
printf("output=%d",out);
getch();
}
void primesum(int input)
{
int sum=8,i,flag,j;
if(input<0)
{
out=-1;
}
else if(input>7)
out=-1;
else
{
for(i=3;i<=input;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
sum=sum+(i*i*i);
}
}
out=sum;
}
}
20. count the number of multiples of 3 in the given array
BR-
1) if any array element is negative, store -1 to output
#include<stdio.h>
#include<conio.h>
void multiple(int input[],int n);
int out;
void main()
{
int input[100],i,n;
clrscr();
printf("enter the size of the array:");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&input[i]);
multiple(input,n);
printf("output=%d",out);
getch();
}
void multiple(int input[],int n)
{
int count=0,i,flag=0;
for(i=0;i<n;i++)
{
if(input[i]<0)
flag=1;
else
{
if(input[i]%3==0)
count++;
}
}
if(flag==1)
out=-1;
else
out=count;
}
21.Remove the repeated elements in the array
BR-
1) if any array element is negative, store -1 to the first position of the output array
2)if array size is negative, store -2 to the ouput

#include<Stdio.h>
#include<conio.h>
void removerepeat(int[],int);
int out[100],i,n;
void main()
{
int input1[100],input;
clrscr();
printf("Enter array range:");
scanf("%d",&input);
printf("Enter array elements:");
for(i=0;i<input;i++)
{
scanf("%d",&input1[i]);
}
removerepeat(input1,input);
printf("\nOutput array elements:\n");
for(i=0;i<n;i++)
printf("%d\t",out[i]);
getch();
}
void removerepeat(int input1[],int input)
{
int j,temp=0,k;
if(input<0)
{
out[0]=-2;
}
else
{
for(i=0;i<input;i++)
{
if(input1[i]<0)
{
temp=1;
}
for(j=i+1;j<input;)
{
if(input1[i]==input1[j])
{
for(k=j;k<input;k++)
{
input1[k]=input1[k+1];
}
input--;
}
else
{
j++;
}
}
}
if(temp==1)
{
out[0]=-1;
}
else
{
for(i=0;i<input;i++)
{
out[i]=input1[i];
}
}
}
if(input<0)
n=1;
else
n=input;
}
22. Remove the negative values from the array and sort the array
Eg:{20,-10,4,78}
Output:{4,20,78}

Business Rule:
Store -1 into the output variable if input2(array size) is negative value

#include<stdio.h>
#include<conio.h>
void delnegandsort(int[],int);
int i,output[100],n;
void main()
{
int input1[100],input;
clrscr();
printf("Enter array length:");
scanf("%d",&input);
printf("Enter array elements:");
for(i=0;i<input;i++)
{
scanf("%d",&input1[i]);
}
delnegandsort(input1,input);
printf("\noutput\n");
for(i=0;i<n;i++)
{
printf("%d\t",output[i]);
}

getch();
}
void delnegandsort(int input1[],int input)
{

int j,temp;
if(input<0)
{
output[0]=-1;
}
else
{
for(i=0;i<input;)
{
if(input1[i]<0)
{
for(j=i;j<input;j++)
{
input1[j]=input1[j+1];
}
input--;
}
else
i++;
}
for(i=0;i<input;i++)

{
for(j=i+1;j<input;j++)
{
if(input1[i]>input1[j])
{
temp=input1[i];
input1[i]=input1[j];
input1[j]=temp;
}
}

}
for(i=0;i<input;i++)
{
output[i]=input1[i];
}
}
if(input<0)
n=1;
else
n=input;
}
23.find sum of cubes of prime for n natural numbers
Business Rule:
Store -1 into the output variable if input value is negative number
Store -2 into the output variable if input value is exceeds 32676
#include<stdio.h>
#include<conio.h>
void sumofcubes(int,int);
long int out,k;
void main()
{
int input;
clrscr();
printf("Enter an number:");
k=(int)if(scanf("%d",&input));
sumofcubes(input,k);
printf("Sum of cubes of prime numbers:\n\tout=%ld",out);
getch();
}
void sumofcubes(int input,int k)
{
int i,j,flag,count=0;
long int sum=1;
if(k!=1)
{
out=-2;
}
else if(input<0)
{
out=-1;
}
else
{
for(i=2;count<input;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{ //printf("%d----%d\n",i,(i*i*i));
sum=sum+i*i*i;
count++;
}
}
out=sum;
}
}
24. From the given array find sum of multiples of 5 and calculate avg
Business Rule:
Store -1 into the output variable if any of the input element is negative
Store -2 into output variable if input2(array size)is negative value
#include<Stdio.h>
#include<conio.h>
void sumofmul5(int[],int);
float out;
int i;
void main()
{
int input1[100],input;
clrscr();
printf("ENter array size");
scanf("%d",&input);
printf("Enter array elements:");
for(i=0;i<input;i++)
scanf("%d",&input1[i]);
sumofmul5(input1,input);
printf("\noutput:");
printf("%.1f\t",out);
getch();
}
void sumofmul5(int input1[],int input)
{
int count=0,sum=0,temp;
if(input<0)
{
out=-2;
}
else
{
for(i=0;i<input;i++)
{
if(input1[i]<0)
{
temp=1;
}
if(input1[i]%5==0)
{
sum=sum+input1[i];
count++;
}
}
if(temp==1)
{
out=-1;
}
else
{
out=sum/count;
}
}
}

33. From the given input array remove the negative elements and then sort the
remaining array
elements, store that into the output array. Consider input1 is array elements and
input2 is array
size.
Eg:
Input1= {8,-6,12,79,57}
Input2=5
Output1= { 8,12,57,79}

Business Rules:
i) If the array size (input2) is negative then store -1 in to the output array.

#include<stdio.h>
#include<conio.h>
void main()
{
int input1[25],input2,output1[25],i,j,temp;
clrscr();
printf("enter the range");
scanf("%d",&input2);
if(input2<0)
{
output1[0]=-1;
printf("%d",output1[0]);
}
else
{
printf("enter array elements");
for(i=0;i<input2;i++)
scanf("%d",&input1[i]);
for(i=0;i<input2;i++)
{
if(input1[i]<0)
{
temp=input1[i];
input1[i]=input1[i+1];
input1[i+1]=temp;
}
}
for(i=0;i<input2-1;i++)
{
for(j=i+1;j<input2-1;j++)
{
if(input1[i]>input1[j])
{
temp=input1[i];
input1[i]=input1[j];
input1[j]=temp;
}
}
}
for(i=0;i<input2-1;i++)
output1[i]=input1[i];
for(i=0;i<input2-1;i++)
printf("\t%d",output1[i]);
}
getch();
}

34. Find the average of multiples of five up to N natural number. Consider input1 is the
N value.

Eg:
Input1= 15
Output1=10

Business Rules:
i) If the input1 is negative store -1 in to the output variable.
ii) If input1 is greater than 500 store -2 in to the output variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int input1,output1,i,sum=0,count=0;
clrscr();
printf("enter limit");
scanf("%d",&input1);
if(input1<0)
{
output1=-1;
printf("%d",output1);
}
else if(input1>500)
{
output1=-2;
printf("%d",output1);
}
else
{
for(i=1;i<=input1;i++)
{
if(i%5==0)
{
sum=sum+i;
count=count+1;
}
}
output1=sum/count;
printf("output1=%d",output1);
}
getch();
}
35. Search and remove the element from the given input array and then sort the
remaining array
elements, store that in to the output array. Consider input1 is array elements, input2
is array size
and input3 is the search element.

Eg:
Input1= {54, 26, 78, 32, 55}
Input2= 5
Input3=78

Ouput1= {26, 32, 54, 55}

Business Rules:
i) If any of the array element (input1) is negative store -1 into the output array.
ii) If the array size (input2) is negative store -2 into the output array.
iii) If the search element (input3) is not present in the array (input1) store -3 into
the output array.

#include<stdio.h>
#include<conio.h>
void main()
{
int input1[25],input2,input3,output1[25],i,temp,j,k=0,count=0;
clrscr();
printf("enter range");
scanf("%d",&input2);
if(input2<0)
{
output1[0]=-2;
printf("%d",output1[0]);
}
else
{
printf("enter array elements");
for(i=0;i<input2;i++)
scanf("%d",&input1[i]);
for(i=0;i<input2;i++)
if(input1[i]<0)
{
count++ ;
//output1[0]=-1;
//printf("%d",output1[0]);
//break;
}
if(count==0)
{
printf("enter element to be searched");
scanf("%d",&input3);
for(i=0;i<input2;i++)
{
if(input1[i]==input3)
{
k++;
temp=input1[i];
input1[i]=input1[i+1];
input1[i+1]=temp;
}
}
if(k>0)
{
for(i=0;i<input2-1;i++)
{
for(j=i+1;j<input2-1;j++)
{
if(input1[i]>input1[j])
{
temp=input1[i];
input1[i]=input1[j];
input1[j]=temp;
}
}
}
for(i=0;i<input2-1;i++)
printf("\t%d",input1[i]);
}
else
{
output1[0]=-3;
printf("%d",output1[0]);
}
}
else
{
output1[0]=-1;
printf("%d",output1[0]);
}
}

getch();
}
36. Convert the given binary number into the decimal number. Consider input1 is the
binary number.
Eg:
Input1=1001
Output1=9
Business Rules:
i) If the given input is not a binary value store -1 to the output variable.
ii) If the given input is greater than 11111 store -2 to the output variable.
#include<stdio.h>
#include<conio.h>
void main()
{
long int input1,output1=0,base=1,rem,count=0,n,d,k=0;
clrscr();
printf("enter number");
scanf("%ld",&input1);
if(input1>11111)
{
output1=-2;
printf("%ld",output1);
}
else
{
n=input1;
while(n!=0)
{
d=n%10;
k++;
if((d==0)||(d==1))
count++;
n=n/10;
}
if(k==count)
{
while(input1>0)
{
rem=input1%10;
output1=output1+rem*base;
input1=input1/10;
base=base*2;
}
printf("output1=%ld",output1);
}
else
{
output1=-1;
printf("output1=%ld",output1);
}
}
getch();
}
41. Given input array is input1[] and size of array is input3
Add the positive elements in array and store result in output1
Eg.
input1[]={2,-3,4,5,-9}
output1=11

Business Rule1: If the size of input array is negative ,then store -2 to output array
Business Rule2: If any element in input array is negative ,then store -1 to output
array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,sum=0,op;
clrscr();
printf("\n enter the limit of the arrays");
scanf("%d",&n);
if(n<0)
{
op=-2;
printf("\n %d",op);
}
else if(n>0)
{
printf("\n enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]>0)
{
sum=sum+a[i];
printf("\n %d",sum);
}
else
{
op=-1;
printf("\n %d",op);
}
}
}
getch();
}

42. Given input array is input1[] and size of array is input3


Identify the second largest element and store it to output1
input1[]={2,3,4,1,9}
output1=4
Business Rule1: If the size of input array is negative ,then store -2 to output array
Business Rule2: If any element in input array is negative ,then store -1 to output
array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,temp=0,op;
clrscr();
printf("\n enter the array limkit");
scanf("%d",&n);
if(n<0)
{
op=-2;
printf("\n %d",op);
}
else if(n>0)
{
printf("\n enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
op=-1;
printf("\n %d",op);
}
else if(a[i]>0)
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

}
printf("\n the second largest element is %d",a[n-2]);
}
}
getch();
}

43. Given input1 number ,


Find the sum of the square of prime number from 1 to N where N is input1
Eg. If input1 is 7
Output1=22+32+52+72
Store the result to output 1
Business rule1: if the input1 is negative then store -1 to output1
Business rule1: if the input1 > 500 then store -2 to output1

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n,sum=0,k=0,op;
clrscr();
printf("\n enter the limit ");
scanf("%d",&n);
if(n<0)
{
op=-1;
printf("\n %d",op);
}
else if(n>=0 && n<=500)
{
for(int i=2;i<=n;i++)
{
k=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
k=1;
break;
}
}
if(k==0)
{
sum=sum+(i*i);
printf("\n %d",i);
}
}
printf("\n sum of square of prime is %d",sum);
}else if(n>500)
{
op=-2;
printf("\n %d",op);
}
getch();
}
44.convert the binary input to decimal
Store result to output1
Eg.
If input1=100
Output1=4
Business rule1: if the input1 is not binary then store -1 to output1
Business rule1: if the input1 > 11111 then store -2 to output1

#include<stdio.h>
#include<conio.h>
void main()
{
long int a,n,d,dec=0,j=1,b,count=0,k=0,op;
clrscr();
printf("\n enter the no to be converted");
scanf("%ld",&n);
b=n;
if(b>=11111)
{
op=-2;
printf("\n %ld",op);
}
else if(b<11111)
{
while(b!=0)
{
count++;
d=b%10;
if(d==0 || d==1)
k++;
b=b/10;
}
if(count==k)
{
while(n!=0)
{
d=n%10;
dec=dec+j*d;
j=j*2;
n=n/10;
}
printf("\n the converted no is %ld",dec);
}
else if(count!=k)
{
op=-1;
printf("\n %d",op);
}
}
getch();
}

45. Program to calculate sum of cubes of primes up to a limit.(including that number)

Business Rule

Input1 <0 set output1 = -1

Input1 >32767 set output1= -2

#include<stdio.h>

#include<conio.h>

void main()

{
long int k,j,i;

long int n,sum=0;

clrscr();

printf("enter the limit");

scanf("%ld",&n);

if(n<0)

sum=-1;

else if(n>32767)

sum=-2;

else

for(i=1;i<=n;i++)

k=0;

for(j=1;j<=i;j++)

if(i%j==0)

k++;

if(k==2)

sum=sum+(i*i*i);
}}

printf("the sum of cube is %ld",sum);

getch();

46. Program to calculate sum of squares of multiples of 5 and find out the average up to n
number.

if

Input1 <0 set output1 = -1

else

Output1=sum.

#include<stdio.h>

#include<conio.h>

void main()

int n,i,k=0,sum=0;

float avg;

clrscr();

printf("enter the limit");

scanf("%d",&n);

if(n<0)

avg=-1;

else
{

for(i=1;i<=n;i++)

if(i%5==0)

sum=sum+(i*i);

k++;

}}

avg=(float)sum/k;

printf("the o/p is %0.2f",avg);

getch();

47. Program to find out whether the numbers in an array is a multiple of 3 if yes

Output1=number

Any nmber in Input1[]<0 output1=-1

Input2 <0 output1=-2 (size)

#include<stdio.h>

#include<conio.h>

void main()

{
int out, n,i,k=0,b=0,a[100];

clrscr();

printf("enter the limit");

scanf("%d",&n);

if(n<0){

out=-2;

else

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

scanf("%d",&a[i]);

if(a[i]>=0)

k++;

if(a[i]%3==0)

b++;

if(k==n)

out=b;

else
out=-1;

printf("the out is %d",out);

getch();

48. Program to find out the sum of odds from a digit.

Input1 < 0 output1=-1

Input1 >32767 output1=-2

#include<stdio.h>

#include<conio.h>

void main()

long int count=0, b, n,rev=0,d;

clrscr();

printf("enter the number");

scanf("%ld",&n);

if(n<0)

count=-1;

else if(n>32767)

count=-2;

else

while(n!=0)

{d=n%10;
if(d%2!=0)

count=count+d;

n=n/10;

printf("the odd count in digit is %ld",count );

getch();

69.Find the second largest element in the array.

If any one of the element in input array is negative ,store-1,

If input2 is negative,store -2.

#include<stdio.h>

#include<conio.h>

void main()

int i,j,n,t,temp=0,output[100];

clrscr();

printf("enter the number");

scanf("%d",&n);

if(n<0)

output[0]=-2;

printf("%d",output[0]);

}
else

printf("enter the elements");

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

scanf("%d",&output[i]);

if(output[i]<0)

temp=1;

if(temp==1)

output[0]=-1;

printf("%d",output[0]);

else

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

for(j=i+1;j<n;j++)

if(output[i]<output[j])
{

t=output[i];

output[i]=output[j];

output[j]=t;

printf("the second largest number is :%d",output[1]);

getch();

70.Fahrenheit to celcius

If input1 is negative,store -1

#include<stdio.h>

#include<conio.h>

void main()

float output,input;

printf("enter farhenheit temp\n");


scanf("%f",&input);

if(input<0)

output=-1;

printf("%f",output);

else

output=1.8*input+32;

printf("equalent temp id %f\n",output);

getch();

71. Celcius to Fahrenheit

If input1 is negative,store -1

#include<stdio.h>

#include<conio.h>

void main()

float output,f;
printf("enter farhenheit temp\n");

scanf("%f",&f);

if(f<0)

output=-1;

printf("%f",c);

else

output=(f-32)/1.8;

printf("equalent temp id %f\n",output);

getch();

72.Find the count of input3 in the given array.

Ex:Input1[]={1,2,4,4,4,5}

Input2:6

Input3:4

Output1:3

If any one of the element in input array is negative ,store-1,


If input2 is negative,store -2.

If input3 is negative,store -3.

#include<stdio.h>

#include<conio.h>

void main()

int input[10],output=0,temp=0,i,j,n,ele;

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

scanf("%d",&n);

if(n<0)

output=-2;

printf("%d",output);

else

printf("enter the elements\n");

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

scanf("%d",&input[i]);

if(input[i]<0)
{

temp=1;

if(temp==1)

output=-1;

printf("%d",output);

else

printf("enter element to get count\n");

scanf("%d",&ele);

if(ele<0)

output=-3;

printf("%d",output);

else

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

if(input[i]==ele)
output++;

printf("count is %d",output);

getch();

81. second highst element in an array

Business rule if any number in array is negative store -1 in output1

Business rule if array size is negative store -2 in output1


#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,n,count=0,temp;
printf("Enter the number");
scanf("%d",&n);
if(n<0)
{
a[n-2]=-1;
}
else
{
printf("Enter the value");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
a[n-2]=-2;
count=count+1;
break;
}}
if(count==0)
{
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}
}
}

printf("%d",a[n-2]);
getch();
}
85. count no. of digits in number(if no. is ve output=-1)
#include<stdio.h>

#include<conio.h>
void main()
{
int num,count=0;
printf("Enter the number");
scanf("%d",&num);
if(num<0)
{
count=-1;
}
else
{
while(num!=0)
{
num=num/10;
count=count+1;
}}
printf("%d",count);
getch();
}
86.. Average of marks of student(if avg>99 output=-1,if 70<avg<80 output=1, if 60<avg<70
output=2, if avg<60 output=3,if any of the marks is ve output=-2)

#include<stdio.h>
#include<conio.h>
void main()
{
int out,n,a[10],i,sum=0;
float avg;
clrscr();
printf("enter the number");
scanf("%d",&n);
printf("enter the marks");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
out=-2;
goto l;
}
}
for(i=0;i<n;i++)
sum=sum+a[i];
avg=(float)sum/(float)n;
printf("the average is %.2f",avg);
if(avg>99)
out=-1;
else if(avg>70 && avg<80)
out=1;
else if(avg>60 && avg<70)
out=2;
else
out=3;
l:
printf("the output should be %d",out);
getch();
}

87. sum of multiples of a no. upto N natural no.s


Input1:N=15
Input2:3
Output:3+6+9+12+15=45
( input2 is ve output=-2,if input1 is ve output=-1)

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,i,j,sum=0;
clrscr();
printf("the number are ");
scanf("%d%d",&a,&b);
if(a<0)
sum=-1;
else if(b<0)
sum=-2;
else
{
for(i=a;i<=b;i++)
{
if(i%a==0)
{
sum=sum+i;
printf(" %d +",i);
}
}
}
printf(" 0 = %d",sum);
getch();
}

88. write a Program to calculate temperature from celcius to Fahrenheit


c/5=f/9-32
Business Rules:
a)If given input is negative store -1 to the output variable

#include<stdio.h>

void main()

float f,c;
clrscr();

printf("enter celsius");

scanf("%f",&c);

if(c<0)

f=-1;

else

f=((c*9)/5)+32;

printf("the fahrenheit is %.2f",f);

getch();

92. Write a program to remove the duplicate elements from the given array .Input2 is size
of the output array. Store the result to output array. Business Rules:

a)If any of the given input element is negative then store -1 to the first position of the
output array
b)If the size of the array is negative then store -2 to the first position of the output array ..

#include<stdio.h>
#include<conio.h>
void main()
{
int c,n,a[10],b[10],i,j,k=0;
clrscr();
printf("Enter the number");
scanf("%d",&n);
printf("Enter the values");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
c=-1;
break;
}
}
if(n<0)
{
a[0]=-2;
printf("the output should be %d",a[0]);
goto l;
}
else
{
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j] && a[i]!=-1 && a[j]!=-1)
{
a[j]=-1;
}
}
}
}
printf("After removing the duplicate: ");
//for(i=0;i<n;i++)
//printf(" %d",a[i]);
for(i=0;i<n;i++)
{
if(a[i]!=-1)
{
b[k]=a[i];
k++;
}
}
//for(i=0;i<k;i++)
//printf(" %d",b[i]);
//printf(" ttt %d",k);

if(c==-1)
{
for(i=k;i>0;i--)
{
b[i]=b[i-1];
}
b[0]=-1;
}
for(i=0;i<k;i++)
printf("\nthe output should be %d\n",b[i]);
//printf("%d",a[0]);
getch();
l:
}

93.Write a program to delete the element from the given array and sort those remaining
elements

Input2 is size of array

Input3 is the element which we need to delete and store the results to output array.

Business Rules:

a)If any of the input is negative then store -1 to the first position of the output array
b)If searching element is not present in the array then store -2 to the first position of
the output array
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],b,pos=0,c,i,j,t;
clrscr();
printf("Enter the number");
scanf("%d",&n);
printf("Enter the values");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
c=-1;
printf("%d",c);
goto l;
}}
printf("Enter the element to be deleted");
scanf("%d",&b);
if(b!=a[i])
{
a[0]=-2;
printf("%d",a[0]);
}
else
{
for(i=1;i<=n;i++)
{
if(a[i]==b)
{
pos=i;
break;
}
}
if(pos!=0)
{
for(i=pos;i<=n;i++)
{
a[i]=a[i+1];
}
c=n-1;
}
else
c=n;
printf("The array after deletion is");
for(i=1;i<=c;i++)
printf("%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;
}
}
}
printf("the array is ");
for(i=0;i<n-1;i++)
printf("%d",a[i]);
}
l:
getch();
}

94. Second largest in a given array.[if array has -ev elements store -1 , if array size is -ve
store -2]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,t,a[10];
clrscr();
printf("Enter the number");
scanf("%d",&n);
if(n<0)
{
a[0]=-2;
printf("The output is %d",a[0]);
goto l;
}
printf("Enter the values");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
a[0]=-1;
printf("%d",a[0]);
break;
}
else
{
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(" The second largest number is %d",a[1]);
l:
}}
getch();
}

95. Check whether the given element is present in array.[store -2 if array has -ve elements,
-2 if array size is -ve and -1 if element not present and 1 if element present]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[10],d,i,output,output1;
clrscr();
printf("enter the number");
scanf("%d",&n);
if(n<0)
{
output=-2;
printf("%d",output);
goto l;
}
printf("Enter the values");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]<0)
{
output1=-2;
printf("%d",output1);
goto l;
}
}
printf("Enter the element to search");
scanf("%d",&d);
for(i=0;i<n;i++)
{
if(d==a[i])
{
output=1;
printf("%d",output);
}
}
if(output!=1)
{
output=-1;
printf("%d",output);
}
else
{
for(i=0;i<n;i++)
{
if(d==a[i])
{
printf(" the element %d in the %d position",d,i);
break;
}
}
}
l:
getch();
}

96. Employee salary.[if basic > 1000 store -1, no of days > 31 store -2,
if basic is -ve store -3, if no Of days is -ve store -4]
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,bs,c,d;
clrscr();
printf("enter the basic salary");
scanf("%d",&bs);
if(bs>1000)
{
c=-1;
printf("\n %d",c);
}
else if(bs<0)
{
c=-3;
printf("\n %d",c);
}
printf("enter the no of days");
scanf("%d",&d);
if(d>31)
{
c=-2;
printf("\n %d",c);
}
else if(d<0)
{
c=-4;
printf("\n %d",c);
}
getch();
}

97. Grade of the student. [if input array has -ve elements -1,size of
array -ve store -2,if average is >99 store -3]
#include<stdio.h>
#include<conio.h>
void main()
{
int av,a[10],n,i,sum=0,j,bs,c,d;
float avg;
clrscr();
printf("enter the no of subjects");
scanf("%d",&n);
if(n<0)
{
av=-2;
printf("%d",av);
goto l;
}
else
{
printf("enter the grades ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]<0)
{
av=-1;
printf("%d",av);
goto l;
}
}
for(i=0;i<n;i++)
sum=sum+a[i];
avg=(float)sum/(float)n;
if(avg>99)
{
av=-3;
printf("%d",av);
}
else
printf("the average is %f",avg);

}
l:
getch();
}
98. Sort the array (half ascending and descending based on odd or even no elements) [if
input array
has -ve elements store -1, size of array -ve store -2]

#include<stdio.h>
#include<conio.h>
void main()
{
int s[20], a[20],i,j,n,b[20],c[20],t,d[20],e[20],k;
clrscr();
printf("enter the limit");
scanf("%d",&n);

printf("enter the element");


for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}

printf("the order of arrangement is \n");


for(i=1;i<=n/2;i++)
b[i]=a[i];

for(i=1;i<=n/2;i++)
{
if(a[i]%2==0)
{
for(j=i+1;j<=n/2;j++)
{
if(a[j]%2==0)
{

if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
}
}

for(i=1;i<=n/2;i++)

{
printf("the even number in 1st half %d\n",a[i]);
}

for(i=(n/2)+1;i<=n;i++)
{
if(a[i]%2!=0)
{
for(j=i+1;j<=n;j++)
{
if(a[j]%2!=0)
{

if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
}
}

for(i=(n/2)+1;i<=n;i++)
{
printf(" the odd is=%d\n",a[i]);
}

getch();
}
99. Sum of digits of a given number. Input1=given number, if the given no is
negative store output1=-1,if given no is >32767
#include<stdio.h>

#include<conio.h>

void main()

long int a,count=0;

clrscr();

printf("enter any no");

scanf("%ld",&a);

if((a<0)||(a>32767))

count=-1;

printf("%d",count);

goto l;

while(a!=0)

a=a/10;

count=count+1;

printf("no of digit in a no %d",count);

l:
getch();

You might also like