You are on page 1of 17

Write a C program to find the sum and average of one

dimensional integer array.

#include <stdio.h>

int main()
{
int a[25], sum=0 , average, i;
printf("ENTER ANY 25 NUMBERS \n");
for(i=0; i<=24; i++)
{
scanf("%d", &a[i]);
sum+=a[i];
}
average =sum/25;
printf("SUM OF THE NUMBERS IS %d \n",sum);
printf("AVERAGE OF THE NUMBERS IS %d \n", average);
return 0;

Write a C program to swap first and last element of an integer 1-d array.

#include <stdio.h>

int main()
{
int a[10],temp,i;
printf("ENTER ANY 10 NUMBERS \n");
for(i=0; i<=9; i++)
{
scanf("%d", &a[i]);
}
temp = a[0];
a[0]=a[9];
a[9]=temp;
printf("THE ARRRAY AFTER SWAPING LAST AND FIRST POSITIONS IS\n");
for(i=0; i<9;i++)
printf("%d ",a[i]);
return 0;
}

Write a program to check whether the entered number is a palindrome or not


#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);

return 0;
}
Write a C program to reverse the element of an integer 1-D array.

#include <stdio.h>

int main()
{
int a[10],temp,i;
printf("ENTER ANY 10 NUMBERS \n");
for(i=0; i<=9; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<5; i++)
{
temp=a[i];
a[i]=a[9-i];
a[9-i]=temp;
}
printf("THE REVERSED ARRAY IS /n");
for(i=0; i<=9; i++)
printf("%d ", a[i]);
return 0;
}

Write a C program to find the largest and smallest element of an array.

#include <stdio.h>

int main()
{
int a[10],temp,i,j;
printf("ENTER ANY 10 NUMBERS \n");
for(i=0; i<=9; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<9; i++)
for(j=0; j<9-i; j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("THE SMALLEST NUMBER IN ARRAY IS \n");
printf("%d\n", a[0]);
printf("THE LARGEST NUMBER IN ARRAY IS \n");
printf("%d", a[9]);
return 0;
}

Write a menu driven C program with following option


a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two parameters name of the
array and number of elements in the array.

#include <stdio.h>

void enter(int a[], int n)


{
int i;
printf("ENTER ELEMENTS IN THE ARRAY \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
}

void display(int a[], int n)


{
int i;
for(i=0; i<n; i++)
{
printf("%d", a[i]);
}
}

void isort(int a[], int n)


{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j]) && (j>=0))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}

void ssort(int a[], int n)


{
int i,j, small, temp;
for(i=0; i<n-1; i++)
{
small=i;
for(j=1; j<n; j++)
{
if(a[j]>a[j+1])
small=j;
if(small!=i)
{
temp=a[small];
a[small]=a[i];
a[i]=temp;
}
}
}
}

void bsort(int a[], int n)


