You are on page 1of 36

https://www.demo2s.

com/
https://codeforwin.org/

1. Write a C program to check if a given Number is zero or Positive or Negative Using


if...else statement.

#include <stdio.h>
int main()
{
double number;
scanf("%lf", &number);
/* The number is entered automatically from the test cases and executed */
/* Write the rest of the code in the box below
As the output should exactly match with the output mentioned in the test cases
so copy and paste the following printf statements wherever and whichever is applicable
printf("The number is 0.");
printf("Negative number.");
printf("Positive number.");
Do not use any other scanf statements */
if (number <= 0.0)
{
if (number == 0.0)
printf("The number is 0.");
else
printf("Negative number.");
}
else
printf("Positive number.");
}

2. Write a C program to check whether a given number (integer) is Even or Odd.


#include <stdio.h>

int main()
{
int number;
scanf("%d", &number); /*An integer number is taken from the test case */
/* Write the rest of the program in the box provided below. As the output
should exactly match with the output provided in the test cases so use exactly the
following printf statement wherever and whichever is applicable.
printf("%d is even.", number);
printf("%d is odd.", number);
*/
if(number %2 == 0)
printf("%d is even.",number);
else
printf("%d is odd.", number);
}
3.Write a C Program to find the Largest Number (integer) among Three Numbers
(integers) using IF and Logical && operator.

#include <stdio.h>
int main()
{
int n1, n2, n3;
scanf("%d %d %d", &n1, &n2, &n3); /*Three numbers are accepted from the test case */
/* Complete the code in the box provided below. Use printf statement as provided below:
printf("%d is the largest number.", n1);
It may be n1, n2 or n3.
*/
if( n1>=n2 && n1>=n3 )
printf("%d is the largest number.", n1);

if( n2>=n1 && n2>=n3 )


printf("%d is the largest number.", n2);

if( n3>=n1 && n3>=n2 )


printf("%d is the largest number.", n3);
}

4. Write a C Program to Find the Smallest Number among Three Numbers (integer values)
using Nested IF-Else statement.

#include <stdio.h>
int main()
{
int n1, n2, n3;
scanf("%d %d %d", &n1, &n2, &n3); /* where three number are read from the test cases and
are stored in the variable n1, n2 and n3 */
/* Complete the program to get the desired output */
/* Only use the printf statement given below to exactly match your output
with the output cases. Change the variable n1 as required.
printf("%d is the smallest number.", n1); //Copy and paste this printf statement wherever
required.
*/
if(n1<n2)
{
if(n1<n3)
printf("%d is the smallest number.", n1);
}
else
{
if(n2<n3)
printf("%d is the smallest number.", n2);
else
printf("%d is the smallest number.", n3);
}
return 0;
}

5. Write a program to find whether a given character is a Vowel or consonant. A character


is taken as input. The character may be in Upper Case or in Lower Case.

#include <stdio.h>
int main()
{
char ch;
scanf("%c",&ch); /* It reads a character from the input cases and store it in ch */
/* Complete the program. Use the printf statement given below to match your
output exactly with the test cases.
printf("%c is a vowel.", ch);
printf("%c is a consonant.", ch);
*/
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("%c is a vowel.", ch);
else
printf("%c is a consonant.", ch);
return 0;
}
OR

#include <stdio.h>
int main()
{
char ch;
scanf("%c",&ch); /* It reads a character from the input cases and store it in ch */
/* Complete the program. Use the printf statement given below to match your
output exactly with the test cases.
printf("%c is a vowel.", ch);
printf("%c is a consonant.", ch);
*/
int Lower_case, Upper_case;
Lower_case = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');

Upper_case = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');


if (Lower_case|| Upper_case)
printf("%c is a vowel.", ch);
else
printf("%c is a consonant.", ch);
return 0;
}

6. Write a C program to calculate the Sum of First and the Last Digit of a given Number.
For example if the number is 1234 the result is 1+ 4 = 5.

