0% found this document useful (0 votes)
92 views

C Important Programs With Logo

The document contains 11 code snippets showing C programs for basic mathematical and logical operations: 1) Calculating the area of a triangle given three sides 2) Calculating the area of a circle given the radius 3) Calculating simple interest given principal, rate, and time 4) Checking if an integer is even or odd 5) Finding the greatest of three numbers 6) Calculating the GCD and LCM of two numbers using Euclid's algorithm 7) The code snippets are presented with input/output examples and are from the Training and Placement Cell of Erode Sengunthar Engineering College in India.

Uploaded by

Dinesh Karthik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

C Important Programs With Logo

The document contains 11 code snippets showing C programs for basic mathematical and logical operations: 1) Calculating the area of a triangle given three sides 2) Calculating the area of a circle given the radius 3) Calculating simple interest given principal, rate, and time 4) Checking if an integer is even or odd 5) Finding the greatest of three numbers 6) Calculating the GCD and LCM of two numbers using Euclid's algorithm 7) The code snippets are presented with input/output examples and are from the Training and Placement Cell of Erode Sengunthar Engineering College in India.

Uploaded by

Dinesh Karthik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

1. Write a C program to find the area of a triangle, given three sides.

#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
------------------------------*/

2. Write a C program to find the area of a circle, given the radius


#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.142
void main()
{

float radius, area;


clrscr();
printf("Enter the radius of a circle\n");
scanf ("%f", &radius);
area = PI * pow (radius,2);
printf ("Area of a circle = %5.2f\n", area);
}
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

*-----------------------------
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
-----------------------------*/

4. Write a C program to check whether a given integer is odd or even.

#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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

{
int n;

printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}

5.Write a C program to check whether a given integer number is positive or negative.


#include <stdio.h>
void main()
{

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);
}

6. Write a C program to find the biggest of three numbers.


/* C program to find largest number using if statement only */
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
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)
{

case '+': result = n1 + n2;


break;
case '-': result = n1 - n2;
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

case '*': result = n1 * n2;


break;
case '/': result = n1 / n2;
break;
default : printf ("Error in operation\n");
break;
}

printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);

}
/*-----------------------------
Output
Simulation of Simple Calculator

Enter two numbers


35
Enter the operator [+,-,*,/]
+
3.00 + 5.00= 8.00

9. Write a C program to find the sum of 'N' natural numbers.


#include <stdio.h>
int main()
{
int n, count, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
count=1;
while(count<=n) /* while loop terminates if count>n */
{
sum+=count; /* sum=sum+count */
++count;
}
printf("Sum = %d",sum);
return 0;
}

10. Write a C program to generate and print first N FIBONACCI numbers


#include <stdio.h>
void 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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