{
int i,j,temp;
for(i=0; i<n; i++)
for(j=0; j<n-i; j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
int main()
{
int a[50],c,s; char x;
do{
printf("HOW MANY ELEMENTS DO YOU WANT IN THE ARRAY (<50) \n");
scanf("%d", &s);
printf(" 1. ENTER ELEMENTS IN THE ARRAY \n 2. DISPLAY ELEMENTS OF THE ARRAY \n");
printf(" 3. INSERTION SORT \n 4. SELECTION SORT \n 5. BUBBLE SORT \n");
scanf("%d", &c);

switch(c)
{
case 1: enter(a,s);
break;

case 2: display(a,s);
break;

case 3: isort(a,s);
break;

case 4: ssort(a,s);
break;

case 5: bsort(a,s);
break;
}
printf("WANT MORE? (Y/N)");
scanf("%c", &x);
}while(x==121);
return 0;
}

P is one-dimensional array of integers. Write a C++ function to efficiently search for a data VAL
from P. If VAL is present in the array then the function should return value 1 and 0 otherwise.

#include <stdio.h>

void enter(int P[], int n)


{
int i;
printf("ENTER NUMBERS \n");
for(i=0; i<n; i++)
scanf("%d", &P[i]);
}

int search(int P[], int n, int VAL)


{
int i;
for(i=0; i<n; i++)
if(P[i]==VAL)
return 1;
else
continue;
return 0;
}

int main()
{
int P[50],c,s,VAL;
printf("ENTER HOW MANY ELEMENTS YOU WANT TO ENTER \n");
scanf("%d",&s);
enter(P,s);
printf("ENTER THE ELEMENT YOU WANT TO SEARCH IN THE ARRAY=");
scanf("%d",&VAL);
c=search(P,s,VAL);
if(c==1)
printf("ELEMENT FOUND");
else
printf("ELEMENT NOT FOUND");

return 0;
}

Write a program to divide the elements of an array in two different arrays checking if they are even or
odd.
#include <stdio.h>
void main()
{
long int ARR[10], OAR[10], EAR[10];
int i, j = 0, k = 0, n,m;
printf("Enter the size of array AR \n");
scanf("%d", &n);
printf("Enter the elements of the array \n");
for (i = 0; i < n; i++)
{
scanf("%ld", &ARR[i]);
}
for (i = 0; i < n; i++)
{
if (ARR[i] % 2 == 0)
{
EAR[j] = ARR[i];
j++;
}
else
{
OAR[k] = ARR[i];
k++;
}
}
printf("The elements of OAR are \n");
for (i = 0; i < j; i++)
{
printf("%ld\n", OAR[i]);
}
printf("The elements of EAR are \n");
for (i = 0; i < k; i++)
{
printf("%ld\n", EAR[i]);
}
}
Write a program to add a element to an array
#include <stdio.h>
void main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter how many elements \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (key < array[i])
{
pos = i;
break;
}
if (key > array[n-1])
{
pos = n;
break;
}
}
if (pos != n)
{
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
}
array[pos] = key;
printf("Final list is \n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}

Write a program to merge two sorted arrays


#include <stdio.h>
void BSort(int A[], int n)
{
int I,J,Temp;
for(I=0;I<n-1;I++)
{
for(J=0;J<(n-1-I);J++)
if(A[J]>A[J+1])
{
Temp=A[J];
A[J]=A[J+1];
A[J+1]=Temp;
}
}
}

int Merge(int A[], int B[], int C[], int N, int M)


{
int I=0, J=0,T;
int K=0;
while (I<N && J<M)
{
if (A[I]<B[J])
C[K++]=A[I++];
else if (A[I]>B[J])
C[K++]=B[J++];
else
{
C[K++]=A[I++];
J++;
}
}
for ( T=I;T<N;T++)
C[K++]=A[T];
for (T=J;T<M;T++)
C[K++]=B[T];
return K;
}
void main()
{
int A[20],B[20],C[40],a,b,c,i,j;
printf("ENTER NUMBER OF ELEMENTS IN FIRST ARRAY=");
scanf("%d",&a);
printf("ENTER NUMBER OF ELEMENTS IN SECOND ARRAY=");
scanf("%d",&b);
printf("ENTER ELEMENTS IN FIRST ARRAY=");
for(i=0; i<a; i++)
scanf("%d",&A[i]);
printf("ENTER ELEMENTS IN SECOND ARRAY=");
for(j=0; j<a; j++)
scanf("%d",&B[j]);
BSort(A,a);
BSort(B,b);
c=Merge(A,B,C,a,b);
printf("ARRAY AFTER MERGING IS \n");
for(i=0; i<c; i++)
printf("%d\n",C[i]);
}

Write a function to calculate the factorial value of any integer as an argument. Call this function
from main( ) and print the results in main( ).

#include<stdio.h>
int factorial(int a)
{
int fact=1;

while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}

int main()
{
int x,f;
printf("Enter number :") ;
scanf("%d",&x);
f=factorial(x);
printf("The factorial is : %d",f);
return 0;
}
Write a function that receives two numbers as an argument and display all prime numbers
between these two numbers. Call this function from main( ).

#include<stdio.h>
void showprime(int a, int b)
{
int i;
for(i=a+1;i<=b;i++)
{
int j,c=0;
for( j=2;j<i;j++)
{
if(i%j==0)
{
c++;
break;
}
}
if(c==0)
printf("%d\n",i);
}
}

int main()
{
int x,y;
printf("Enter first number : ");
scanf("%d",&x);
printf("Enter second number : ");
scanf("%d",&y);
showprime(x,y);
return 0;
}
Write a program to find the length of string.

#include<stdio.h>
int main( )
{
char str[80];
printf("Enter string: ");
gets(str);
int i;
for(i = 0; str[i] != '\0'; i++);
printf("Length of string is: %d",i);
return 0;
}

Write a program to display string from backward.

#include<stdio.h>

int main( )
{
char str[80]; int i;
printf("Enter string: ");
gets(str);
int l;
for(l = 0; str[l] != '\0'; l++);
for(i = l - 1; i >= 0; i--)
{
printf("%c",str[i]);
}
return 0;
}
Write a program to count number of words in string.

#include<stdio.h>

int main( )
{
char str[80]; int i;
printf("Enter a string: ");
gets(str);
int words = 0;
for(i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
words++;
}
}
printf("The number of words = %d", words+1 );
return 0;
}
Write a program to concatenate one string contents to another.

#include<stdio.h>

int main( )
{
char str1[80], str2[80]; int i;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
int l;
for(l = 0; str1[l] != '\0'; l++);
for(i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}
str1[l] = '\0';
printf("\nThe first string after adding second string content:\n\n");
puts(str1);
return 0;
}

Write a program to check whether a string is a palindrome or not

#include <stdio.h>
#include<string.h>
void main() {
char a[100];
int i,len,flag=0;
printf("\nENTER A STRING: ");
gets(a);
len=strlen(a);
for (i=0;i<len;i++) {
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("\nTHE STRING IS PALINDROME"); else
printf("\nTHE STRING IS NOT PALINDROME");
}

Write a program to delete any vowel from a string

#include <stdio.h>
#include <string.h>

int check_vowel(char);

int main()
{
char s[100], t[100];
int i, j = 0;
printf("Enter a string to delete vowels\n");
gets(s);
for(i = 0; s[i] != '\0'; i++) {
if(check_vowel(s[i]) == 0) {
t[j] = s[i];
j++;
}
}
t[j] = '\0';
strcpy(s, t);
printf("String after deleting vowels: %s\n", s);
return 0;
}

int check_vowel(char c)
{
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return 1;
default:
return 0;
}
}

Write a program to print a string in reverse word by word


#include <stdio.h>
#include<string.h>
void main() {
char a[100], b[20][20];
int i,k=0,j=0,m,n,len;
printf("ENTER A STRING: ");
gets(a);
len=strlen(a);
for (i=0;i<len;i++) {
switch(a[i]){
case ' ': b[k][j]=' ';
b[k][++j]='\0';
j=0;
k++;
break;

default: b[k][j]=a[i];
j++;
break;

}}
b[k][j]=' ';
b[k][j]='\0';
for(m=k; m>=0; m--)
for(n=0; b[m][n]!='\0'; n++)
printf("%c",b[m][n]); }

Write a program to print a pyramid made up of*


#include <stdio.h>
int main()
{
int i, space, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1;space<=rows-i;++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

Write a program to print pascal triangle.

#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}

Write a program to print Inverted full pyramid using *


#include<stdio.h>
int main()
{
int rows, i, j, space;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=rows; i>=1; --i)
{
for(space=0; space < rows-i; ++space)
printf(" ");
for(j=i; j <= 2*i-1; ++j)
printf("* ");
for(j=0; j < i-1; ++j)
printf("* ");

printf("\n");
}
return 0;
}
Write a program to print n terms of fibonacci series
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);
printf("Fibonacci Series: ");

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