#include <stdio.h>
int main()
{
int N, First_digit, Last_digit;
scanf("%d", &N); //The number is accepted from the test case
Last_digit=N%10;
First_digit=N;
while(First_digit>=10)
{
First_digit=First_digit/10;
}
printf("Sum of First and Last digit = %d", First_digit + Last_digit);
return 0;
}

6. Write a C program to find power of a number using while loops. The base number (>0)
and exponent (>=0) is taken from the test cases.
#include <stdio.h>
int main()
{
int base, exponent;
long int result;
scanf("%d", &base); //The base value is taken from test case
scanf("%d", &exponent); //The exponent value is taken from test case
if(exponent==0)
result=1;
else
{
result=1;

while(exponent !=0)
{
result=result*base;
--exponent;
}
}
printf("The result is : %ld\n", result);
return 0;
}

7. Write a C program to count total number of digits of an Integer number (N).

#include <stdio.h>
int main()
{
int N;
scanf("%d",&N); /*The number is accepted from the test case data*/
/* Complete the rest of the code. Please use the printf statements as below
by just changing the variables used in your program
printf("The number %d contains %d digits.",N,count);
*/
int temp,count;
temp=N;
count=0;
while(temp!=0)
{
count++;
temp/=10;
}
printf("The number %d contains %d digits.",N,count);
return 0;
}
OR
#include <stdio.h>
int main()
{
int N;
scanf("%d",&N); /*The number is accepted from the test case data*/
/* Complete the rest of the code. Please use the printf statements as below
by just changing the variables used in your program

printf("The number %d contains %d digits.",N,count);


*/

int temp, count;


count=0;
temp=N;
while(temp>0)
{
count++;
temp/=10;
}
printf("The number %d contains %d digits.",N,count);
}

8. Write a C program to find sum of following series where the value of N is taken as input
 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N.

#include<stdio.h>
int main()
{
int N;
float sum = 0.0;
scanf("%d",&N); /*Read the value of N from test cases provided*/
/* Complete the program. Please use the printf statement given below:
printf("Sum of the series is: %.2f\n",sum);
*/
float i;
for(i=1;i<=N;i++)
{
sum=sum+(1/i);
}
printf("Sum of the series is: %.2f\n",sum);
return 0;
}
OR
#include<stdio.h>
int main()
{
int N;
float sum = 0.0;
scanf("%d",&N); /*Read the value of N from test cases provided*/

/* Complete the program. Please use the printf statement given below:
printf("Sum of the series is: %.2f\n",sum);
*/
int i;
for(i=1;i<=N;i++)
sum = sum + ((float)1/(float)i);
printf("Sum of the series is: %.2f\n", sum);
}

9. Write a C program to check whether the given number(N) can be expressed as Power of
Two (2) or not. For example 8 can be expressed as 2^3.

#include <stdio.h>
int main()
{
int N;
scanf("%d",&N); /* The value of N is taken from the test case data */
/* Complete the code.
Use the printf statements as below
printf("%d is a number that can be expressed as power of 2.",N);
printf("%d cannot be expressed as power of 2.",N);
*/
int temp,flag;
temp=N;
flag=0;
while(temp!=1)
{
if(temp%2!=0)
{
flag=1;
break;
}
temp=temp/2;
}
if(flag==0)
printf("%d is a number that can be expressed as power of 2.",N);
else
printf("%d cannot be expressed as power of 2.",N);
return 0;
}