{
int fib1 =0, fib2 =1, fib3, num, count =0;

printf("Enter the value of num \n");


scanf("%d",&num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count =2;/* fib1 and fib2 are already used */
while(count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
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();

printf("Enter the value of N\n");


scanf ("%d", &N);

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


{
if (i % 2 == 0)
evenSum = evenSum + i;
else
oddSum = oddSum + i;
}

printf ("Sum of all odd numbers = %d\n", oddSum);


printf ("Sum of all even numbers = %d\n", evenSum);
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
/*-----------------------------
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;

printf("Enter the value of x (in degrees) \n");


scanf("%f",&x);
x1 = x;
/* Converting degrees to radians */
x = x *(3.142/180.0);
sinval =sin(x);
printf("Enter the accuracy for the result \n");
scanf("%f",&accuracy);
term = x;
sinx = term;
n =1;
do
{
denominator =2* n *(2* n +1);
term =-term * x * x / denominator;
sinx = sinx + term;
n = n +1;
}while(accuracy <=fabs(sinval - sinx));
printf("Sum of the sine series = %f \n", sinx);
printf("Using Library function sin(%d) = %f\n", x1,sin(x));
}

15.Write a C program to find the value of cos(x) using the series.


#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float acc, term, den, x, cosx=0, cosval;
clrscr();
printf("Enter the value of x (in degrees)\n");
scanf("%f",&x);
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

x1 = x;

/* Converting degrees to radians*/


x = x*(3.142/180.0);
cosval = cos(x);
printf("Enter the accuary for the result\n");
scanf("%f", &acc);
term = 1;
cosx = term;
n = 1;
do
{
den = 2*n*(2*n-1);
term = -term * x * x / den;
cosx = cosx + term;
n = n + 1;
} while(acc <= fabs(cosval - cosx));
printf("Sum of the cosine series = %f\n", cosx);
printf("Using Library function cos(%d) = %f\n", x1,cos(x));
} /*End of main() */
/*------------------------------
Output
Enter the value of x (in degrees)
30
Enter the accuary for the result
0.000001
Sum of the cosine series = 0.865991
Using Library function cos(30) = 0.865991
---------------------------------------------*/

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

printf("There are no primes upto %d\n", num2);


exit(0);
}
printf("Prime numbers are \n");
temp = num1;
if( num1 %2==0)
{
num1++;
}
for(i = num1; i <= num2; i = i +2)
{
flag =0;
for(j =2; j <= i /2; j++)
{

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

printf("Note: First number must be less then to second number\n\n");


printf("Enter first number: ");
scanf("%d", &n1);
printf("Enter second number: ");
scanf("%d", &n2);
printf("Enter divisor: ");
scanf("%d", &div);
printf("Numbers that are divisor by %d :\n",div);
for(n1; n1<=n2; n1++)
{
if(n1%div==0)
{
printf("%d\t",n1);
sum=sum+n1;
}
}
printf("\nSum of numbers that are divisible by %d = %d",div,sum);
getch();
return 0;
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

{
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

Input array elements


+5
-3
+0
-7
+6
Sum of all negative numbers = -10
Sum of all positive numbers = 11
Average of all input numbers = 0.20
--------------------------------------*/

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

if( keynum == array[i] )


{
found = 1;
break;
}
}
if ( found == 1)
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");
} /* End of main */

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

/*--------------------------
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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
-----------------------------------*/

24.Write a C program to evaluate the given polynomial


P(x)=AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0 by reading its coefficients into an array. [Hint:
Rewrite the polynomial as P(x) = a0 + x(a1+x(a2+x(a3+x(a4+x(...x(an-1+xan)))) and
evaluate the function starting from the inner loop]

#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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

printf("\nSum of the polynomial = %6.2f\n",polySum);


}
/*-----------------------------------------------------
Output
RUN 1
Enter the order of the polynomial
2
Enter the value of x
2
Enter 3 coefficients
3
2
6
Given polynomial is:
+ 3x^2 + 2x^1 + 6x^0
Sum of the polynomial = 22.00
-----------------------------------------------------*/

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

printf("MATRIX A is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

printf("Enter the elements of matrix B\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&B[i][j]);
}
}
printf("MATRIX B is\n");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",B[i][j]);
}
printf("\n");
}
printf("Enter your option: 1 for Addition and 2 for Subtraction\n");
scanf("%d",&option);
switch (option)
{
case 1: for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
sumat[i][j] = A[i][j] + B[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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
}
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;

case 2:for(i=0; i<M; i++)


{
for(j=0; j<N; j++)
{
diffmat[i][j] = A[i][j] - B[i][j];
}
}

printf("Difference matrix is\n");


for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",diffmat[i][j]) ;
}
printf("\n");
}

trace (diffmat, M, N);


break;
}
} /* 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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

/*Function to find the trace of a given matrix and print it*/


void trace (int arr[][10], int M, int N)
{
int i, j, trace = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if (i==j)
{
trace = trace + arr[i][j];
}
}
}
printf ("Trace of the resultant matrix is = %d\n", trace);
}

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");

for (c = 0; c < m; c++)


for(d = 0; d < n; d++)
scanf("%d",&matrix[c][d]);

for (c = 0; c < m; c++)


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

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

