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

CP All Programs

The document contains C program code snippets for various problems involving arrays, structures, recursion, matrices, strings and patterns. Some of the problems addressed include reading and displaying book records using a structure array, adding two matrices, checking if a word is a palindrome, finding the transpose and sum of a matrix, calculating factorials and Fibonacci series recursively, multiplying matrices, and finding the power and factorial of a number using recursion.

Uploaded by

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

CP All Programs

The document contains C program code snippets for various problems involving arrays, structures, recursion, matrices, strings and patterns. Some of the problems addressed include reading and displaying book records using a structure array, adding two matrices, checking if a word is a palindrome, finding the transpose and sum of a matrix, calculating factorials and Fibonacci series recursively, multiplying matrices, and finding the power and factorial of a number using recursion.

Uploaded by

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

1) Write a program to read Title, Author and Price of 5 books using array of structures.

Display the
records in ascending order of Price.

#include<stdio.h>
struct book
{
char title[30],author[30];
float price;
};
int main()
{
struct book b[5];
int i,j;
struct book temp;
for(i=0;i<5;i++)
{
printf("\nEnter book title, author and price=");
scanf("%s%s%f",&b[i].title,&b[i].author,&b[i].price);
}
for(i=1;i<5;i++)
{
for(j=0;j<5-i;j++)
{
if(b[j].price>b[j+1].price)
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
printf("\nSorted List=\n");
for(i=0;i<5;i++)
{
printf("\n");
printf("\nTitle=%s",b[i].title);
printf("\nAuthor=%s",b[i].author);
printf("\nPrice=%f",b[i].price);
}
return 0;
}

2) Implement a program to perform addition of two matrices.


#include <stdio.h>
int main()
{
int row,col, i, j, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d %d", &row, &col);
printf("Enter the elements of first matrix\n");
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) scanf("%d", &first[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) scanf("%d", &second[i][j]);
printf("Sum of entered matrices:-\n");
for (i = 0; i <row; i++)
{
for (j = 0; j < col; j++)
{
sum[i][j] = first[i][j] + second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}

3) Write a program to check whether a word is palindrome or not..


#include<stdio.h>
#include<string.h>
int main(){
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0;i < len ;i++){
if(str[i] != str[len-i-1]){
temp = 1;
break;
}
}
if (temp==0) {
printf("String is a palindrome");
}
else {
printf("String is not a palindrome");
}
return 0;
}

6) Implement a program to find transpose of a matrix.


#include<stdio.h>
int main()
{
int a[10][10],b[10][10];
int m,n,i,j;
printf("Enter number of rows and columns of matrix=");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter Number=");
scanf("%d",&a[i][j]);
}
}
printf("\nEntered matrix: \n");
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
printf("%d ", a[i][j]);
if (j == n - 1)
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[j][i]=a[i][j];
}
}
printf("\nTranspose Matrix=\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
return 0;
}

7) Write a C program to find LCM of two numbers using recursion.


#include<stdio.h>
#include<conio.h>
int lcm(int a , int b)
{
static int m = 0;
m = m + b;
if(m % a == 0 && m % b == 0)
{
return m;
}
return lcm(a , b);
}
int main()
{
int x , y;
printf("Enter First Number : ");
scanf("%d" , &x);
printf("Enter Seconf Number : ");
scanf("%d" , &y);
printf("LCM of %d and %d = %d" , x , y , lcm(x , y));
return 0;
}

11) Write a program to print Fibonacci series.

#include <stdio.h>

int main()
{
int i, n, firstTerm=0, secondTerm=1, sum=0;
printf("\nEnter number of terms required in Fibonacci Series: ");
scanf("%d",&n);
printf("\nFibonacci Series is:\n\n\n %d %d ", firstTerm,
secondTerm);
i=2;

while (i<n)
{
sum=firstTerm+secondTerm;
firstTerm=secondTerm;
secondTerm=sum;
++i;
printf("%d ",sum);
}
return 0;
}