10. Write a C program to print the following Pyramid pattern upto Nth row. Where N
(number of rows to be printed) is taken as input. For example when the value of N is 5 the
pyramid will be printed as follows
*****
****
***
**
*
#include<stdio.h>
int main()
{
int N;
scanf("%d", &N); /*The value of N is taken as input from the test case */
int i,j;
for(i=N;i>0;i--)
{
for(j=0;j<i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

11.Write a C Program to find Largest Element of an Integer Array. 


Here the number of elements in the array ‘n’ and the elements of the array is read from the
test data. 
Use the printf statement given below to print the largest element.
printf("Largest element = %d", largest);
#include <stdio.h>
int main()
{
int i, n, largest;
int arr[100];
scanf("%d", &n); /*Accepts total number of elements from the test data */
for(i = 0; i< n; ++i)
{
scanf("%d", &arr[i]); /* Accepts the array element from test data */
}
largest= arr[0];
for(i=1;i<n;++i)
{
if(largest < arr[i])
largest = arr[i];
}
printf("Largest element = %d",largest);
return 0;
}

12. Write a C Program to print the array elements in reverse order (Not reverse sorted
order. Just the last element will become first element, second last element will become
second element and so on)
Here the size of the array, ‘n’ and the array elements is accepted from the test case data.
The last part i.e. printing the array is also written. 
You have to complete the program so that it prints in the reverse order.
#include<stdio.h>

int main()
{
int arr[20], i, n;
scanf("%d", &n); /* Accepts the number of elements in the array */
for (i = 0; i< n; i++)
scanf("%d", &arr[i]); /*Accepts the elements of the array */
int j,temp;
for(i=n-1,j=0;i>0;i--,j++)
{
if(i>j)
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
for (i = 0; i< n; i++) {
printf("%d\n", arr[i]); // For printing the array elements
}
return (0);
}
OR
#include<stdio.h>
int main()
{
int arr[20], i, n;
scanf("%d", &n); /* Accepts the number of elements in the array */
for (i = 0; i< n; i++)
scanf("%d", &arr[i]); /*Accepts the elements of the array */
int j, temp;
j = i - 1; // last Element of the array
i = 0; // first element of the array
while (i< j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
for (i = 0; i< n; i++) {
printf("%d\n", arr[i]); // For printing the array elements
}
return (0);
}

13. Write a C program to read Two One Dimensional Arrays of same data type (integer
type) and merge them into another One Dimensional Array of same type.
#include<stdio.h>
int main()
{
int arr1[20], arr2[20], array_new[40], n1, n2, size, i;
/*n1 size of first array (i.e. arr1[]), n2 size of second array(i.e. arr2[]),
size is the total size of the new array (array_new[]) */
scanf("%d", &n1); //Get the size of first array from test data and store it in n1.
for (i = 0; i< n1; i++)
scanf("%d", &arr1[i]); //Accepts the values for first array
scanf("%d", &n2); //Get the size of second array from test data and store it in n2.
for (i = 0; i< n2; i++)
scanf("%d", &arr2[i]); //Accepts the values for second array
int j;
size=n1+n2;
for(i=0;i<n1;i++)
array_new[i]=arr1[i];
for(i=n1,j=0;i<size;i++,j++)
array_new[i]= arr2[j];
//Printing after merging
for (i = 0; i< size; i++) {
printf("%d\n", array_new[i]);
}

}
OR

#include<stdio.h>
int main()
{
int arr1[20], arr2[20], array_new[40], n1, n2, size, i;
/*n1 size of first array (i.e. arr1[]), n2 size of second array(i.e. arr2[]),
size is the total size of the new array (array_new[]) */
scanf("%d", &n1); //Get the size of first array from test data and store it in n1.
for (i = 0; i< n1; i++)
scanf("%d", &arr1[i]); //Accepts the values for first array
scanf("%d", &n2); //Get the size of second array from test data and store it in n2.
for (i = 0; i< n2; i++)
scanf("%d", &arr2[i]); //Accepts the values for second array
//Marge two arrays
int j;
for (i=0;i<n1;++i)
array_new[i]=arr1[i];

size = n1 + n2;
for(i=0, j=n1; j<size && i<n2; ++i, ++j)
array_new[j] = arr2[i];
//Printing after merging
for (i = 0; i< size; i++) {
printf("%d\n", array_new[i]);
}

14. Write a C Program to delete duplicate elements from an array of integers.
#include<stdio.h>
int main()
{
int array[50], i, size;
scanf("%d", &size); /*Accepts the size of array from test case data */
for (i = 0; i< size; i++)
scanf("%d", &array[i]); /* Read the array elements from the test case data */
int j,k;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;)
{
if(array[j]==array[i])
{
for(k=j;k<size;k++)
{
array[k]=array[k+1];
}
size--;
}
else
j++;
}

}
for (i = 0; i< size; i++) {
printf("%d\n", array[i]);
}

15. Write a C program to find the sum of all elements of each row of a matrix.
#include <stdio.h>
int main()
{
int matrix[20][20];
int i,j,r,c;
scanf("%d",&r); //Accepts number of rows
scanf("%d",&c); //Accepts number of columns
for(i=0;i< r;i++) //Accepts the matrix elements from the test case data
{
for(j=0;j< c;j++)
{
scanf("%d",&matrix[i][j]);
}
}
/*Complete the code to print the sum of each rows. Use the printf() statement as
printf("%d\n",sum); Where sum is the sum of a row.
*/
int sum;
for(i=0;i<r;i++)
{
sum=0;
for(j=0;j<c;j++)
{
sum += matrix[i][j];
}
printf("%d\n",sum);
}
}

16. Write a C program to find subtraction of two matrices i.e. matrix_A -


matrix_B=matrix_C.
If the given martix are
 2 3 5 and 1 5 2 Then the output will be 1 -2 3
 4 5 6        2 3 4                                       2 2 2
 6 5 7        3 3 4                                       3 2 3
 The elements of the output matrix are separated by one blank space.

#include <stdio.h>
int main()
{
int matrix_A[20][20], matrix_B[20][20], matrix_C[20][20];
int i,j,row,col;
scanf("%d",&row); //Accepts number of rows
scanf("%d",&col); //Accepts number of columns
/* Elements of first matrix are accepted from test data */
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d", &matrix_A[i][j]);
}
}

/* Elements of second matrix are accepted from test data */


for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d", &matrix_B[i][j]);
}
}

/* Complete the program to get the desired output. Use printf() statement as below
printf("%d ", matrix_C[i][j]); You can declare your own variables if required.
*/
for(i=0;i<row;i++)
{

for(j=0;j<col;j++)
{
matrix_C[i][j] = matrix_A[i][j] - matrix_B[i][j];

}
}

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

{
for(j=0;j<col;j++)

{
printf("%d ", matrix_C[i][j]);
}
printf("\n");

}
return 0;
}
OR

#include <stdio.h>
int main()
{
int matrix_A[20][20], matrix_B[20][20], matrix_C[20][20];
int i,j,row,col;
scanf("%d",&row); //Accepts number of rows
scanf("%d",&col); //Accepts number of columns

/* Elements of first matrix are accepted from test data */


for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d", &matrix_A[i][j]);
}
}
/* Elements of second matrix are accepted from test data */
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d", &matrix_B[i][j]);
}
}

