Question On Array
Question On Array
(One Dimensional )
1) Write a C program to input n numbers through keyboard and count the total number of even numbers as well as total number
of odd numbers present among them.
#include<stdio.h>
#include<conio.h>
main()
{
int i, n, a[100], n, e=0, o=0;
clrscr();
printf(“ Enter how many numbers you want to enter : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i] % 2 ==0)
e++;
else
o++;
}
printf(“\n Total number of even numbers : %d”,e);
printf(“\n Total number of odd numbers : %d”,o);
}
2) Write a C program to input n numbers through keyboard and find out the greatest as well as the smallest number present
among them.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, g, s, n;
clrscr();
printf(“ Enter how many array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
s = g = a[0];
for(i=1;i<n;i++)
{
if(a[i] < s)
s = a[i];
else if(a[i] > g)
g = a[i];
}
printf(“ \n Greatest element = %d”,g);
printf(“ \n Smallest element = %d”,s);
}
3) Write a C program to input n numbers through keyboard and find out the greatest and smallest even numbers as well the
greatest and smallest odd numbers among them.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, n, ge, se, go, so;
clrscr();
printf(“ Enter number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
if(a[i] % 2 ==0)
ge = se = a[i];
else
so = go = a[i];
}
for(i=0;i<n;i++)
{
if(a[i] % 2 == 0)
{
if([a[i] >ge)
ge = a[i];
else if(a[i] < se)
se = a[i];
}
else
{
if([a[i] > go)
go = a[i];
else if(a[i] < so)
so = a[i];
}
}
printf(“\n Greatest even number : %d”,ge);
printf(“\n Smallest even number : %d”,se);
printf(“\n Greatest odd number : %d”,go);
printf(“\n Smallest odd number : %d”,so);
}
4) Write a C program to input a decimal number through keyboard and find out it’s binary equivalent number.
#include<stdio.h>
#include<conio.h>
main()
{
int a[25], i = 0, n, j;
clrscr();
printf(“ Enter a decimal number : “);
scanf(“%d”,&n);
while(n %2 != 0)
{
a[i++] = n%2;
n = n / 2;
}
printf(“\n The binary equivalent number is : “);
for(j=--i;j>=0;j--)
{
printf(“%d”,a[j]);
}
}
5) Write a C program to print all the digits of a number in words. Ex. Input – 12345, Output – One Two Three Four Five.
Method #1
#include<stdio.h>
#include<conio.h>
main()
{
int a[20], i=0, j, n;
clrscr();
printf(“ Enter a number : “);
scanf(“%d”,&n);
while(n != 0)
{
a[i++] = n % 10;
n = n / 10;
}
printf(“\n The digits in words : “);
for(j=--I;j>=0;j--)
{
if(a[j] == 0)
printf(“ Zero “);
else if(a[j] == 1)
printf(“ One “);
else if(a[j] == 2)
printf(“ Two “);
else if(a[j] == 3)
printf(“ Three “);
else if(a[j] == 4)
printf(“ Four “);
else if(a[j] == 5)
printf(“ Five “);
else if(a[j] == 6)
printf(“ Six “);
else if(a[j] == 7)
printf(“ Seven “);
else if(a[j] == 8)
printf(“ Eight “);
else
printf(“ Nine “);
}
}
Method #2
#include<stdio.h>
#include<conio.h>
main()
{
int a[20], i=0, j, n;
char b[10][10] = { “ Zero\0”,
“ One\0”,
“ Two\0”,
“ Three\0”,
“ Four\0”,
“ Five\0”,
“ Six\0”,
“ Seven\0”,
“ Eight\0”,
“ Nine\0”
{;
clrscr();
printf(“ Enter a number : “);
scanf(“%d”,&n);
while(n != 0)
{
a[i++] = n % 10;
n = n / 10;
}
printf(“\n The digits in words : “);
for(j=i-1;j>=0;j--)
{
printf(“%s”,b[a[j]]);
}
}
6) Write a C program to input n numbers through keyboard and count how many of them are multiple of 5.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, n, c=0;
clrscr();
printf(“ Enter number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i] % 5 == 0)
c++;
}
print(“\n Total number of elements which are multiple of 5 : %d”,c);
}
7) Write a C program to input n numbers through keyboard and a constant X. Find out whether the constant is present among
them or not.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, n, x;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter the constant to search : “);
scanf(“%d”,&x);
for(i=0;i<n;i++)
{
if(a[i] == x)
{
printf(“\n The constant %d present at %d position”, x,i+1);
break;
}
}
if(i == n)
printf(“\n the constant does not present in the array.”);
}
8) Write a C program to input n numbers through keyboard and count how many of them are prime numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, n, j, c = 0;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=2;j<=a[i]/2;j++)
{
if(a[i] % j == 0)
break;
}
if(j == a[i]/2 + 1)
c++;
}
printf(“\n Total number of prime numbers present in the array : %d”,c);
}
9) Write a C program to input n numbers through keyboard and display all the prime numbers present among them.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n;
clrscr();
printf(“ Enter number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(%d”,&a[i]);
}
printf(“\n The prime numbers are as follows : \n”);
for(i=0;i<ni++)
{
for(j=2;j<=a[i]/2;j++)
{
if(a[i] % j == 0)
break;
}
if(j == a[i] /2 + 1)
printf(“\n prime number = %d”,a[i]);
}
}
10) Write a C program to input n numbers through keyboard and arrange them in ascending order using (a) Selection Sort (b)
Bubble Sort and (c) Insertion Sort method.
Selection Sort Method
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, t;
clrscr();
printf(“ Enter number of array elements : “);
scanf(“%%d”,&n);
printf(“ Enter array elements : “);
for)i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
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;
}
}
}
printf(“\n The sorted array elements are : “);
for(i=0;i<n;i++)
{
printf(“%d, “,a[i]);
}
}
12) Write a C program to input a decimal number through keyboard and display it’s BCD ( Binary Coded Decimal ) equivalent
number.
Method #1
#include<stdio.h>
#include<conio.h>
main()
{
int a[25], i, j, n;
clrscr();
printf(“ Enter a decimal number : “);
scanf(“%d”,&n);
while(n != 0)
{
a[i++] = n%10;
n = n/10;
}
printf( “\n The BCD equivalent number is : “);
for(j=i-1;j>=0;j--)
{
if(a[j] == 0)
printf(“ 0000”);
else if(a[j] == 1)
printf(“ 0001”);
else if(a[j] == 2)
printf(“ 0010”);
else if(a[j] == 3)
printf(“ 0011”);
else if(a[j] == 4)
printf(“ 0100”);
else if(a[j] == 5)
printf(“ 0101”);
else if(a[j] == 6)
printf(“ 0110”);
else if(a[j] == 7)
printf(“ 0111”);
else if(a[j] == 8)
printf(“ 1000”);
else
Printf(“ 1001”);
}
}
Method #2
#include<stdio.h>
#include<conio.h>
main()
{
int a[25], i, j, n;
char b[10][5] = { “0000\0”,
“0001\0”,
“0010\0”,
“0011\0”,
“0100\0”,
“0101\0”,
“0110\0”,
“0111\0”,
“1000\0”,
“1001\0”
};
clrscr();
printf(“ Enter a decimal number : “);
scanf(“%d”,&n);
while(n != 0)
{
a[i++] = n%10;
n = n/10;
}
printf( “\n The BCD equivalent number is : “);
for(j=i-1;j>=0;j--)
{
printf(“ %s”,b[a[j]]);
}
}
13) Write a C program to input a binary number through keyboard and convert it into it’s Octal equivalent number without
converting it into decimal number.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j, oct=0, p=0,l, c, d=1,k=1;
clrscr();
printf(“ Enter a binary number : “);
gets(a);
l = strlen(a);
if(l % 3 ==0)
c = l/3;
else
c = l/3 +1;
for(i=1;i<=c;i++)
{
for(j=1,x=l-1;j<=3;j++,x--)
{
if(x < 0)
break;
else
{
p = p + (int) (a[x]%48) * d;
d = d * 2;
}
}
oct = oct + ( p * k);
k = k * 10;
p = 0;
d = 1;
}
printf(“\n Theoctal equivalent of the binary number : %d”,oct);
}
14) Write a C program to input a binary number through keyboard and convert it into it’s Hexadecimal equivalent number
without converting it into decimal number.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
intb[20], i, j, p=0, l, c, d=1,k=1;
clrscr();
printf(“ Enter a binary number : “);
gets(a);
l = strlen(a);
if(l % 4 ==0)
c = l/4;
else
c = l/4 +1;
for(i=0;i<c;i++)
{
for(j=1,x=l-1;j<=4;j++,x--)
{
if(x < 0)
break;
else
{
p = p + (int) (a[x]%48) * d;
d = d * 2;
}
}
b[i] = p;
p = 0;
d = 1;
}
printf(“\n The octal equivalent of the binary number : “);
for(i=c-1;i>=0;i--)
{
if(b[i] == 10)
printf(“A”);
else if(b[i] == 11)
printf(“B”);
else if(b[i] == 12)
printf(“C”);
else if(b[i] == 13)
printf(“D”);
else if(b[i] == 14)
printf(“E”);
else if(b[i] == 15)
printf(“F”);
else
printf(“%d”, b[i]);
}
}
15) Input n numbers through keyboard and a constant X through keyboard. Write a C program to delete the constant from the list
if the constant is present in the list.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, x;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter the element to delete : “);
scanf(“%d”,&x);
for(i=0;i<n; i++)
{
if(x == a[i])
{
for(j=i;j<n-1;j++)
{
a[j] = a[j+1];
}
n--;
break;
}
}
printf(“\n The array elements after deletion : “);
for(i=0;i<n;i++)
{
printf(“ %d “,a[i]);
}
}
16) Input n numbers through keyboard into an array having duplicate values. Write a C program to count the occurrence of each
distinguished element present in the array except 0.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, c=0;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
if(a[i] != 0)
{
for(j=i+1;j<n;j++)
{
if(a[i] == a[j])
{
a[j] = 0;
c++;
}
}
Printf(“\n The occurrence of %d element = %d times.”,a[i],c);
c = 0;
}
}
}
17) Input n numbers through keyboard into an array having duplicate values. Write a C program to delete all the numbers from
the array which occurs more than once in the array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, k;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i] == a[j])
{
for(k=j;k<n-1;k++)
{
a[k] = a[k+1];
}
j--;
n--;
}
}
}
printf(“\n The array elements after deletion of duplicate elements : \n”);
for(i=0;i<n;i++)
{
printf(“%d “,a[i])
}
}
18) Input n numbers into an array and two constants P and Q, where P denotes the position Q denotes the value. Write a C
program to insert the value Q into the array at position P.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, k, t, p, q;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter the position where you want to insert the constant : “);
scanf(“%d”,&p);
printf(“ Enter the constant to insert : “);
scanf(“%d”,&q);
t = a[p-1];
for(i=p-1; i<=n;i++)
{
a[i] = q;
q = t;
t = a[i+1];
}
n++;
printf(“\n The array after insertion of the constant : \n”);
for(i=0;i<n;i++)
{
printf(“ %d”,a[i]);
}
}
19) Input n numbers into an array in ascending order and a constant P. Write a C program to insert the constant P into the array in
such a way that after insertion the array must be sorted.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, k, t, p;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements in sorted order : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter the constant you want to insert in the sorted array : “);
scanf(“%d”,&p);
for(i=0;i<n;i++)
{
if(p <= a[i])
{
t = a[i];
for(j=I;j<=n;j++)
{
a[j] = p;
p = t;
t = a[j+1];
}
n++;
break;
}
}
printf(“\n The sorted array after insertion of the constant : “);
for(i=0;i<n;i++)
{
printf(“ %d”,a[i]);
}
}
20) Input n numbers into an array and a constant P through keyboard. Write a C program to delete all the occurrences of the
constant from the array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], i, j, n, k, t, p;
clrscr();
printf(“ Enter the number of array elements : “);
scanf(“%d”,&n);
printf(“ Enter array elements in sorted order : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter the constant you want to delete from the array : “);
scanf(“%d”,&p);
for(i=0;i<n;i++)
{
if(p == a[i])
{
for(j=I;j<n-1;j++)
{
a[j] = a[j+1];
}
n--;
i = i-1;
}
}
printf(“\n The array after deletion of all the occurrence of p : “);
for(i=0;i<n;i++)
{
printf(“ %d”,a[i]);
}
}
21) Input n and m elements into two arrays A and B. Write a C program to merge both the arrays and display the resultant array
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], b[100], c[200], i, j, n, m;
clrscr();
printf(“ Enter the number of elements in both the array : “);
scanf(“%d %d”,&m,&n);
printf(“ Enter elements in the first array : “);
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter elements in the second array : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
}
for(i=0;i<m;i++)
{
c[i] = a[i]
}
for(j=0;j<n;j++,i++)
{
c[i] = b[j];
}
printf(“\n The merged array elements are : \n”);
for(i=0;i<m+n;i++)
{
printf(“ %d”,c[i]);
}
}
.
22) Input n and m number of elements into two arrays A and B in sorted order. Write a C program to merge both the arrays in
such a way that after merging the resultant array must be in sorted order.
#include<stdio.h>
#include>conio.h>
main()
{
int a[100], b[100], c[200], i, j, n, m;
clrscr();
printf(“ Enter the number of elements in both the array : “);
scanf(“%d %d”,&m,&n);
printf(“ Enter elements in the first array in sorted order : “);
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter elements in the second array in sorted order : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
}
for(i=0,j=0,k=0;i<m+n;i++)
{
if(j == m)
{
c[i] = b[k];
k++;
}
else if(k == n)
{
c[i] = a[j];
j++;
}
else if(a[j] <= b[k])
{
c[i] = a[j];
j++;
}
else
{
c[i] = b[k];
k++;
}
}
printf(“\n The sorted array after merging : \n”);
for(i=0;i<m+n;i++)
{
printf(“ %d”,c[i]);
}
}
23) Input m and n elements into two arrays through keyboard. Write a C program to find out their union and display the
elements of the union.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], b[100], c[200], i, j, n, m;
clrscr();
printf(“ Enter the number of elements in both the array : “);
scanf(“%d %d”,&m,&n);
printf(“ Enter elements in the first array : “);
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter elements in the second array : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
}
for(i=0;i<m;i++)
{
c[i] = a[i]);
}
For(j=0;j<n;j++)
{
For(k=0;k<m;k++)
{
If(b[j] ==a[k])
Break;
}
If( k == m)
C[i++] = b[j];
}
printf(“\n The elements of the union of two arrays are as follows : \n”);
for(j=0;j<i;j++)
{
printf(“ %d”,c[j]);
}
}
24) Input m and n elements into two arrays through keyboard. Write a C program to find out their intersection and display the
elements of the intersection.
#include<stdio.h>
#include<conio.h>
main()
{
int a[100], b[100], c[200], i, j, n, m;
clrscr();
printf(“ Enter the number of elements in both the array : “);
scanf(“%d %d”,&m,&n);
printf(“ Enter elements in the first array : “);
for(i=0;i<m;i++)
{
scanf(“%d”,&a[i]);
}
printf(“ Enter elements in the second array : “);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
}
for(i=0,k=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i] == b[j])
{
c[k++] = a[i];
break;
}
}
}
printf(“\n The elements of the intersection of two arrays are as follows : \n”);
for(i=0;i<k;i++)
{
printf(“ %d”,c[i]);
}
}
25) Write a C program to input two binary numbers through keyboard and find out their sum also display the resultant in binary
form.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[25], b[25], c[30], d=’0’;
int i, j, k, l1, l2, l;
clrscr();
printf(“ Enter first binary number : “);
gets(a);
printf(“ Enter second binary number : “);
gets(b);
l1 – strlen(a);
l2 = strlen(b);
if(l1 <= l2)
l = l2;
else if(l1 > l2)
l = l1;
else
l = l1 =l2;
for(i=l+1,--l1,--l2;i>0;i--,l1--,l2--)
{
if(l1 < 0)
{
if(l2 == ‘1’ && d == ‘1’)
c[i] = ‘0’;
else if( (l2 == ‘0’ && d == ‘1’) || (l2 == ‘1’ && d == ‘0’) )
{
c[i] = ‘1’;
d = ‘0’;
}
}
else if(l2 < 0)
{
if(l1 == ‘1’ && d == ‘1’)
c[i] = ‘0’;
else if( (l1 == ‘0’ && d == ‘1’) || (l1 == ‘1’ && d == ‘0’) )
{
c[i] = ‘1’;
d = ‘0’;
}
}
else if( (a[i] == ‘0’ && b[i] == ‘0’ && d == ‘1’) || (a[i] == ‘0’ && b[i] == ‘1’ && d ==
‘0’) || (a[i] == ‘1’ && b[i] == ‘0’ && d == ‘0’)
c[i] = ‘1’;
else if((a[i] == ‘0’ && b[i] == ‘1’ && d == ‘1’) || (a[i] == ‘1’ && b[i] == ‘0’ && d ==
‘1’) || (a[i] == ‘1’ && b[i] == ‘1’ && d == ‘0’) )
{
c[i] = ‘0’;
d = ‘1’;
}
else if((a[i] == ‘1’ && b[i] == ‘1’ && d == ‘1’) || (a[i] == ‘1’ && b[i] == ‘1’ && d ==
‘1’) || (a[i] == ‘1’ && b[i] == ‘1’ && d == ‘1’) )
{
c[i] = ‘1’;
d = ‘1’;
}
else
{
c[i] = ‘0’;
d = ‘0’;
}
}
c[i] = d;
c[l+2] = ‘\0’;
printf(“\n the resultant binary number is : %s”,c);
}
26) Write a C program to input two binary numbers through keyboard and find out the resultant after performing Logical AND
operation between them, also display the resultant.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[25], b[25], c[30], d=’0’;
int i, j, k, l1, l2, l;
clrscr();
printf(“ Enter first binary number : “);
gets(a);
printf(“ Enter second binary number : “);
gets(b);
l1 – strlen(a);
l2 = strlen(b);
if(l1 <= l2)
l = l2;
else
l = l1;
for(i=l,--l1,--l2;i>=0;i--,l1--,l2--)
{
if(l1 < 0)
c[i] = b[l2];
else if(l2 < 0)
c[i] = a[l1];
else if(a[l1] == ‘1’ && b[l2] == ‘1’)
c[i] = ‘1’;
else
c[i] = ‘0’;
}
c[l] = ‘\0’;
printf(“\n The resultant after ANDing operation is as follows : %s”,c);
}
27) Write a C program to input two binary numbers through keyboard and find out the resultant after performing Logical OR
operation between them, also display the resultant.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[25], b[25], c[30], d=’0’;
int i, j, k, l1, l2, l;
clrscr();
printf(“ Enter first binary number : “);
gets(a);
printf(“ Enter second binary number : “);
gets(b);
l1 – strlen(a);
l2 = strlen(b);
if(l1 <= l2)
l = l2;
else
l = l1;
for(i=l,--l1,--l2;i>=0;i--,l1--,l2--)
{
if(l1 < 0)
c[i] = b[l2];
else if(l2 < 0)
c[i] = a[l1];
else if( (a[l1] == ‘1’ && b[l2] == ‘1’) || (a[l1] == ‘1’ && b[l2] == ‘0’) || (a[l1] == ‘0’ &&
b[l2] == ‘1’) )
c[i] = ‘1’;
else
c[i] = ‘0’;
}
c[l] = ‘\0’;
printf(“\n The resultant after Logical OR operation is as follows : %s”,c);
}
28) Write a C program to input three binary numbers through keyboard and find out the resultant after performing Logical XOR
operation among them, also display the resultant.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[25], b[25], c[25], d[25];
int i, j, k, l;
clrscr();
printf(“ Enter first binary number : “);
gets(a);
printf(“ Enter second binary number of equal length : “);
gets(b);
printf(“ Enter third binary number of equal length : “);
gets(c);
l = strlen(a);
for(i=l-1;i>=0;i--)
{
if( (a[i] == ‘0’ && b[i] == ‘0’ && c[i] == ‘0’) || (a[i] == ‘0’ && b[i] == ‘1’ && c[i] ==
‘1’) || (a[i] == ‘1’ && b[i] == ‘0’ && c[i] == ‘1’) || (a[i] == ‘1’ && b[i] == ‘1’
&& c[i] == ‘0’))
d[i] = ‘0’;
else if( (a[i] == ‘1’ && b[i] == ‘0’ && c[i] == ‘0’) || (a[i] == ‘0’ && b[i] == ‘1’ && c[i]
== ‘0’) || (a[i] == ‘0’ && b[i] == ‘0’ && c[i] == ‘1’) || (a[i] == ‘1’ && b[i] ==
‘1’ && c[i] == ‘1’) )
d[i] = ‘1’;
}
d[l] = ‘\0’;
printf(“\n the resultant number after XOR operation is as follows : %s”,d);
}
29) Input a string through keyboard and count the total number of vowels, consonants, digits, special symbols as well as the total
number of words present in the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, v=0, c=0, w=0, s=0, d=0;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] == ‘a’ || a[i] == ‘A’ || a[i] == ‘e’ || a[i] == ‘E’ || a[i] == ‘i’ || a[i] == ‘I’ || a[i] == ‘o’
|| a[i] == ‘O’ || a[i] == ‘u’ || a[i] == ‘U’)
v++;
else if(a[i] >= 48 && a[i] <= 57)
d++;
else if( (a[i] >= 65 && a[i] <= 90) || (a[i] >= 97 && a[i] <=122) )
c++;
else if(a[i] == ‘ ‘)
w++;
else
s++;
}
printf(“\n Total number of vowels : %d”,v);
printf(“\n Total number of consonants : %d”,c);
printf(“\n Total number of digits : %d”,d);
printf(“\n Total number of words : %d”,w);
printf(“\n Total number of special symbol : %d”,s);
}
30) Input a string through keyboard and Write a C program to delete all the vowels present in the string and display the resultant
string.
#include<stdio.h>
@include<conio.h>
@include<string.h>
main()
{
char a[100];
int i, j;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i+1]!=’\0’;i++)
{
if(a[i] == ‘a’ || a[i] == ‘A’ || a[i] == ‘e’ || a[i] == ‘E’ || a[i] == ‘i’ || a[i] == ‘I’ || a[i] == ‘o’
|| a[i] == ‘O’ || a[i] == ‘u’ || a[i] == ‘U’)
{
for(j=i;a[j+1]!=’\0’;j++)
{
a[j] = a[j+1];
}
--i;
}
}
printf(“\n The resultant string after deletion of vowels is : %s”,a);
}
31) Input a name through keyboard and write a C program to display the name in the given below format. Ex. Input : Sanjeev
Kumar Das Output : (a) S. K. D. (b) S. K. Das (c) Das S. K.
(a) S. K. D.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j;
clrscr();
printf(“ Enter name in full : “);
gets(a);
printf(“\n The name in initials are as follows : “);
printf(“%c. “,a[0]);
for(i=1;a[i]!=’\0’;i++)
{
if(a[i] == ‘ ‘)
printf(“%c. “,a[i+1]);
}
}
(b) S. K. Das
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j;
clrscr();
printf(“ Enter name in full : “);
gets(a);
printf(“\n The name in initials are as follows : “);
printf(“%c“,a[0]);
for(i=1;a[i]!=’\0’;i++)
{
if(a[i] == ‘ ‘)
{
printf(“.%c “,a[i+1]);
j = i;
}
}
for(i=j+2;a[i]!=’\0’;i++)
{
printf(“%c”,a[i]);
}
}
( c ) Das S. K.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j;
clrscr();
printf(“ Enter name in full : “);
gets(a);
printf(“\n The name in initials are as follows : “);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] == ‘ ‘)
j = i;
}
for(i=j+1;a[i]!=’\0’;i++)
{
printf(“%c”,a[i]);
}
printf(“ %c. “,a[0]);
for(i=1;i<j;i++)
{
if(a[i] == ‘ ‘)
printf(“%c. “,a[i+1]);
}
}
32) Input a string through keyboard and write a C program to count the occurrence of two consecutive vowels present in the
string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, c=0;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if( ( (a[i] == ‘a’ || a[i] == ‘A’ || a[i] == ‘e’ || a[i] == ‘E’ || a[i] == ‘i’ || a[i] == ‘I’ || a[i] ==
‘o’ || a[i] == ‘O’ || a[i] == ‘u’ || a[i] == ‘U’) ) && ( (a[i+1] == ‘a’ || a[i+1] == ‘A’ || a[i+1]
== ‘e’ || a[i+1] == ‘E’ || a[i+1] == ‘i’ || a[i+1] == ‘I’ || a[i+1] == ‘o’ || a[i+1] == ‘O’ ||
a[i+1] == ‘u’ || a[i+1] == ‘U’)))
c++;
}
Printf(“\n The total number of consecutive vowels are : %d”,c);
}
33) Input a string through keyboard in upper case letter and write a C program to convert it into lower case
string and vice-versa.
Uppercase to lowercase
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, c=0;
clrscr();
printf(“ Enter a string in upper case letter : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] >=65 && a[i] <= 90)
a[i] = a[i] + 32;
}
printf(“\n The string in lower case letter is : %s”,a);
}
Lowercase to uppercase
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, c=0;
clrscr();
printf(“ Enter a string in lower case letter : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] >=97&& a[i] <= 122)
a[i] = a[i] - 32;
}
printf(“\n The string in upper case letter is : %s”,a);
}
34) Input a string through keyboard and write a C program to change all upper case letters with lower case letter, all lower case
letters with upper case letters and all blanks with ‘B’.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, c=0;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] >=65 && a[i] <= 90)
a[i] = a[i] + 32;
else if(a[i] >= 97 && a[i] <=122)
a[i] = a[i] – 32;
else if(a[i] == ‘ ‘)
a[i] = ‘ ‘;
}
printf(“\n The string after conversion is : %s”,a);
}
35) Input a string through keyboard and write a C program to count the occurrence of each distinguished character present in the
string except blank.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j, c=1;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] != ‘ ‘)
{
for(j=i+1;a[j]!=’\0’;j++)
{
if(a[i] == a[j])
{
c++;
a[j] = ‘ ‘;
}
}
printf(“\n The character %c occurs %d times.”.a[i],c);
c = 1;
}
}
}
36) Input a string through keyboard and write a C program to delete all the leading vowels from the string which occurs
consecutively
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if( ( (a[i] == ‘a’ || a[i] == ‘A’ || a[i] == ‘e’ || a[i] == ‘E’ || a[i] == ‘i’ || a[i] == ‘I’ || a[i] ==
‘o’ || a[i] == ‘O’ || a[i] == ‘u’ || a[i] == ‘U’) ) && ( (a[i+1] == ‘a’ || a[i+1] == ‘A’ || a[i+1]
== ‘e’ || a[i+1] == ‘E’ || a[i+1] == ‘i’ || a[i+1] == ‘I’ || a[i+1] == ‘o’ || a[i+1] == ‘O’ ||
a[i+1] == ‘u’ || a[i+1] == ‘U’)))
{
for(j=I;a[j]!=’\0’;j++)
{
a[j] = a[j+1];
}
i--;
}
}
printf(“\n The string after deletion of consecutive leading vowels ; %s”,a);
}
37) Input a string through keyboard and write a C program to count the occurrence of each vowels ( ignore case) present in the
string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], c[5] = {“AEIOU”};
static int i, b[5] ;
clrscr();
printf(“ Enter a string : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] == ‘a’ || a[i] == ‘A’)
b[0] = b[0] + 1;
else if(a[i] == ‘e’ || a[i] == ‘E’)
b[1] = b[1] +1;
else if(a[i] == ‘i’ || a[i] == ‘I’)
b[2] = b[2] +1;
else if(a[i] == ‘o’ || a[i] == ‘O’)
b[3] = b[3] + 1;
else if(a[i] == ‘u’ || a[i] == ‘U’)
b[4] = b[4] +1;
}
For(i=0;i<5;i++)
{
printf(“\n The vowel %c occurs %d times.”,c[i],b[i]);
}
}
38) Input a string through keyboard and write a C program to count the length of each word present in the string along with the
word.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100];
int i, j, k=0, l;
clrscr();
printf(“ Enter a string : “);
gets(a);
l = strlen(a);
printf(“\n The length of the word : “);
for(i=0;i<=l;i++)
{
if)a[i] == ‘ ‘ || a[i] == ‘\0’)
{
printf(“ : %d”,k);
k = 0;
printf(“\n The length of the word : “);
}
else
{
printf(“%c”,a[i]);
k++;
}
}
}
39) Input a string through keyboard and write a C program to find out the greatest as well as the smallest word present in the
string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[25], c[25], d[25];
int i, j, k=0, l;
clrscr();
printf(“ Enter a string : “);
gets(a);
l = strlen(a);
for(i=0;a[i]!=’ ‘;i++)
{
b[i] = a[i];
c[i] = a[i];
}
b[i] = ‘\0’;
c[i] = ‘\0’;
fot(j=i+1;j<=l;j++)
{
if(a[j] == ‘ ‘ || a[j] == ‘\0’)
{
d[k] = ‘\0’
if(strlen(b) > strlen(d))
strcpy(b,d);
else if(strlen© < strlen(d))
strcpy(c,d);
k=0;
}
else
d[k++] = a[j];
}
printf(“\n The smallest word present in the string : %s”,b);
printf(“\n The smallest word present in the string : %s”,c);
}
40) Input a string through keyboard and write a C program to reverse the sting ( do not use any other string to store the reversed
string).
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], t;
int i, j, l;
clrscr();
printf(“ Enter a string : “);
gets(a);
l = strlen(a);
printf(“\n The reversed string is : “);
for(i=0,j=l-1;i<l/2;i++,j--)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
printf(“ %s”,a);
}
41) Input a string through keyboard and write a C program to check whether the string is PALINDROME or not. Ex. : madam,
Malayalam are palindrome.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], t;
int i, j, l;
clrscr();
printf(“ Enter a string : “);
gets(a);
l = strlen(a);
for(i=0,j=l-1;i<l/2;i++,j--)
{
if(a[i] != a[j])
{
printf(“\n The string %s is not PALINDROME”);
break;
}
}
if(i == l/2)
printf(“\n The string is PALINDROME”);
}
42) Input a string through keyboard and write a C program to reverse each word present in the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], t;
static int i, j, l, k;
clrscr();
printf(“ Enter a string : “);
gets(a);
l = strlen(a);
printf(“\n The string after reversing each word : “);
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
for(j=i;j>=k;j--)
{
printf(“%c”,a[j]);
}
k = i;
}
}
}
43) Input two string through keyboard and write a C program to find out the concatenated string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100], c[200];
static int i, j;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
for(i=0;a[i]!=’\0’;i++)
{
c[i] = a[i];
}
c[i] = ‘ ‘;
i++;
for(j=0;b[j]!=’\0’;j++,i++)
{
c[i] = b[j];
}
c[i] = ‘\0’;
printf(“\n The concatenated string is : %s”,c);
}
44) Input two string through keyboard and write a C program to append the second string into the first but from the front side and
character by character.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100], t, t1;
static int i, j, k l, l1;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
l = strlen(a);
l1 = strlen(b);
for(i=l;i>=0;i--)
{
t = ‘ ‘;
t1 = b[0];
for(j=0;j<=l1;j++)
{
b[j] = t;
t = t1;
t1 = a[j+1];
}
if(I == l)
b[0] = ‘ ‘;
else
b[0] = a[i];
}
printf(“\n The concatenated string is : %s”,b);
}
45) Input a string through keyboard and write a C program to count the total number of article present in the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[15];
static int i, j, c=0, l;
clrscr();
printf(“ Enter first string : “);
gets(a);
l = strlen(a);
for(i=0;i<l;i++)
{
a[i] = tolower(a[i]);
}
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
b[j] = ‘\0’;
if(strcmp(b,”a”) == 0 || strcmp(b,”an”) == 0 || strcmp(b,”the”) == 0)
c++;
j = 0;
}
else
b[j++] = a[i];
}
printf(“\n Total number of articles present in the string : %d”,c);
}
46) Input a string through keyboard and write a C program to count the occurrence of each article present in the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[15], art[2][4] = {“a\0”,”an\0”,”the\0”};
static int i, j, c[3], l;
clrscr();
printf(“ Enter first string : “);
gets(a);
l = strlen(a);
for(i=0;i<l;i++)
{
a[i] = tolower(a[i]);
}
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
b[j] = ‘\0’;
if(strcmp(b,art[0]) == 0)
c[0] = c[0] + 1;
else if(strcmp(b,art[1]) == 0)
c[1] = c[1] + 1;
else if(strcmp(b,art[2]) == 0)
c[2] = c[2] + 1;
j = 0;
}
else
b[j++] = a[i];
}
For(i=0;i<3;i++)
{
printf(“\n Total number of articles %s is %d.”, art[i],c[i]);
}
}
47) Input two strings through keyboard and write a C program to count the total number of common characters present in both
the strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100];
static int i, j, c=0;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
for(i=0;a[i]!=’\0’;i++)
{
for(j=0;b[j]!=’\0’;j++)
{
if(a[i] == b[j])
{
c++;
break;
}
}
}
printf(“\n Total number of common characters in both the strings are : %d”, c);
}
48) Input two string through keyboard and write a C program to delete all the common character from both the string which are
common.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100];
static int i, j, k, l;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
for(i=0;a[i]!=’\0’;i++)
{
for(j=0;b[j]!=’\0’;j++)
{
if(a[i] == b[j])
{
for(k=i;a[k]!=’\0’;k++)
{
a[k] = a[k+1];
}
for(l=j;b[l]!=’\0’;l++)
{
b[l] = b[l+1];
}
i--;
break;
}
}
}
printf(“\n First string after deletion of common characters : %s”,a);
printf(“\n Second string after deletion of common characters : %s”,b);
}
49) Input two strings through keyboard and write a C program to find out all the common words present in both the strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100], c[20], d[20];
static int i, j, k, l, l1, l2;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
l1 = strlen(a);
l2 = strlen(b);
printf(“\n The common words are : “);
for(i=0;i<=l1;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
c[k] = ‘\0’;
for(j=0;j<=l2;j++)
{
if(b[j] == ‘ ‘ || b[j] == ‘\0’)
{
d[l] = ‘\0’;
if(strcmp(c,d)==0)
{
printf(“%s, “,c);
break;
}
l = 0;
}
else
d[l++] = b[j];
}
k = 0;
}
else
c[k++] = a[i];
}
}
50) Input two strings through the keyboard and write a C program to delete all the common words from both the strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100], c[20], d[20];
static int i, j, k, l, l1, l2, m, n;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
l1 = strlen(a);
l2 = strlen(b);
for(i=0;i<=l1;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
c[k] = ‘\0’;
for(j=0;j<=l2;j++)
{
if(b[j] == ‘ ‘ || b[j] == ‘\0’)
{
d[l] = ‘\0’;
if(strcmp(c,d)==0)
{
for(m=0;m<k;m++)
{
for(n=i-k;a[n]!=’\0’;n++)
{
a[n] = a[n+1];
}
}
for(m=0;m<k;m++)
{
for(n=j-k;b[n]!=’\0’;n++)
{
b[n] = b[n+1];
}
}
l1 = l1 - k;
l2 = l2 - k;
}
l = 0;
}
else
d[l++] = b[j];
}
k = 0;
}
else
c[k++] = a[i];
}
Printf(“\n First string After deletion of common words : %s”,a);
Printf(“\n Second string after deletion of common words : %s”,b);
}
51) Input two strings through keyboard and write a C program to find out the greatest as well as the smallest common word
present in both the strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100], c[20], d[20], e[25][20];
static int i, j, k, l, l1, l2, m;
clrscr();
printf(“ Enter first string : “);
gets(a);
printf(“ Enter second string : “);
gets(b);
l1 = strlen(a);
l2 = strlen(b);
printf(“\n The common words are : “);
for(i=0;i<=l1;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
c[k] = ‘\0’;
for(j=0;j<=l2;j++)
{
if(b[j] == ‘ ‘ || b[j] == ‘\0’)
{
d[l] = ‘\0’;
if(strcmp(c,d)==0)
{
strcpy(e[m++],c);
break;
}
l = 0;
}
else
d[l++] = b[j];
}
k = 0;
}
else
c[k++] = a[i];
}
for(i=0;i<m-1;i++)
{
for(j=i+1;j<m;j++)
{
if(strcmp(e[i],e[j]) > 0)
{
strcpy(c,e[i]);
strcpy(e[i],e[j]);
strcpy(e[j],c);
}
}
}
printf(“\n The smallest common word is : %s”,e[0]);
printf(“\n The greatest common word is : %s”,e[m-1]);
}
52) Input a string through keyboard and write a C program to count the occurrence of each distinguished word present in the
string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[20], d[20];
static int i, j, k, c=1, l, l1, m, n;
clrscr();
printf(“ Enter first string : “);
gets(a);
l1 = strlen(a);
for(i=0;i<=l1;i++)
{
if(a[i] == ‘ ‘ || a[i] ==’\0’)
{
b[k] = ‘\0’;
for(j=i+1;j<=l1;j++)
{
if(a[j] == ‘ ‘ || a[j] == ‘\0’)
{
d[l] = ‘\0’;
if(strcmp(c,d)==0)
{
c++;
for(m=0;m<k;m++)
{
for(n=i-k;a[n]!=’\0’;n++)
{
a[n] = a[n+1];
}
}
l1 = l1 – k;
}
l = 0;
}
else
d[l++] = a[j];
}
Printf(“\n The word ‘ %s ‘ occurs %d times”,b,c);
c = 1;
k = 0;
}
else
b[k++] = a[i];
}
}
53) Input a string and a word through keyboard and write a C program to search whether the word is present in the string or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300], b[20], c[20];
static int i, j, k, l;
clrscr();
printf(“ Enter main string : “);
gets(a);
printf(“ Enter a word to search : “);
gets(b);
l = strlen(a);
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
c[j] = ‘\0’;
if(strcmp(b,c)==0)
{
printf(“\n The word is present in the string”);
break;
}
j = 0;
}
else
c[j++] = a[i];
}
if(i == l +1)
printf(“\n The string is not present in the string”);
}
54) Input a string, a word and an integer constant p through keyboard. Write a C program to insert the word into the string at pth
blank position ( p > 0 ).
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300], b[20], t, t1;
static int i, j, k, l, l1, p, c;
clrscr();
printf(“ Enter main string : “);
gets(a);
printf(“ Enter a word to insert : “);
gets(b);
printf(“ Enter the blank position of insertion : “);
for(i=0;a[i]!=’\0’;i++)
{
If(a[i] == ‘ ‘)
c++;
}
l = strlen(a);
if(p <= c)
{
c = 0;
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘)
c++;
if(c == p)
break;
}
for(j=0;j<=strlen(b);j++)
{
t = ‘ ‘;
t1 = a[i];
for(k=i;k<=l;k++)
{
a[k] = t;
t = t1;
t1 = a[k+1];
}
l++;
}
for(j=i,k=0;b[k]!=’\0’;j++,k++)
{
a[j] = b[k];
}
}
printf(“\n the string after insertion : %s”,a);
}
55) Input a string and a word through keyboard and write a C program to search whether the word is present in the string or not,
if the word is present in the string then delete the word from the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300], b[20], c[20];
static int i, j, k, l;
clrscr();
printf(“ Enter main string : “);
gets(a);
printf(“ Enter a word to search and delete : “);
gets(b);
l = strlen(a);
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
c[j] = ‘\0’;
if(strcmp(b,c)==0)
{
printf(“\n The word is present in the string”);
break;
}
j = 0;
}
else
c[j++] = a[i];
}
for(j=0;j<strlen(b);j++)
{
for(k=i-strlen(b);k<l’;k++)
{
a[k] = a[k+1];
}
l--;
}
printf(“\n The string after deletion of the word : %s”,a);
}
56) Input a string through keyboard and the string contains 1 letter word, 2 letter word upto 10 letter words. Write a C program to
count the total number of 1 letter word, 2 letter word up to 10 letter words present in the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300];
static int b[11], i, j, k, l;
clrscr();
printf(“ Enter main string : “);
gets(a);
l = strlen(a);
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
b[j] = b[j] +1;
j=0;
}
else
j++;
}
for(i=0;i<=10;i++)
{
printf”\n Total no of %d letter words = %s”,i,b[i]);
}
}
57) Input a string through the keyboard and write a C program to display the string in cyclic order as given below :-
Input : SANJEEV
Output : SANJEEV
ANJEEVS
NJEEVSA
JEEVSAN
EEVSANJ
EVSANJE
VSANJEE
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300];
static int i, j, k, l;
clrscr();
printf(“ Enter a string : “);
gets(a);
l = strlen(a);
printf(“\n The string in the cyclic order is as follows : \n”);
for(i=0;i<l;i++)
{
for(j=i,k=0;k<l;k++,j++)
{
if(j == l)
j = 0;
printf(“%c “,a[j]);
}
printf(“\n”);
}
}
58) Input a string through keyboard containing more than one blank spaces between words. Write a C program to delete all the
leading blank spaces between words which occurs more than one.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300];
static int i, j, k;
clrscr();
printf(“ Enter a string containing more than 1 blank spaces between words : “);
gets(a);
for(i=0;a[i]!=’\0’;i++)
{
if(a[i] == ‘ ‘ && a[i+1] == ‘ ‘)
{
for(j=I;a[j]!=’\0’;j++)
{
a[j] = a[j+1];
}
--i;
}
}
printf(“\n The string after deletion of leading blank spaces : %s”,a);
}
59) Input a string through keyboard and write a C program to replace all the word “or” present in the string with the word “and”.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300], b[15], t, t1;
static int i, j, k, l;
clrscr();
printf(“ Enter a string containing more than 1 blank spaces between words : “);
gets(a);
l = strlen(a);
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
b[j] = ‘\0’;
if(strcmp(b,”or”) == 0)
{
t = ‘ ‘;
t1 = a[i];
for(k=I;k<=l;k++)
{
a[k] = t;
t = t1;
t1 = a[k+1];
}
l++;
a[i-2] = ‘a’;
a[i-1] = ‘n’;
a[i] = ‘d’;
}
j = 0;
}
else
b[j++] = a[i];
}
printf(“\n The string after replacement of or with and : %s”,a);
}
60) Input a string as well as two words str1 and str2 through keyboard. Write a C program to check whether the word str1 is
present in the string or not, if the word str1 is present in the string then replace the word in string with the second word str2.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[300], str1[20], str2[20],d[20], t, t1;
static int i, j, k, l, l1, l2;
clrscr();
printf(“ Enter main string : “);
gets(a);
printf(“ Enter string str1 : “);
gets(b);
printf(“ Enter string str2 : “);
gets(c);
l = strlen(a);
l1 = strlen(str1);
l2 = strlen(str2);
if(l1 == l2)
{
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
d[j] = ‘\0’;
if(strcmp(str1,d) == 0)
{
for(k=i-j,j=0;j<strlen(str2);j++,k++)
{
a[k] = str2[j];
}
}
j = 0;
}
else
d[j++] = a[i];
}
}
if(l1 < l2)
{
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
d[j] = ‘\0’;
if(strcmp(str1,d) == 0)
{
for(m=0;m<l2-l1;m++)
{
t = ‘ ‘;
t1 = a[i];
for(n=i;n<=l;n++)
{
a[n] = t;
t = t1;
t1 = a[n+1];
}
l++;
}
for(k=i-j,j=0;j<strlen(str2);j++,k++)
{
a[k] = str2[j];
}
}
j = 0;
}
else
d[j++] = a[i];
}
}
if(l1 > l2)
{
for(i=0;i<=l;i++)
{
if(a[i] == ‘ ‘ || a[i] == ‘\0’)
{
d[j] = ‘\0’;
if(strcmp(str1,d) == 0)
{
for(m=0;m<l1-l2;m++)
{
for(n=i-j;n<l;n++)
{
a[n] = a[n+1];
}
l--;
}
for(k=i-j,j=0;j<strlen(str2);j++,k++)
{
a[k] = str2[j];
}
}
j = 0;
}
else
d[j++] = a[i];
}
}
printf(“\n The main string after replacement : %s”,a);
}
( Two Dimensional )
1) Input a 4 X 4 matrix through keyboard and write a C program to find out the sum of each row as well as each
column elements present in the matrix.
#include<stdio.h>
#include<conio.h>
main()
{
int a[4][4], i, j, sr=0, sc=0;
clrscr();
printf(“ Enter array elements : “);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sacnf(“%d”,&a[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sr = sr + a[i][j];
sc = sc + a[j][i];
}
printf(“\n Sum of %d row = %d”,i,sr);
printf(“\n Sum of %d column = %d”,i,sc);
}
}
2) Input a 4 X 4 matrix through keyboard and write a C program to find out the sum of its forward as well as the
backward diagonal elements.
#include<stdio.h>
#include<conio.h>
main()
{
int a[4][4], i, j, fd=0, bd=0;
clrscr();
printf(“ Enter array elements : “);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sacnf(“%d”,&a[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i + j == 3)
fd = fd + a[i][j];
if(I == j)
bd = bd + a[i][j];
}
}
printf(“\n Sum of the elements of the forward diagonal = %d”,fd);
printf(“\n Sum of the elements of the backward diagonal = %d”,bd);
}
3) Input a 4 X 4 matrix through keyboard and write a C program to display the elements of its upper and lower half
of the forward diagonal as well as backward diagonal.
#include<stdio.h>
#include<conio.h>
main()
{
int a[4][4], i, j, fd=0, bd=0;
clrscr();
printf(“ Enter array elements : “);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sacnf(“%d”,&a[i][j]);
}
}
printf(“\n The upper half of the backward diagonal : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i< j)
printf(“%d “,a[i][j]);
}
printf(“\n”);
}
printf(“\n The lower half of the backward diagonal : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i > j )
printf(“%d “,a[i][j]);
}
printf(“\n”);
}
printf(“\n The upper half of the forward diagonal : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i+ j < 3)
printf(“%d “,a[i][j]);
}
printf(“\n”);
}
printf(“\n The lower half of the backward diagonal : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i + j> 3)
printf(“%d “,a[i][j]);
}
printf(“\n”);
}
}
4) Input two 4 X 4 matrix through keyboard and write a C program to find out their sum and store the result into a
third 4 X 4 matrix.
#include<stdio.h>
#include<conio.h>
main()
{
int a[4][4], b[4][4], c[4][4], i, j;
clrscr();
printf(“ Enter elements for first array : “);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sacnf(“%d”,&a[i][j]);
}
}
printf(“ Enter elements for second array : “);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sacnf(“%d”,&a[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf(“\n The resultant matrix is : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(“ %f “,c[i][j]);
}
printf(“\n”);
}
}
5) Input two 4 X 4 matrix through keyboard and write a C program to find out their multiplication and store the
result into a third 4 X 4 matrix.
6) Input a 4 X 4 matrix through keyboard and write a C program to find out the transposed matrix.
#include<stdio.h>
#include<conio.h>
main()
{
int a[4][4], i, j, c;
clrscr();
printf(“ Enter elements for first array : “);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
sacnf(“%d”,&a[i][j]);
}
}
printf(“\n The main matrix is : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“\n”);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if( i < j)
{
c = a[i][j];
a[i][j] = a[j][i];
a[j][i] = c;
}
}
}
printf(“\n The transposed matrix is : \n”);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(“%d ”,a[i][j]);
}
printf(“\n”);
}
}
7) Input a few line of text through keyboard and write a C program to count the total number of characters present in
each line of text.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80];
static int i, j, k, c;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
printf(“\n The no of characters in the line : “);
while(j < i)
{
while(a[j][k] != ‘\0’)
{
printf(“%c”,a[j][k]);
if(a[j][k] != ‘ ‘)
c++;
k++;
}
printf(“ = %d”,c);
printf(“\n The no of characters in the line : “);
j++;
k = 0;
c = 0;
}
}
8) Input a few line of text through keyboard and write a C program to find out the greatest line as well as the
smallest line present in the text. Display the lines along with their length.
Method 1 ( Using string library function )
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80], b[80], c[80];
static int i, j, k, c;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
strcpy(b, a[0]);
strcpy(c, a[0]);
j = 1;
while( j < i)
{
if(strlen(b) < strlen(a[j]))
strcpy(b, a[j]);
else if(strlen( c) > strlen(a[j])
strcpy(c, a[j]);
j++;
}
printf(“\n The greatest line is : %s”,b);
printf(“\n The length of the greatest line = %d”,strlen(b));
printf(“\n The smallest line is : %s”,c);
printf(“\n The length of the greatest line = %d”,strlen(c));
}
9) Input a few line of text through keyboard and write a C program to find the line which contains the highest
number of words.
Method 1 ( Without using string library function )
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80], b[80];
static int i, j, k, now, x, c;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
for(j=0; j < i; j++)
{
for(k=0; a[j][k]!=’\0’; k++)
{
if(a[j][k] ==’ ‘ )
c++;
}
if(k > 0)
c++;
if(c > now)
{
for(k=0; a[j][k]!=’\0’; k++)
{
b[k] = a[j][k];
}
b[k] = ‘\0’;
now = c;
}
c = 0;
}
printf(“\n The line contains highest number of words : %s”,b);
printf(“\n The total number of words in the line is : %d”,now);
}
10) Input a few line of text through keyboard and write a C program to reverse each line of text present in the text.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80], b[80];
static int i, j, k, I;
char c;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
for(j = 0; j < i; j++)
{
for(k = 0, l = strlen(a[j]) - 1; k < strlen(a[j])/2; k++, l--)
{
c = a[j][k];
a[j][k] = a[j][l];
a[j][l] = c;
}
}
printf(“\n The text in reverse order is as under :- “);
for( j = 0; j < I; j++)
{
printf(“\n %s”, a[j]);
}
}
11) Input a few line of text through keyboard and write a C program to reverse each word present in the text.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80];
static int i, j, k, l, x, y, c;
char t;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
For(j = 0; j < I; j++)
{
For(k = 0; k <= strlen(a[j]); k++)
{
If(a[j][k] ==’ ‘ || a[j][k] == ‘\0’)
{
for(y = 0, x = k-1,l = k – c ; y < c/2 ; x--, l++, y++)
{
t = a[j][x];
a[j][x] = a[j][l];
a[j][l] = t;
}
c = 0;
}
else
c++;
}
}
printf(“\n The text after reversing each word is : “);
for(j = 0 ; j < i; j++)
{
printf(“\n %s”,a[j]);
}
}
12) Input a few line of text through keyboard and the text contains some blank lines, write a C program to delete all
the blank lines present in the text.
Method 1 ( Using string library function )
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80];
static int i, j, k;
clrscr();
printf(“ Enter a few line of text : “);
while(strcmp(gets(a[i], “Stop”)) != 0)
{
i++;
}
for[j = 0; j < i; j++)
{
if(strlen(a[j]) == 0)
{
for(k = j; k < i-1; k++)
{
strcpy(a[k],a[k+1]);
}
i--;
j--;
}
}
printf(“\n The text after deletion of all the blank lines : “);
for(j = 0; j < i; j++)
{
printf(“\n %s”,a[j]);
}
}
13) Input a few line of text through keyboard and write a C program to display only those lines which starts with an
article.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80], b[20];
static int i, j, k;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
printf(“\n The lines starts with an article are : “);
for(j = 0; j < i; j++)
{
k = 0;
while(a[j][k] != ‘ ‘ || a[j][k] !=’\0’)
{
b[k] = a[j][k];
k++;
}
b[k] = ‘\0’;
if(strcmp(b,”A”) == 0 || strcmp(b, “An”) == 0 || strcmp(b,”The”) == 0)
{
printf(“\n %s”,a[j]);
}
}
}
14) Input a few line of text through keyboard and write a C program to delete all the lines from the text which starts
with an article.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80], b[20];
static int i, j, k;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
printf(“\n The lines starts with an article are : “);
for(j = 0; j < i; j++)
{
k = 0;
while(a[j][k] != ‘ ‘ || a[j][k] !=’\0’)
{
b[k] = a[j][k];
k++;
}
b[k] = ‘\0’;
if(strcmp(b,”A”) == 0 || strcmp(b, “An”) == 0 || strcmp(b,”The”) == 0)
{
for(k = j; k < i – 1; k++)
{
strcpy(a[k],a[k+1]);
}
j--;
i--;
}
}
printf(“\n The text after deletion of lines which starts with an article : “);
for(j = 0; j < i; j++)
{
printf(“\n %s”, a[j]);
}
}
15) Input a few line of text through keyboard and write a C program to count the occurrence of each distinguished
word present in the text.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][80], b[20], c[20];
static int i, j, k, l, x, y, ct, m, n, p;
clrscr();
printf(“ Enter a few line of text : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
for(j = 0; j < i; j++)
{
for(k = 0; k <= strlen(a[j]); k++)
{
if(a[j][k] == ‘ ‘ || a[j][k] ==’\0’)
{
b[l] = ‘\0’;
for(x = j; x < i; x++)
{
for(y = k+l; y <= strlen(a[x][y]) ; y++)
{
if(a[x][y] == ‘ ‘ || a[x][y] == ‘\0’)
{
c[m] = ‘\0’;
if(strcmp(b,c) == 0)
{
ct++;
for(n = 0; n < m; n++)
{
for(p = y-m; a[x][p] != ‘\0’; p++)
{
a[x][p] = a[x][p+1];
}
}
}
m = 0;
}
else
c[m++] = a[x][y];
}
}
l = 0;
printf(“\n The word %s occurs %d times in the text.”,b, ct);
}
else
b[l++] = a[j][k];
}
}
}
16) Input the names of few students of a class through keyboard and write a C program to short the names in
ascending order alphabetically.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][50], b[50];
static int i, j, k;
clrscr();
printf(“ Enter the name of students of the class : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
for(j = 0; j < i-1; j++)
{
for(k = j+1; k < i; k++)
{
if(strcmp(a[j],a[k]) > 0)
{
strcpy(b, a[j]);
strcpy(a[j], a[k]);
strcpy(a[k], b);
}
}
}
printf(“\n The names in ascending order alphabetically is : “);
for( j = 0; j < i; j++)
{
printf(“\n %s”, a[j]);
}
}
17) Input few names of students of a class through keyboard. Write a C program to check whether a particular student
is present in the class or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][50], b[50];
static int i, j;
clrscr();
printf(“ Enter the name of students of the class : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
printf(“\n Enter the name of the student to search : “);
gets(b);
for(j = 0; j < i; j++)
{
if(strcmp(a[j], b) == 0)
{
printf(“:\n The student %s is present in the class.”, b);
break;
}
}
if(j == i)
printf(“\n The student %s is not present in the class.”, b);
}
18) Input the names of few students of a class through keyboard. Write a C program to display the name of the
students in the format given below. Ex. Input : Sanjeev Kumar Das Output : Das S.K..
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][50];
static int i, j, k, l;
clrscr();
printf(“ Enter the name of students of the class : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
printf(“\n The name of the students in the given format are as follows : \n”);
for(j = 0; j < i; j++)
{
for(k = 0; a[j][k] != ‘\0’; k++)
{
if(a[j][k] == ‘ ‘)
l = k;
}
for(k = l + 1; a[j][k] != ‘\0’; k++)
{
printf(“%c”,a[j][k]);
}
printf(“ %c.”,a[j][0]);
for(k = 1; k < l ; k++)
{
if(a[j][k] == ‘ ‘)
printf(“ %c.”,a[j][k]);
}
printf(“\n”);
l = 0;
}
}
19) Input the names of few students of a class and write a program in C to count the number of students whose names
starts with each distinguished alphabet.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100][50], b[27] = {“ ABCDEFGHIJKLMNOPQRSTUVWXYZ”};
static int i, j, k, l, c[27];
clrscr();
printf(“ Enter the name of students of the class : “);
while(strlen(gets(a[i])) > 0)
{
i++;
}
for(j = 0; j < i; j++)
{
for(l = 1; l < 27; l++)
{
if(a[j][0] == b[l])
{
c[l] = c[l] + 1;
break;
}
}
}
printf(“\ The total number of students whose names starts with different alphabets are as follows : “);
for(j = 1; j < 27; j++)
{
if(c[j] > 0)
printf(“\n The number of students having name start with %c = %d”,b[j], c[j]);
}
}
20) Input a number through keyboard and write a C program to display the number in words in the format given
below. Ex. Input : 12345 Output : Twelve thousand three hundred forty five.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[20][10] = {
“\0”,
“ONE\0”,
“TWO\0”,
“THREE\0”,
“FOUR\0”,
“FIVE\0”,
“SIX\0”,
“SEVEN\0”,
“EIGHT\0”,
“NINE\0”,
“TEN\0”,
“ELEVEN\0”,
“TWELVE\0”,
“THIRTEEN\0”,
“FORTEEN\0”,
“FIFTEEN\0”,
“SIXTEEN\0”,
“SEVENTEEN\0”,
“EIGHTEEN\0”,
“NINTEEN\0”
};
Char b[8][10] = {“TWENTY\0”,
“THIRTY\0”,
“FORTY\0”,
“FIFTY\0”,
“SIXTY\0”,
“SEVENTY\0”,
“EIGHTY\0”,
“NINTY\0” };
static int i, j, k;
long int n;
clrscr();
printf( Enter a number : “);
scanf(“%ld”,&n);
printf(“\n The digit of the number in words is : “);
if(n >= 10000000 && n <1000000000)
{
i = n /10000000;
if(i >19)
printf(“ %s %s CRORE “,b[i/10 – 2], a[i % 10]);
else
printf(“ %s CRORE “,a[i]);
n = n % 10000000;
}
if(n >=100000 && I <10000000)
{
i = n /100000;
if(i >19)
printf(“ %s %s LAKH “,b[i/10 – 2], a[i % 10]);
else
printf(“ %s LAKH “,a[i]);
n = n % 100000;
}
if(n >=1000 && I <100000)
{
i = n /1000;
if(i >19)
printf(“ %s %s THOUSAND “,b[i/10 – 2], a[i % 10]);
else
printf(“ %s THOUSAND “,a[i]);
n = n % 1000;
}
if(n >= 100 && n < 1000)
{
printf(“%s HUNDRED “,a[n/100]);
n = n % 100;
}
if(n >= 1 && n < 100)
{
i= n / 10)
if(i > 1)
printf(“%s %s”, b[i-2],a[n%10]);
else
printf(“%s”, a[n]);
}
}
21) According to Georgian calendar the first day of the year1900 was Monday. Input a month number from 1 to 12
through keyboard and display the calendar of that month and year in the format given below –
Ex. Input month number from 1 to 12 : 2
Input year number : 2017
Output : Calendar for the month : February, 2017
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
--------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
main()
{
Char month[13][10] = { “ \0”,
“January\0”,
“February\0”,
“March\0”,
“April\0”,
“May\0”,
“June\0”,
“July\0”,
“August\0”,
“September\0”,
“October\0”,
“November\0”,
“December\0”
};
char day[7][4] = { “Mon\0”,
“Tue\0”,
“Wed\0”,
“Thu\0”,
“Fri\0”,
“Sat\0”,
“Sun\0”
};
int year, mon, i, j, x, y, d, e, le, nle, k;
int m[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
clrscr();
printf(“ Enter a month number from 1 to 12 : “);
scanf(“%d”, &mon);
printf(“ Enter a year later than 1900 : “);
scanf(“%d”, &year);
d = year -1901;
le = d / 4;
nle = d – le;
e = nle + 2 * (le + 1);
if(year % 4 == 0 && mon > 2)
e = e + m[mon – 1] + 1;
else
e = e + m[mon – 1];
j = e % 7;
if(j == 0)
j = 6;
x = 5;
y = 10;
gotoxy(x, y);
printf(“ Calendar for the month : %s %d”, month[mon], year);
x++;
gotoxy(x, y);
for(i = 0; i < 7 i++)
{
gotoxy(x, y +i*7);
printf(“%s”, day[i]);
}
gotoxy(++x, y);
for(i = 1; i <= 50; i++)
{
printf(“-“);
}
x++;
if(mon ==1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12)
k = 31;
else if(mon == 4 || mon == 6 || mon == 9 || mon == 11)
k = 30;
else if(mon == 2 && year % 4 == 0)
k = 29;
else
k = 28;
for(i = 1; i <= k; i++, j++)
{
gotoxy(x, y + j*7);
printf(“ %d“, i);
if(j == 7)
{
j = -1;
x++;
}
}
}