12) Write a program using recursion to find factorial of a number.


#include<stdio.h>
int factorial(int n)
{
if(n==0)
return 1;
else
return (n*factorial(n-1));

void main()
{
int number,fact;
printf("Enter a number: ");
scanf("%d",&number);
fact=factorial(number);
printf("Factorial of %d is %d\n",number,fact);
}

14) Write a C program to perform multiplication of two matrices


#include<stdio.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

16th 2nd part) Write a program to find the power of x raised to n that is: xn, using recursive function.
#include<stdio.h>
int power(int b,int e)
{
if(e==0)
return 1;
else
return (b*power(b,e-1));

void main()
{
int base,exponent,pow;
printf("Enter a base:\n ");
scanf("%d",&base);
printf("Enter the exponent: \n");
scanf("%d",&exponent);
pow=power(base,exponent);
printf("Power %d ^ %d = %d\n ",base,exponent,pow);
}

18) Write a program to print the following pattern.


A
BB
CCC
DDDD

#include<stdio.h>
void main()
{
int i, j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%c ",'A'-1 + i);
}
printf("\n");
}
}

19) Write a program to find largest element of an 1D array.

#include<stdio.h>
void main()
{
int n,i,a[50],max;
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements of array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];

}
}
printf("Largest elemnt of an array is %d\n",max);
}

20) Write a Program to calculate and display sum of all the elements of the matrix.
#include<stdio.h>
int main()
{
int a[10][10];
int m,n,i,j,s1=0;
printf("Enter rows and columns of matrix=");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter Number=");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
s1=s1+a[i][j];
}
}
printf("\nSum of all array elements=%d",s1);
return 0;
}

21) Define a structure called player with data members as player name, team name, batting
average. Store and display the information of at least 10 players.

#include<stdio.h>
struct player
{
char pname[30],tname[30];
float average;
};
int main()
{
struct player p[10];
int i,j;
for(i=0;i<10;i++)
{
printf("\nEnter player name, team name and batting average=");
scanf("%s%s%f",&p[i].pname,&p[i].tname,&p[i].average);
}
printf("\nPlayer List=\n");
for(i=0;i<10;i++)
{
printf("\n");
printf("\nPlayer Name=%s",p[i].pname);
printf("\nTeam Name=%s",p[i].tname);
printf("\nBatting Average=%f",p[i].average);
}
return 0;
}

22) Write a program to accept three numbers from the user and display the greatest of three using
the conditional operator.
# include <stdio.h>
void main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
}

23) Write a program to display the following for the user specified number of lines.
*
**
***
****
*****
******

#include<stdio.h>
int main()
{
int n,i,j,s;
printf("Enter number of lines=");
scanf("%d",&n);
s=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=s;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
s--;
}
return 0;
}

24) Write a program to check if the entered number is prime number or not.
#include<stdio.h>
int main()
{
int n,i,count=0;
printf("Enter number=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
printf("Prime Number");
}
else
{
printf(" Not a Prime number");
}
return 0;
}

25Write a program in C to find out the power of x raised to n (xn), using non-recursive function.

#include <stdio.h>
int main()
{
int base, exponent;
int power = 1;
int i;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++)
{
power = power * base;
}
printf("%d ^ %d = %d", base, exponent, power);
return 0;
}

26) Write a program in C to find the smallest of N elements using an array.

#include <stdio.h>
int main()
{
int a[30],i,num,smallest;
printf("\n Enter no of elements: ");
scanf("%d",&num);
printf("Enter the array elements: ");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
smallest=a[0];

for(i=0;i<num;i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
printf("The smallest element is: %d",smallest);
}

27) Write a program in C to find the reverse of a given string without using inbuilt string function.

#include<stdio.h>
#include<string.h>
void main()
{
int i,n;
char str[20];
printf("Enter the String to get reversed: ");
gets(str);
n=strlen(str);
printf("\nReversed string is ");
for(i=n-1;i>=0;i--)
{
printf("%c",str[i]);
}
}

