Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
117 views20 pages

List of Programs

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 20

LIST OF PROGRAMS

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


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()

{
float a,b,c,s,area;
clrscr();
printf("Enter Three side of triange -> ");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Side1 =%7.1f\nSide2 =%7.1f\nSide3 =%7.1f\n",a,b,c);
printf("\nArea of Triange :-> %8.4f \n",area);
getch();
}
2. Write a 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);
}

3. Write a program to find the simple interest , given principle, rate of interest and times?
#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);
getch();
}

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


#include <stdio.h>
#include<conio.h>
void main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0)
printf("%d is even.",num);
else
printf("%d is odd.",num);
getch();
}

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


#include <stdio.h>
int main()
{
float num;
printf("Enter a number: ");
scanf("%f",&num);
if (num<=0)
{
if (num==0)
printf("You entered zero.");
else
printf("%.2f is negative.",num);
}
else
printf("%.2f is positive.",num);
getch();
}

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


#include <stdio.h>
main(){
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %d",a);
else
printf("Largest number = %d",c);
}
else
{
if(b>=c)
printf("Largest number = %d",b);
else
printf("Largest number = %d",c);
}
getch();
}
7. Write a program to find and output all the roots of a quadratic equation, for non-zero coefficients.
#include <stdio.h>
#include <math.h>
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);
}
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);
}
getch();
}

8. Write a 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 a,b,c,d;
clrscr();
printf("Enter the values of a and b:");
scanf("%d%d" ,&a,&b);
printf(" Enter your choice between 1 to 5 /n");
scanf("%d , &d);

switch(d)
{
case 1;
c=a+b;
printf(" Addition=%d", c);
break:
case 2;
c=a-b;
printf(" Substraction=%d" , c);
break:
case 3;
c=a*b;
printf(" Multiplication=%d", c);
break:
case 4;
c=a/b;
printf(" Divide=%d", c);
break:
case 5;
c=a%b;
printf(" Remainder=%d",c );
break:
default:
printf(" Invalid Result");
break:
}

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


#include<stdio.h>
#include<conio.h>

main()
{
int i,no,sum=0;

clrscr();
printf("Enter the Number : ");
scanf("%d",&no);

for(i=1;i<=no;i++)
{
sum=sum+i;
}
printf("The Sum is : %d",sum);

getch();
}
10. Write a program to generate and print first N FIBONACCI numbers
#include<stdio.h>
#include<conio.h>

main()
{
int n,f1=0,f2=1,f3=0,i;

clrscr();
printf("Enter the Final Number : ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf("\n%d\t%d",i,f3);
}
getch();
}
11. Write a program to find the GCD and LCM of two integers output the results along with the given
integers. Use Euclids' algorithm.
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;
}
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);
getch();
}

12. Write a 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
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);
getch();
}

13. Write a program to reverse a given integer number and check whether it is a palindrome.
#include <stdio.h>
main()
{
int num, temp, remainder, reverse = 0;
printf("Enter an integer \n");
scanf("%d", &num);
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");
getch();
}

14. Write a program to find the value of cos(x) using the series up to the given accuracy
(without using user defined function) Also print cos(x) using library function.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{
int n, x1;
float accuracy, term, denominator, x, cosx, cosval;
clrscr();
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
/* Converting degrees to radians */

x = x * (3.142 / 180.0);
cosval = cos(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = 1;
cosx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n - 1);
term = -term * x * x / denominator;
cosx = cosx + term;
n = n + 1;
} while (accuracy <= fabs(cosval - cosx));
printf("Sum of the cosine series = %f\n", cosx);
printf("Using Library function cos(%d) = %f\n", x1, cos(x));
getch();
}

15. Write a program to check whether a given number is prime or not and output the given number
with suitable message
#include <stdio.h>
#include <conio.h>
main()
{
int n, i, flag=0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
getch();
}
16. Write a program to generate and print prime numbers in a given range.
Also print the number of prime numbers.
#include <stdio.h>
#include <stdlib.h>
main()
{
int num1, num2, i, j, flag, temp, count = 0;
clrscr();
printf("Enter the value of num1 and num2 \n");
scanf("%d %d", &num1, &num2);
if (num2 < 2)
{
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);
getch();
}

