C Important Programs With Logo
C Important Programs With Logo
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int s, a, b, c, area;
clrscr();
printf("Enter the values of a,b and c\n");
scanf ("%d %d %d", &a, &b, &c);
/* compute s*/
s = (a + b + c) / 2;
area = sqrt ( s * (s-a) * (s-b) * (s-c));
printf ("Area of a triangale = %d\n", area);
}
/*-----------------------------
Output
Enter the values of a,b and c
3
4
5
Area of a triangale = 6
------------------------------*/
*-----------------------------
Output
Enter the radius of a circle
3.2
Area of a circle = 32.17
------------------------------*/
3. Write a C program to find the simple interest, given principle, rate of interest and year
#include <stdio.h>
#include <conio.h>
void main()
{
float p, r, si;
int t;
clrscr();
printf("Enter the values of p,r and t\n");
scanf ("%f %f %d", &p, &r, &t);
si = (p * r * t)/ 100.0;
printf ("Amount = Rs. %5.2f\n", p);
printf ("Rate = Rs. %5.2f%\n", r);
printf ("Time = %d years\n", t);
printf ("Simple interest = %5.2f\n", si);
}
/*-----------------------------
Output
Enter the values of p,r and t
2000
8
3
Amount = Rs. 2000.00
Rate = Rs. 8.00%
Time = 3 years
Simple interest = 480.00
-----------------------------*/
#include <stdio.h>
int main()
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
int number;
printf("Enter a number \n");
scanf("%d",&number);
if(number >=0)
printf("%d is a positive number \n", number);
else
printf("%d is a negative number \n", number);
}
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
return 0;
}
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0;
}
7. Write a C program to find and output all the roots of a quadratic equation.
#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
8. Write a C program to simulate a simple calculator to perform arithmetic operations like addition,
Subtraction, multiplication and division only on integers. Error message should be reported if any
attempt is made to divide by zero.
#include <stdio.h>
#include <conio.h>
void main()
{
char oper; /* oper is an operator to be selected */
float n1, n2, result;
clrscr();
printf ("Simulation of a Simple Calculator\n\n");
printf("Enter two numbers\n");
scanf ("%f %f", &n1, &n2);
fflush (stdin);
printf("Enter the operator [+,-,*,/]\n");
scanf ("%c", &oper);
switch (oper)
{
}
/*-----------------------------
Output
Simulation of Simple Calculator
{
int fib1 =0, fib2 =1, fib3, num, count =0;
11.Write a C program to find the GCD and LCM of two integers , output the results along with the given
integers. Use Euclids’ algorithm.
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr();
printf("Enter two numbers\n");
scanf("%d %d", &num1,&num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
}
remainder = num1 % num2;
while(remainder !=0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d \n", num1,num2,gcd);
printf("LCM of %d and %d = %d \n", num1,num2,lcm);
} /* End of main() */
12. Write a C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the
computed sums on two different lines with suitable headings.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N, oddSum = 0, evenSum = 0;
\clrscr();
}
/*-----------------------------
Output
Enter the value of N
10
Sum of all odd numbers = 25
Sum of all even numbers = 30
------------------------------*/
13. Write a C program to reverse a given integer number and check whether it is a palindrome. Output the
given numbers with suitable message.
#include <stdio.h>
void main()
{
int num, temp, remainder, reverse =0;
printf("Enter an integer \n");
scanf("%d",&num);
/* original number is stored at temp */
temp = num;
while(num >0)
{
remainder = num %10;
reverse = reverse *10+ remainder;
num /=10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", reverse);
if(temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
}
14. Write a C program to find the value of sin(x) using the series.
include <stdio.h>
#include <math.h>
#include <stdlib.h>
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;
x1 = x;
16. Write a C program to generate and print prime numbers in a given range.
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num1, num2, i, j, flag, temp, count =0;
printf("Enter the value of num1 and num2 \n");
scanf("%d %d",&num1,&num2);
if(num2 <2)
{
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
if((i % j)==0)
{
flag =1;
break;
}
}
if(flag ==0)
{
printf("%d\n", i);
count++;
}
}
printf("Number of primes between %d & %d = %d\n", temp, num2, count);
}
17.Write a C program to find the number of integers divisible by 5 between the given range N1 and N2,
where N1 < N2 and are integers. Also find the sum of all these integer numbers that divisible by 5 and
compute the average for the same
#include<stdio.h>
#include<conio.h>
int main()
{
int n1,n2,div,sum=0;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
18.Write a C program to read N integers (zero, +ve and –ve) into an array A and do the following
a) Find the sum of negative numbers
b) Find the sum of positive numbers and
c) Find the average of all input numbers
Output the various results computed with proper headings
#include <stdio.h>
#include <conio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, N, negsum=0, posum=0;
float total=0.0, averg;
clrscr();
printf ("Enter the value of N\n");
scanf("%d", &N);
printf("Enter %d numbers (-ve, +ve and zero)\n", N);
for(i=0; i< N ; i++)
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
{
scanf("%d",&array[i]);
fflush(stdin);
}
printf("Input array elements\n");
for(i=0; i< N ; i++)
{
printf("%+3d\n",array[i]);
}
/* Summing begins */
for(i=0; i< N ; i++)
{
if(array[i] < 0)
{
negsum = negsum + array[i];
}
else if(array[i] > 0)
{
posum = posum + array[i];
}
else if( array[i] == 0)
{
;
}
total = total + array[i] ;
}
averg = total / N;
printf("\nSum of all negative numbers = %d\n",negsum);
printf("Sum of all positive numbers = %d\n", posum);
printf("\nAverage of all input numbers = %.2f\n", averg);
} /*End of main()*/
/*-------------------------------------
Output
Enter the value of N
5
Enter 5 numbers (-ve, +ve and zero)
5
-3
0
-7
6
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
19. Write a C program to input N numbers and store them in an array. Conduct a linear search for a given
key number and report success or failure in the form of a suitable message.
#include <stdio.h>
#include <conio.h>
void main()
{
int array[10];
int i, N, keynum, found=0;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
printf("Enter the elements one by one\n");
for(i=0; i<N ; i++)
{
scanf("%d",&array[i]);
}
printf("Input array is\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
printf("Enter the element to be searched\n");
scanf("%d", &keynum);
/* Linear search begins */
for ( i=0; i < N ; i++)
{
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
20.Write a C program to sort N numbers in ascending order using Bubble sort and print both the given
and the sorted array with suitable headings
include <stdio.h>
void main()
{
int a[20],n,i,pass,temp;
printf("Enter the size of an array\n");
scanf("%d",&n);
printf("Enter the array elements to be sorted:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Unsorted array elements are:\n");
for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
for(pass=1;pass<n;pass++)
{
for(i=0;i<n-pass;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
a[i+1]=temp;
}
}
}
printf("Sorted array elements are:\n");
for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
}
21.Write a C program to accept N numbers sorted in ascending order and to search for a given number
using binary search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
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 is not present in the list.\n", search);
return 0;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
22.Write a C program to input real numbers and find the mean, variance and standard deviation
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;
clrscr();
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter %d real numbers\n",n);
for(i=0; i<n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for(i=0; i<n; i++)
{
sum = sum + x[i];
}
avrg = sum /(float) n;
/* Compute varaience and standard deviation */
for(i=0; i<n; i++)
{
sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;
SD = sqrt(var);
printf("Average of all elements = %.2f\n", avrg);
printf("Varience of all elements = %.2f\n", var);
printf("Standard deviation = %.2f\n", SD);
} /*End of main()*/
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
/*--------------------------
Output
Enter the value of N
6
Enter 6 real numbers
12
34
10
50
42
33
Average of all elements = 29.66
Varience of all elements = 213.89
Standard deviation = 14.62
-------------------------------------*/
23.Write a C program to read in four integer numbers into an array and find the average of largest two of
the given numbers without sorting the array. The program should output the given four numbers and the
average with suitable headings
#include <stdio.h>
#include <conio.h>
#define MAX 4
void main()
{
int a[MAX], i, l1,l2,temp;
clrscr();
printf("Enter %d integer numbers\n", MAX);
for (i=0; i < MAX; i++)
{
scanf("%d", &a[i]);
}
printf("Input interger are\n");
for (i=0; i < MAX; i++)
{
printf("%5d", a[i]);
}
printf("\n");
l1 = a[0]; /*assume first element of array is the first largest*/
l2 = a[1]; /*assume first element of array is the second largest*/
if (l1 < l2)
{
temp = l1;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
l1 = l2;
l2 = temp;
}
for (i=2;i<4;i++)
{
if (a[i] >= l1)
{
l2 = l1;
l1 = a[i];
}
else if(a[i] > l2)
{
l2= a[i];
}
}
printf("\n%d is the first largest\n", l1);
printf("%d is the second largest\n", l2);
printf("\nAverage of %d and %d = %d\n", l1,l2, (l1+l2)/2);
}
/*-----------------------------------
Output
Enter 4 integer numbers
45
33
21
10
Input interger are
45 33 21 10
45 is the first largest
33 is the second largest
Average of 45 and 33 = 39
-----------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 10
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
voidmain()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;
clrscr();
printf("Enter the order of the polynomial\n");
scanf("%d", &N);
printf("Enter the value of x\n");
scanf("%f", &x);
/*Read the coefficients into an array*/
printf("Enter %d coefficients\n",N+1);
for (i=0;i <= N;i++)
{
scanf("%d",&a[i]);
}
polySum = a[0];
for (i=1;i<= N;i++)
{
polySum = polySum * x + a[i];
}
power = N;
/*power--;*/
printf("Given polynomial is:\n");
for (i=0;i<= N;i++)
{
if (power < 0)
{
break;
}
/* printing proper polynomial function*/
if (a[i] > 0)
printf(" + ");
else if (a[i] < 0)
printf(" - ");
else
printf (" ");
printf("%dx^%d ",abs(a[i]),power--);
}
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
25. Write a C program to read two matrices A (MxN) and B(MxN) and perform addition AND Subtraction
of A and B. Find the trace of the resultant matrix.
#include <stdio.h>
#include <conio.h>
void main()
{
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, M,N, option;
void trace (int arr[][10], int M, int N);
clrscr();
printf("Enter the order of the matrice A and B\n");
scanf("%d %d", &M, &N);
printf("Enter the elements of matrix A\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{ scanf("%d",&A[i][j]); }
}
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
printf("MATRIX A is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
}
}
printf("Sum matrix is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",sumat[i][j]) ;
}
printf("\n");
}
trace (sumat, M, N);
break;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
26. Write a C program to read A (MxN), find the transpose of a given matrix and output both the
input matrix and the transposed matrix.
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix\n");
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
transpose[d][c] = matrix[c][d];
27. Write a C program to read a matrix A (MxN) and to find the following using functions
}
}
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
}
return(rsum);
}
28. Write a C program to read two integers M and N and to swap their values. Use a user-defined
function for swapping. Output the values of M and N before and after swapping with suitable
messages
#include <stdio.h>
void swap(float*ptr1,float*ptr2);
void main()
{
float m, n;
printf("Enter the values of M and N \n");
scanf("%f %f",&m,&n);
printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);
swap(&m,&n);
printf("After Swapping:M = %5.2ftN = %5.2f\n", m, n);
}
/* Function swap - to interchanges the contents of two items */
void swap(float*ptr1,float*ptr2)
{
float temp;
temp =*ptr1;
*ptr1 =*ptr2;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
*ptr2 = temp;
}
29. Write a C program to read N integers and store them in an array A, and so find the sum of all these
elements using pointer. Output the given array and the computed sum with suitable heading.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
void main()
{
int i,n,sum=0;
int *a;
clrscr();
printf("Enter the size of array A\n");
scanf("%d", &n);
a=(int *) malloc(n*sizeof(int)); /*Dynamix Memory Allocation */
printf("Enter Elements of First List\n");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
/*Compute the sum of all elements in the given array*/
for(i=0;i<n;i++)
{
sum = sum + *(a+i);
}
printf("Sum of all elements in array = %d\n", sum);
} /* End of main() */
/*----------------------------
Output
Enter the size of array A
4
Enter Elements of First List
10
20
30
40
Sum of all elements in array = 100
-------------------------------------*/
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
30. Write a C program to read a string and check whether it is a palindrome or not (without using
library functions). Output the given string along with suitable message.
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25]={'\0'};
int i, length =0, flag =0;
printf("Enter a string \n");
gets(string);
/* keep going through each character of the string till its end */
for(i =0; string[i]!='\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for(i = length -1; i >=0; i--)
{
reverse_string[length - i -1]= string[i];
}
/* Check if the string is a Palindrome */
31.Write a C program to read two strings and concatenate them (without using library functions).
Output the concatenated string along with the given string.
#include<stdio.h>
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
#include<string.h>
void concat(char[], char[]);
int main() {
char s1[50], s2[30];
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("nConcated string is :%s", s1);
return (0);
}
32. Write a C program to read an English sentence and replace lowercase letter by uppercase and vice-
versa. Output the given sentence as well as the case converted sentence on two different lines.
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
}
sentence[i]='\0';
count = i; /*shows the number of chars accepted in a sentence*/
printf("The given sentence is : %s",sentence);
printf("\nCase changed sentence is: ");
for(i=0; i < count; i++)
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
{
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
} /*End of main()*/
/*------------------------------
Output
Enter a sentence
Mera Bharat Mahan
The given sentence is : Mera Bhaaat Mahan
Case changed sentence is: mERA bHARAT mAHAN
------------------------------------------------*/
33. Write a C program read a sentence and count the number of number of vowels and consonants in
the given sentence.
#include <stdio.h>
void main()
{
char sentence[80];
int i, vowels =0, consonants =0, special =0;
printf("Enter a sentence \n");
gets(sentence);
for(i =0; sentence[i]!='\0'; i++)
{
if((sentence[i]=='a'|| sentence[i]=='e'|| sentence[i]==
'i'|| sentence[i]=='o'|| sentence[i]=='u')||
(sentence[i]=='A'|| sentence[i]=='E'|| sentence[i]==
'I'|| sentence[i]=='O'|| sentence[i]=='U'))
{
vowels = vowels +1;
}
else
{
consonants = consonants +1;
}
if(sentence[i]=='t'||sentence[i]=='\0'|| sentence[i]==' ')
{
special = special +1;
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
}
}
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
}
34. Write a C program to read N names, store them in the form of an array and sort them in alphabetical
order. Output the give names and the sorted names in two columns side by side with suitable heading
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;
clrscr();
printf("Enter the value of N\n");
scanf("%d", &N);
Output
Enter the value of N
3
Enter 3 names
Monica
Laxmi
Anand
----------------------------------------
Input Names Sorted names
----------------------------------------
Monica Anand
Laxmi Laxmi
Anand Monica
----------------------------------------
---------------------------------------- */
35. Write a C program to convert the given binary number into decimal
#include <stdio.h>
void main()
{
int num, binary_val, decimal_val =0, base =1, rem;
printf("Enter a binary number(1s and 0s) \n");
scanf("%d",&num);/* maximum five digits */
binary_val = num;
while(num >0)
{
rem = num %10;
decimal_val = decimal_val + rem * base;
num = num /10;
base = base *2;
}
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
36. Program to accept N integer number and store them in an array AR. The odd elements in the AR are
copied into OAR and other elements are copied into EAR. Display the contents of OAR and EAR
#include <stdio.h>
void main()
{
long int ARR[10], OAR[10], EAR[10];
int i,j=0,k=0,n;
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]);
fflush(stdin);
}
/*Copy odd and even elemets into their respective arrays*/
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]);
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
}
} /*End of main()*/
/*-------------------------------------
Output
Enter the size of array AR
6
Enter the elements of the array
12
345
678
899
900
111
The elements of OAR are
345
899
111
The elements of EAR are
12
678
900
---------------------------------------*/
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
printf(" %ld",f3 );
}
return 0;
}
Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
38. Write a C program to insert a particular element in a specified position in a given array
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
41.Write a C program to accept a string and a substring and check if the substring is present in the given
String.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],search[10];
int count1=0,count2=0,i,j,flag;
clrscr();
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
puts("Enter a string:");
gets(str);
puts("Enter search substring:");
gets(search);
while (str[count1]!='\0')
count1++;
while (search[count2]!='\0')
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=search[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts("SEARCH SUCCESSFUL!");
else
puts("SEARCH UNSUCCESSFUL!");
getch();
}
/*------------------------------
Output
Enter a string:
Hello how are you?
Enter search substring:
how
SEARCH SUCCESSFUL!
------------------------------*/
Volume = a3
#include<stdio.h>
int main(){
float a;
float surface_area,volume;
printf("Enter size of any side of a cube : ");
scanf("%f",&a);
surface_area = 6 * (a * a);
volume = a * a * a;
printf("Surface area of cube is: %.3f",surface_area);
printf("\nVolume of cube is : %.3f",volume);
return 0;
}
Sample output:
Enter size of any side of a cube: 3
Surface area of cube is: 54.000
Volume of cube is: 27.000
43. Write a C program to accept a decimal number and convert it binary and count the number of 1's in the
binary number.
#include<stdio.h>
int main(){
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
44. Write a c program to find whether a given year is leap year or not
#include<stdio.h>
int main(){
int year;
if(((year%4==0)&&(year%100!=0))||(year%400==0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}
Sample output:
Enter any year: 2010
2010 is not a leap year
45. Write a c program to swap the contents of two numbers using bitwise XOR operation. Don't use either
the temporary variable or arithmetic operator.
#include <stdio.h>
void main()
{
long i,k;
printf("Enter two integers\n");
scanf("%ld %ld",&i,&k);
printf("\nBefore swapping i= %ld and k = %ld",i,k);
i = i^k;
k = i^k;
i = i^k;
printf("\nAfter swapping i= %ld and k = %ld",i,k);
}
/*------------------------------------------
Output
Enter two integers
23 34
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
46. Write a c program to convert given number of days to a measure of time given in years, weeks and
days. For example 375 days is equal to 1 year 1 week and 3 days (ignore leap year)
#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;
printf("Enter the number of days\n");
scanf("%d",&ndays);
year = ndays/365;
week = (ndays % 365)/DAYSINWEEK;
days = (ndays%365) % DAYSINWEEK;
printf ("%d is equivalent to %d years, %d weeks and %d days\n",
ndays, year, week, days);
}
47. Program to accepts two strings and compare them. Finally it prints whether both are equal, or first
string is greater than the second or the first string is less than the second string.( don’t use library function)
#include<stdio.h>
int main() {
char str1[30], str2[30];
int i;
gets(str2);
i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
return (0);
}
48. Program to accept two strings and concatenate them, i.e. The second string is appended to the end of
the first string. ( don’t use library function)
#include<stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
for(i=0; s1[i]!='\0';++i);/* i contains length of string s1. */
for(j=0; s2[j]!='\0';++j,++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
return0;
}
49. Write a c program to find the length of a string without using the built-in function also check
whether it is a palindrome or not.
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
50. Accept a string and find the number of times the word 'the' appears in it
#include<stdio.h>
#include<conio.h>
void main()
{
int count=0,i,times=0,t,h,e,space;
char str[100];
clrscr();
puts("Enter a string:");
gets(str);
/*Traverse the string to count the number of characters*/
while (str[count]!='\0')
{
count++;
}
/*Finding the frequency of the word 'the'*/
for(i=0;i<=count-3;i++)
{
t=(str[i]=='t'||str[i]=='T');
h=(str[i+1]=='h'||str[i+1]=='H');
e=(str[i+2]=='e'||str[i+2]=='E');
space=(str[i+3]==' '||str[i+3]=='\0');
Prepared By
ERODE SENGUNTHAR ENGINEERING COLLEGE
(Approved by AICTE, New Delhi, Permanently Affiliated to Anna University, Chennai,
Accredited by National Board of Accreditation (NBA), New Delhiand
National Assessment & Accreditation Council (NAAC), Bangalore with ‘A’ Grade)
if ((t&&h&&e&&space)==1)
times++;
}
printf("Frequency of the word \'the\' is %d\n",times);
getch();
}
/*-----------------------------------------------------
Output
Enter a string:
The Teacher's day is the birth day of Dr.S.Radhakrishnan
Frequency of the word 'the' is 2
----------------------------------------------------*/
Prepared By