28) Write a program to accept a set of 10 numbers and print the numbers using arrays.

#include<stdio.h>
int main()
{
int a[10],n,i,s=0;
float average;
for(i=0;i<10;i++)
{
printf("\nEnter element=");
scanf("%d",&a[i]);
}
printf("\nArray Elements are\n");
for(i=0;i<10;i++)
{
printf("\t%d",a[i]);
}
for(i=0;i<10;i++)
{
s=s+a[i];
}
average=s/10.0;
printf("\n Average is =%f",average);
return 0;
}

29) Write a program to store and display at least 10 records of the name, roll number and fees of a
student using structure.

#include<stdio.h>
struct student
{
char name[20];
int roll;
int fees;
};
int main()
{
struct student s[10];
int i,j,n=10;
for(i=0;i<n;i++)
{
printf("\nEnter Name Roll No. Fees of student-%d = ",i+1);
scanf("%s %d %d",s[i].name,s[i].roll,s[i].fees);
}
for(i=0;i<n;i++)
{
printf("\n%s %d %d",s[i].name,s[i].roll,s[i].fees);
}
return 0;
}

32 2nd part) Write a program to find summation of n numbers using recursion.

#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}

33) Write a program to print the following pattern. (Note- Not only 4 lines, it should print N lines
taken from the user.)
A
BB
CCC
DDDD

#include<stdio.h>
void main()
{
int i, j,k,n;
printf("Enter any Number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=n;k>=i;k--)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%c ",'A'-1 + i);
}
printf("\n");
}
}

34) Write a C-program to create array of structures in order to store details of almost 100 books.
The book details are book name, book price, book page number and book author name.

#include<stdio.h>
struct books
{
int pno;
char bn[30],an[30];
float p;
};
int main()
{
struct book b[100];
int i,j;
for(i=0;i<=99;i++)
{
printf("\nEnter Book record=");
scanf("%s%f%d%s",&b[i].bn, &b[i].p, &b[i].pno, &b[i].an);
}
for(i=0;i<=99;i++)
{
printf("Book Name %s Price %f Page no %d Author %s",b[i].bn, b[i].p,
b[i].pno, b[i].an);
}
return 0;
}

35) Write a program that will accept two-dimensional square matrix and find the sum of diagonal
elements. (Note- sum of diagonal elements should be calculated for both sides).
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10],principal=0,secondary=0 ;
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
// Condition for principal diagonal
if (rows == columns)
principal += a[rows][columns];
// Condition for secondary diagonal
if ((rows+columns) == (i - 1))
secondary += a[rows][columns];
}
}
printf("\n The Primary and secondary principal diagonal sum = %d %d",
principal,secondary );
printf("\n The Sum of All Diagonal Elements of a Matrix = %d",
principal+secondary );
return 0;
}

38) Write a C program to find GCD of two numbers using recursion.

#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}

int hcf(int n1, int n2)


{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

39) Write a C program to implement month name by accepting month number from user. ( Use
switch case)

#include<stdio.h>
void main()
{
int choice;
printf("\n Enter the month number : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Month is : January");
break;
case 2: printf("Month is : February");
break;
case 3: printf("Month is : March");
break;
case 4: printf("Month is : April");
break;
case 5: printf("Month is : May");
break;
case 6: printf("Month is : June");
break;
case 7: printf("Month is : July");
break;
case 8: printf("Month is : August");
break;
case 9: printf("Month is : September");
break;
case 10: printf("Month is : October");
break;
case 11: printf("Month is : November");
break;
case 12: printf("Month is : December");
break;
default : printf("invalid number");
}
}

40) Write a C program to accept 10 integers from the user and arrange them in ascending order and
display them.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,a[10];
printf("Enter 10 integer numbers: \n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("\n\nThe 10 numbers sorted in ascending order are: \n");
for(i=0;i<10;i++)
printf("%d\t",a[i]);

You might also like