/* Complete the program to get the desired output. Use printf() statement as below
printf("%d ", matrix_C[i][j]); You can declare your own variables if required.
*/
/*
Subtract both matrices and store the result in matrix C
*/
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
matrix_C[i][j] = matrix_A[i][j] - matrix_B[i][j];
}
}

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


{
for(j=0; j<col; j++)
{
printf("%d ", matrix_C[i][j]);
}
printf("\n");
}
return 0;
}

17. Write a C program to print lower triangle of a square matrix.


For example the output of a given matrix
 2 3 4 will be 2 0 0
5 6 7       5 6 0
4 5 6       4 5 6
#include <stdio.h>
int main()
{
int matrix[20][20];
int i,j,r;
scanf("%d", &r); //Accepts number of rows or columns
for(i=0;i< r;i++) //Accepts the matrix elements from the test case data
{
for(j=0;j<r; j++)
{
scanf("%d",&matrix[i][j]);
}
}
/* Complete the program to get the desired output. Use the printf() statement as
printf("%d ", matrix[i][j]);
*/
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
if(i<j)
{
matrix[i][j]=0;
}
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;

18. Write a C program to print Largest and Smallest Word from a given sentence. If there
are two or more words of same length, then the first one is considered. A single letter in the
sentence is also consider as a word.

#include<stdio.h>
#include<string.h>
int main()
{
char str[100]={0},substr[100][100]={0};
//str[100] is for storing the sentence and substr[50][50] is for storing each word.
scanf("%[^\n]s", str); //Accepts the sentence from the test case data.
/* Complete the program to get the desired output.
The print statement should be as below
printf("Largest Word is: %s\nSmallest word is: %s\n", -------,--------);
*/
int i=0,j=0,k=0,strr,m=0,n=0,max=0,min=0;
char c;
while(str[k]!='\0') //for splitting sentence into words
{
j=0;
while(str[k]!=' '&&str[k]!='\0' && str[k]!='.')
{
substr[i][j]=str[k];
k++;
j++;
}
substr[i][j]='\0';
i++;
if(str[k]!='\0')
{
k++;
}
}
int len=i;
max=strlen(substr[0]);
min=strlen(substr[0]);
//After splitting getting length of string and finding its index having max length and index
having min length
for(i=0;i<len;i++)
{
strr=strlen(substr[i]);
if(strr>max)
{
max=strr;
m=i;
}
if(strr<min)
{
min=strr;
n=i;
}
}
printf("Largest Word is: %s\nSmallest word is: %s\n",substr[m],substr[n]);
return 0;
}

OR

#include<stdio.h>
#include<string.h>
int main()
{
char str[100]={0},substr[100][100]={0};
//str[100] is for storing the sentence and substr[50][50] is for storing each word.
scanf("%[^\n]s", str); //Accepts the sentence from the test case data.
/* Complete the program to get the desired output.
The print statement should be as below
printf("Largest Word is: %s\nSmallest word is: %s\n", -------,--------);
*/
int i=0,j=0,k=0,a,minIndex=0,maxIndex=0,max=0,min=0;
char c;
while(str[k]!='\0') //for splitting sentence into words
{
j=0;
while(str[k]!=' '&&str[k]!='\0' && str[k]!='.')
{
substr[i][j]=str[k];
k++;
j++;
}
substr[i][j]='\0';
i++;
if(str[k]!='\0')
{
k++;
}
}
int len=i;
max=strlen(substr[0]);
min=strlen(substr[0]);
//After splitting getting length of string and finding its index having max length and index
having min length
for(i=0;i<len;i++)
{
a=strlen(substr[i]);
if(a>max)
{
max=a;
maxIndex=i;
}
if(a<min)
{
min=a;
minIndex=i;
}
}
printf("Largest Word is: %s\nSmallest word is: %s\n",substr[maxIndex],substr[minIndex]);
return 0;
}
19. Write a C Program to find HCF of 4 given numbers using recursive function.

#include<stdio.h>
int HCF(int, int); //You have to write this function which calculates the HCF.

int main()
{
int a, b, c, d, result;
scanf("%d %d %d %d", &a, &b, &c, &d); /* Takes 4 number as input from the test data */
result = HCF(HCF(a, b), HCF(c,d));
printf("The HCF is %d", result);
}
//Complete the rest of the program to calculate HCF
int HCF(int x,int y)
{
while(x!=y)

{
if(x>y)
{
return HCF(x-y,y);
}

else
{
return HCF(x,y-x);
}
}

return x;
}

20. Write a C Program to print Binary Equivalent of an Integer using Recursion.


#include <stdio.h>
int binary_conversion(int); //function to convert binary to decimal number
int main()
{
int num, bin; //num is the decimal number and bin is the binary equivalent for the number

scanf("%d", &num); //The decimal number is taken from the test case data
bin = binary_conversion(num); //binary number is stored in variable bin
printf("The binary equivalent of %d is %d\n", num, bin);
return 0;
}
int binary_conversion(int num)
{
if(num==0)
{
return 0;
}
else
{
return(num%2)+10*binary_conversion(num/2);
}
}

21.Write a program to calculate the sum of a given series.


1+1/22+1/32+1/42+…………….+1/nn till nth term.

#include <math.h> //For using the pow() to calculate power


#include <stdio.h>
double Series_Sum(int n); /* function for calculation the series sum */
int main()
{
int n;
scanf("%d", &n);
double Result;
Result = Series_Sum(n);

printf("The sum of the series is = %.6f\n", Result);


return 0;
}
double Series_Sum(int n)
{
int i;
double sums=0.0,ser;
for(i=1;i<=n;i++)
{
ser=1/pow(i,i);
sums+=ser;
}
return sums;
}

OR

#include <math.h> //For using the pow() to calculate power


#include <stdio.h>
double Series_Sum(int n); /* function for calculation the series sum */
int main()
{
int n;
scanf("%d", &n);
double Result;
Result = Series_Sum(n);

printf("The sum of the series is = %.6f\n", Result);


return 0;
}
double Series_Sum(int n)
{
int i;
double sum = 0.0, element;
for (i=1; i<=n; ++i)
{
element = 1/ pow(i,i);
sum = sum + element;
}
return sum;
}

22. Write a program to express a given integer as a Sum of Two Prime Numbers
For example if the number is 10
The result will be
10 = 3 + 7
10 = 5 + 5
#include <stdio.h>
int checkPrime(int n);
int main() {
int n;
scanf("%d", &n); //An integer number is taken from the test case
/* Complete the program
Use
Use the printf statement as below:
printf("%d = %d + %d\n", n, variable1, variable2); */
int i,flag = 0;
for(i = 2; i <= n / 2; ++i)
{
if(checkPrime(i) == 1)
{
if(checkPrime(n - i) == 1)
{
printf("%d = %d + %d\n", n, i, n - i);
flag = 1;
}
}
}

if(flag == 0)
printf("%d cannot be expressed as the sum of two prime numbers.", n);

return 0;
}
// function to check prime number
int checkPrime(int n)
{
int i, isPrime = 1;
if (n == 0 || n == 1)
{
isPrime = 0;
}
else
{
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = 0;
break;
}
}
}
return isPrime;
}