transpose[d][c] = matrix[c][d];

printf("Transpose of entered matrix :-\n");

for (c = 0; c < n; c++) {


for (d = 0; d < m; d++)
printf("%d\t",transpose[c][d]);
printf("\n");
}
return 0;
}

27. Write a C program to read a matrix A (MxN) and to find the following using functions

a) Sum of the elements of each row


b) Sum of the elements of each column
c) Find the sum of all the elements of the matrix
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum,sumall=0;
/* Function declaration */
int Addrow(int A[10][10], int k, int c);
int Addcol(int A[10][10], int k, int c);
clrscr();
printf("Enter the order of the matrix\n");
scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix\n");
for(i=0; i<row; i++)
{
for(j=0; j< col; j++)
{
scanf("%d", &arr[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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

printf("Input matrix is\n");


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

/* computing row sum */


for(i=0; i<row; i++)
{
rowsum = Addrow(arr,i,col);
printf("Sum of row %d = %d\n", i+1, rowsum);
}

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


{
colsum = Addcol(arr,j,row);
printf("Sum of column %d = %d\n", j+1, colsum);
}
/* computation of all elements */
for(j=0; j< row; j++)
{
sumall = sumall + Addrow(arr,j,col);
}
printf("Sum of all elements of matrix = %d\n", sumall);
}
/* Function to add each row */
int Addrow(int A[10][10], int k, int c)
{
int rsum=0, i;
for(i=0; i< c; i++)
{
rsum = rsum + A[k][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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
return(rsum);
}

/* Function to add each column */


int Addcol(int A[10][10], int k, int r)
{
int csum=0, j;

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


{
csum = csum + A[j][k];
}
return(csum);
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

*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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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 */

for(flag =1, i =0; i < length ; i++)


{
if(reverse_string[i]!= string[i])
flag =0;
}
if(flag ==1)
printf("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

#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);
}

void concat(char s1[], char s2[]) {


int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

{
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
}
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);

printf("Enter %d names\n", N);


for(i=0; i< N ; i++)
{
scanf("%s",name[i]);
strcpy (Tname[i], name[i]);
}
for(i=0; i < N-1 ; i++)
{
for(j=i+1; j< N; j++)
{
if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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


{
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");
} /* End of main() */

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

printf("The Binary number is = %d \n", binary_val);


printf("Its decimal equivalent is = %d \n", decimal_val);
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

}
} /*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
---------------------------------------*/

37 Write a C program to generate Fibonacci sequence 0 1 1 2 3 5 8 13 21


#include<stdio.h>
int main(){
int k,r;
long int f1=l,f2=1,f3;
//Taking maximum numbers from user
printf("Enter the number range:");
scanf("%d",&r);

printf("FIBONACCI SERIES: ");


printf("%ld %ld",f1,f2); //printing firts two values.
for(k=2;k<r;k++){
f3=f1+f2;
f1=f2;
f2=f3;

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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);

printf("Enter %d elements\n", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
return 0;
}

39. Write a C program to accept an integer and reverse it


include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

printf("Reverse of entered number is = %d\n", reverse);


return 0;
}

40. Write a C program to check whether a given number is Armstrong or not.


#include<stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num=num+(rem*rem*rem);
n1=n1/10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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!
------------------------------*/

42.Write a c program to compute the surface area and volume of a cube.


Formula of surface area of cube:
Surface_area = 6 * a2

Formula of volume of cube:


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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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(){

long int decimalNumber,remainder,quotient;


int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

44. Write a c program to find whether a given year is leap year or not
#include<stdio.h>
int main(){
int year;

printf("Enter any year: ");


scanf("%d",&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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

Before swapping i= 23 and k = 34


After swapping i= 34 and k = 23
------------------------------------------*/

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;

printf("\nEnter two strings :");


gets(str1);

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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;

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++;
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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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 */
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}

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)

Thudupathi, Erode – 638 057.

TRAINING AND PLACEMENT CELL

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

You might also like