17. Write a 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 output the
computed results.
#include<stdio.h>
#include<conio.h>
main()
{
int i, N1, N2, count = 0, sum = 0;
clrscr();
printf ("Enter the value of N1 and N2\n");
scanf ("%d %d", &N1, &N2);
/*Count the number and compute their sum*/
printf ("Integers divisible by 5 are\n");
for (i = N1; i < N2; i++)
{
if (i%5 == 0)
{
printf("%3d,", i);
count++;
sum = sum + i;
}
}
printf ("\nNumber of integers divisible by 5 between %d and %d = %d\n", N1,N2,count);
printf ("Sum of all integers that are divisible by 5 = %d\n", sum);
getch();
}
18. Write a program to read N integers (zero, +ve and -ve) into an array A and to
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>
main()
{
int sumOdd = 0;
int sumEven = 0;
int upperbound;
int absDiff;
clrscr();

printf("Enter the upperbound: ");


scanf("%d", &upperbound);

int number = 1;
while (number <= upperbound) {
if (number % 2 == 0) {
sumEven += number;
} else {
sumOdd += number;
}
++number;
}
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
printf("The sum of odd numbers is %d.\n", sumOdd);
printf("The sum of even numbers is %d.\n", sumEven);
printf("The absolute difference is %d.\n", absDiff);
getch();
}
19. Write a program to input N numbers (integers or reals) 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>

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++)
{
if( keynum == array[i] )
{
found = 1;
break;
}
}
if ( found == 1)
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");
getch();
}

20. Write a 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>
#include <conio.h>
#include <string.h>
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");
for(i=0; i< N ; i++)
{
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("------------------------------------------\n");
getch();
}
21. Write a program to accept N numbers sorted in ascending order and to search for a given number using binary search.
Report success or failure in the form of suitable messages.
#include <stdio.h>
#include <conio.h>

main()
{
int array[10];
int i, j, N, temp, keynum;
int low,mid,high;
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 elements\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
/* Bubble sorting begins */
for(i=0; i< N ; i++)
{
for(j=0; j< (N-i-1) ; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted 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);
low=1;
high=N;
do
{
mid= (low + high) / 2;
if ( keynum < array[mid] )
high = mid - 1;
else if ( keynum > array[mid])
low = mid + 1;
} while( keynum!=array[mid] && low <= high); /* End of do- while */

if( keynum == array[mid] )


{
printf("SUCCESSFUL SEARCH\n");
}
else
{
printf("Search is FAILED\n");
}
getch();
}
22. Write a program to input real numbers and find the mean, variance and standard deviation.
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10

main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, 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];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
getch();
}

23. Write a 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];
l2 = a[1];
if (l1 < l2)
{
temp = l1;
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);
getch();
}
26. Write a 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
main()
{
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--);
}
printf("\nSum of the polynomial = %6.2f\n",polySum);
getch();
}
27. Write a program to read two matrices A (MxN) and B(MxN) and perform addition OR subtraction of A and B. Find the trace of
the resultant matrix. Output the given matrix, their sum or Differences and the trace.
#include <stdio.h>
#include <conio.h>

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

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

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

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);
getch();
}
28. Write a 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>

main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
clrscr();
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++ )
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");
}

getch();
}
29. Write a 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 <conio.h>
#include <string.h>
main()
{
char string[25], revString[25]={'\0'};
int i,length = 0, flag = 0;
clrscr();
fflush(stdin);
printf("Enter a string\n");
gets(string);
for (i=0; string[i] != '\0'; i++) /*keep going through each */
{ /*character of the string */
length++; /*till its end */
}
for (i=length-1; i >= 0 ; i--)
{
revString[length-i-1] = string[i];
}
for (i=0; i < length ; i++)
{
if (revString[i] == string[i])
flag = 1;
else
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome\n", string);
else
printf("%s is not a palindrome\n", string);
getch();
}
30. Write a program to read two strings and concatenate them (without using library functions).
Output the concatenated string along with the given string.
#include <stdio.h>
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);
getch();
}
31. Write a program to read an English sentence and replace lowercase characters 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>

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;
printf("The given sentence is : %s",sentence);
printf("\nCase changed sentence is: ");

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