23. Write a program to print all the locations at which a particular element (taken as input)
is found in a list and also print the total number of times it occurs in the list. The location
starts from 1.
For example if there are 4 elements in the array
5
6
5
7
If the element to search is 5,  then the output will be:
5 is present at location 1
5 is present at location 3
5 is present 2 times in the array.

#include <stdio.h>
int main()
{
int array[100], search, n, count = 0;
//"search" is the key element to search and 'n' is the total number of element of the array
// "count" is to store total number of elements
scanf("%d", &n); //Number of elements is taken from test case
int c;
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
scanf("%d", &search); // The element to search is taken from test case
/* Use the printf statements as below:
"%d is present at location %d.\n" for each locations
"%d is not present in the array.\n" if the element is not found in the list
"%d is present %d times in the array.\n"
*/
for(c=0;c<n;c++)
{
if(array[c]==search)
{
printf("%d is present at location %d.\n", search,c+1);
count++;
}
}
if(count==0)
printf("%d is not present in the array.\n",search);
else
printf("%d is present %d times in the array.\n",search,count);
return 0;
}

24. Write a C program to search a given element from a 1D array and display the position
at which it is found by using linear search function. The index location starts from 1.
#include <stdio.h>
int linear_search(int[], int, int);
int main()
{
int array[100], search, c, n, position;
/* search - element to search, c - counter, n - number of elements in array,
position - The position in which the element is first found in the list. */

scanf("%d", &n); // Number of elements in the array is read from the test case data
for (c = 0; c < n; c++)
scanf("%d", &array[c]); //Elements of array is read from the test data
scanf("%d", &search); //Element to search is read from the test case data
/* Use the following in the printf statement to print the output
printf("%d is not present in the array.\n", search);
printf("%d is present at location %d.\n", search, position+1); //As array[0] has the position 1
*/
position=linear_search(array,n,search);
if(position==-1)
printf("%d is not present in the array.\n", search);
else
printf("%d is present at location %d.\n", search, position+1);
return 0;
}