{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Write a program to find transpose of a matrix


#include <stdio.h>

int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);

printf("\nEnter elements of matrix:\n");


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}

printf("\nEntered Matrix: \n");


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}

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


for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}

printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
} return 0;}
Write a program to print factors of an integer
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}

Write a program to print LCM of two numbers


#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
minMultiple = (n1>n2) ? n1 : n2;
while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
break;
}
++minMultiple;
}
return 0;
}

Write a program to print the roots of a quadratic equation


#include <stdio.h>
#include <math.h>

int main()
{
double a, b, c, determinant, root1,root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
determinant = b*b-4*a*c;
if (determinant > 0)
{
root1 = (-b+sqrt(determinant))/(2*a);
root2 = (-b-sqrt(determinant))/(2*a);
printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
}
else if (determinant == 0)
{
root1 = root2 = -b/(2*a);
printf("root1 = root2 = %.2lf;", root1);
}
else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-determinant)/(2*a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}

Write a program to find whether the input is a armstrong number or not


#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;

if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}

Write a program to print upper and lower triangle of a matrix.


#include<stdio.h>
void main()
{
int A[4][4],I,J;
printf("ENTER MATRIX A VALUES\n");
for(I=0;I<4;I++)
{
for(J=0;J<4;J++)
{
scanf("%d",&A[I][J]);
}
}
printf("LOWER MATRIX C VALUES ARE :\n");
for(I=0;I<4;I++)
{
printf("\n");
for(J=0;J<=I;J++)
{
printf("%d\t",A[I][J]);
}
printf("\n");
}
printf("UPPER MATRIX C VALUES ARE :\n");
for(I=0;I<4;I++)
{
printf("\n");
for(J=0;J<4;J++)
{
if(J>=I)
printf("%d\t",A[I][J]);
else
printf("\t");
}
printf("\n");
}
printf("\n");
}
Write a program to multiply two matrices

#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}

You might also like