{
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
getch();
}
32. Write a program read a sentence and count the number of number of vowels and consonants in
the given sentence. Output the results on two lines with suitable headings.
#include <stdio.h>
main()
{
char sentence[80];
int i, vowels = 0, consonants = 0, special = 0;
clrscr();
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;
}
}
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
getch();
}
33. Write a 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 <string.h>
main()
{
char name[10][8], tname[10][8], temp[8];
int i, j, n;
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 (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input NamestSorted names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", tname[i], name[i]);
}
printf("------------------------------------------\n");
getch();
}

34. Write a program to sort given N elements using SELECTION sort method using functions
* a) To find maximum of elements
* b) To swap two elements
#include <stdio.h>
#include <conio.h>
main()
{
int array[10];
int i, j, N, temp;
int findmax(int b[10], int k); /* function declaration */
void exchang(int b[10], int k);
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 elements\n");
for(i=0; i<N ; i++)
{
printf("%d\n",array[i]);
}
exchang(array,N);
printf("Sorted array is...\n");
for(i=0; i< N ; i++)
{
printf("%d\n",array[i]);
}
getch();
}
/* function to find the maximum value */
int findmax(int b[10], int k)
{
int max=0,j;
for(j = 1; j <= k; j++)
{
if ( b[j] > b[max])
{
max = j;
}
}
return(max);
}
void exchang(int b[10],int k)
{
int temp, big, j;
for ( j=k-1; j>=1; j--)
{
big = findmax(b,j);
temp = b[big];
b[big] = b[j];
b[j] = temp;
}
return;
}
35. Write a 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
* Output the computed results with suitable headings.
#include <stdio.h>
#include <conio.h>

main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum,sumall=0;
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]);
}
}
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);
getch();
}

/* 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];
}
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);
}
36. Write a 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
37. Write a 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>

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

38. Write a program to convert the given binary number into decimal 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
39. Write a program to generate Fibonacci sequence is 0 1 1 2 3 5 8 13 21 ...
40. Write a program to insert a particular element in a specified position in a given array
41. Write a program to accept an integer and reverse it .This program is to illustrate how user authentication
* is made before allowing the user to access the secured resources. It asks for the user name and then the
* password. The password that you enter will not be displayed, instead that character is replaced by '*'
42. Write a program to accept a string and a substring and check if the substring is present in the given string
43. Write a program to compute the surface area and volume of a cube
44. Write a program to accept a decimal number and convert it binary and count the number of 1's in the binary number
45. Write a program to find whether a given year is leap year or not
46. Write a program to swap the contents of two numbers using bitwise XOR operation. Don't use either the
temporary variable or arithmetic operators
47. Write a 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)
48. Program to accept 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
49. Program to accept two strings and concatenate them. i.e. The second string is appended to the end of the first string
50. Write a program to find the length of a string without using the built-in function
51. Write a program to find the length of a string without using the built-in function also check whether it is a palindrome or not.
52. Write a program to accept a string and find the number of times the word 'the' appears in it
53. Write a program to compute the value of X ^ N given X and N as inputs
54. Write a program to accept two matrices and find the sum and difference of the matrices
55. Write a Program to accept two matrices and check if they are equal
56. Write a Program to check if a given matrix is an identity matrix
57. Write a program to accept a matrix of given order and interchange any two rows and columns in the original matrix
58. Write a program to display the inventory of items in a store/shop. The inventory maintains details such as name, price,
quantity and manufacturing date of each item.
59. Write a program to multiply given number by 4 using bitwise operators
60. Write a program to cyclically permute the elements of an array A .i.e. the content of A1 become that of A2.And A2 contains
that of A3 & so on as An contains A1
61. Write a program to accept a matrix of order M x N and store its elements and interchange the main diagonal elements of the
matrix with that of the secondary diagonal elements
62. Write a program to accept a figure code and find the areas of different geometrical figures such as circle, square, rectangle
etc using switch
63. Write a program to accept a string and find the sum of all digits present in the string
64. Write a program to accept a matric and determine whether it is a sparse matrix. A sparse matrix is matrix which has more
zero elements than nonzero elements
65. Write a Program to convert the lower case letters to upper case and vice-versa.

You might also like