int linear_search(int a[],int n,int find)


{
int c;
for(c=0;c<n;c++)
{
if(a[c]==find)
return c;
}
return-1;
}
25. Write a C program to search a given number from a sorted 1D array and display the
position at which it is found using binary search algorithm.
The index location starts from 1.

#include <stdio.h>
int main()
{
int c, n, search,
array[100];
scanf("%d",&n); //number of elements in the array
for (c = 0; c < n; c++)
scanf("%d",&array[c]);

scanf("%d", &search); //The element to search is read from test case.


/* Use the printf statements as below:
printf("%d found at location %d.\n", search, variable_name);
printf("Not found! %d isn't present in the list.\n", search);
*/
int first,last,middle;
first=0;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(array[middle]<search)
first=middle+1;
else if(array[middle]==search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else

last=middle-1;
middle=(first+last)/2;
}
if(first>last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
26. Write a C program to reverse an array by swapping the elements and without using
any new array.

#include <stdio.h>
int main() {
int array[100], n, c;
scanf("%d", &n); // n is number of elements in the array.
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}

int temp,end;
end = n-1;
for(c = 0;c < n/2;c++)
{
temp = array[c];
array[c] = array[end];
array[end]=temp;
end--;
}
printf("Reversed array elements are:\n");
for (c = 0; c < n; c++)
{
printf("%d\n", array[c]);
}
return 0;
}

27. Write a C program to find the root of the equation using bisection method for different
values of allowable error of the root f(x)=2x3-3x-5.
#include<stdio.h>
float fun (float x); //Function fun returns the function value of f(x)
void bisection (float *x, float a, float b, int *itr); // This function computes the root of f(x) using
bisection method
int main ()
{
int itr = 0, maxmitr=10;
float x, a=1.0, b=2.0, allerr, x1; // x is the value of root in each iteration, x1 is the final value
of the root
// a and b are the initial range for calculating the root using bisection method
scanf("%f", &allerr); // allerr is the allowable error taken from test case data
bisection (&x, a, b, &itr);
/* Use the printf statement as given below to print the root
printf("Root = %1.4f\n", x1); */

do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
{
printf("Root = %1.4f\n", x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
return 1;
}
float fun (float x)
{
return (2*x*x*x - 3*x - 5);
}
void bisection (float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
*x=(a+b)/2;
++(*itr);
}

28. Write a C program to find the root of the equation using Newton Raphson method for
different values of allowable error of the root f(x)=x3-3x-5.The maximum number of steps
are taken as input.
#include<stdio.h>
float f(float x);
float df (float x);

int main()
{
int itr, maxmitr; // itr is the iteration number and maxitr is the maximum allowable iteration
float x0=1.0, x1; // x0 is the initial value and x1 is result
scanf("%d", &maxmitr); // Taken from the test cases

// use the printf statement as printf("Root = %8.6f\n", x1);


float h;
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
x0=x1;
}
printf("Root = %8.6f\n", x1);
return 0;
}
float f(float x)
{
return x*x*x - 2*x - 3;
}
float df (float x)
{
return 3*x*x-2;
}

29. Write a C program to sort a given 1D array using pointer in ascending order.

#include <stdio.h>
int main()
{
int a[100],i, n;
scanf("%d",&n); //Number of elements of the array is taken from the test case data

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


{
scanf("%d",a+i); // Input the array elements
}
int j,t;
for (i=0; i<(n-1); i++)
{
for (j=i+1; j<n; j++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}
// Printing sorted array in ascending order
for (i=0; i<n; i++)
{
printf("%d\n",*(a+i));
}
return 0;
}
30. Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not invertible if its
determinant is 0.
#include<stdio.h>
int main()
{
int a[3][3], i, j;
long determinant;
// 9 elements of matrix is taken as input from test data
for(i = 0 ; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
/*Use the printf statements as:
printf("The given matrix is not invertible");
printf("The given matrix is invertible");
*/
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
if ( determinant == 0)
printf("The given matrix is not invertible");
else
printf("The given matrix is invertible");
return 0;
}

31. The velocity of a car at different time instant is given as


Time(t) 10 15 18 22 30
Velocity v(t) 22 26 35 48 68
A linear Lagrange Interpolant is found using these data points. Write a C program to find
the velocity of the car at different time instants.
#include<stdio.h>
int main()
{
float t[100]={10,15,18,22,30}, v[100]={22,26,35,48,68};
float a; //Value of the t to find the respective value of v(t)
scanf("%f", &a); // This will be taken from test cases
int i,j;
float b, c, k =0;
for(i=0; i<5; i++)
{
b=1;
c=1;
for(j=0; j<5; j++)
{
if(j!=i)
{
b=b*(a-t[j]);
c=c*(t[i]-t[j]);
}
}
k=k+((b/c)*v[i]);
}
printf("The respective value of the variable v is: %.2f", k);
return 0;
}

32.Write a C program to find ∫ x 2 dx using trapezoidal rule with 10 segments between a


a

and b. The values are taken from the test.

#include<stdio.h>
float func(float x);
int main()
{
int n=10; //Taking n=10 sub intervals
float a,b,integral; //integral is the integration result
scanf("%f",&a); // initial limit taken from test case
scanf("%f",&b); // Final limit taken from test case
//Use the printf statement as printf("The integral is: %0.6f\n",integral);
int i;
float h,x, sum=0;
if(b>a)
h=(b-a)/n;
else
h=-(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+func(x);
}
integral=(h/2)*(func(a)+func(b)+2*sum);
printf("The integral is: %0.6f\n",integral);
return 0;
}
float func(float x)
{
return x*x;
}
33. Write a C program to check whether the given input number is Prime number or not
using recursion. So, the input is an integer and output should print whether the integer is
prime or not. Note that you have to use recursion.
#include <stdio.h>
int checkPrime(int, int); //Function to check prime or not
int main()
{
int num, check;
scanf("%d", &num); //The number is taken from test case data
check = checkPrime(num, num/2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
{
printf("%d is not a prime number\n", num);
}
return 0;
}
int checkPrime(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return checkPrime(num, i - 1);
}
}
}

34. Write a C program to reverse a word using Recursion. Input to the program is a string
that is to be taken from the user and output is reverse of the input word. Note that you
have to use recursion.

#include <stdio.h>
#define MAX 100
char *reverse(char[]);

int main()
{
char str[MAX], *rev;
//printf("Enter a String: ");
scanf("%s", str);
rev = reverse(str); //You have to write this function
printf("The reversed string is : %s\n", rev);
return 0;
}
char* reverse(char str[])

{
static int i= 0;
static char rev[MAX];
if (*str)
{
reverse(str+1);
rev[i++]= *str;
}
return rev;
}
35. Write a C program which accept date, month and year as Input from the user, and
using switch case, display the date in worded format.

#include <stdio.h>
int main()
{
int month = 0;
int day = 0;
int year = 0;

scanf("%d", &day);
scanf("%d", &month);
scanf("%d", &year);

if(day > 3 && day < 21 || day > 23 && day < 31)
printf("%dth ",day);
else
printf("%d%s ", day, (day%10 == 1 ? "st": (day%10 == 2 ? "nd" : "rd")));

switch(month)
{
case 1:
printf("January ");
break;
case 2:
printf("February ");
break;
case 3:
printf("March ");
break;
case 4:
printf("April ");
break;
case 5:
printf("May");
break;
case 6:
printf("June");
break;
case 7:
printf("July");
break;
case 8:
printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
}
printf("%d", year);
return 0;
}

36. Write a C program to print a hollow rectangle pattern using "*" symbol. The number
of rows and columns is accepted from the test case data.
e.g. For row = 5 and columns = 6 The output will be 

******
*      *
*      *
*      *
******

#include<stdio.h>
void hollow_rectangle(int n, int m);
int main()
{
int rows, columns;
scanf("%d", &rows);
scanf("%d", &columns);
hollow_rectangle(rows, columns);
return 0;
}
//Write the above function to print the hollow rectangle pattern
void hollow_rectangle(int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (i == 1 || i == n || j == 1 || j == m)
printf( "*");
else
printf(" ");
}
printf("\n");
}

You might also like