CP All Programs
CP All Programs
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;
}
#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;
}
void main()
{
int number,fact;
printf("Enter a number: ");
scanf("%d",&number);
fact=factorial(number);
printf("Factorial of %d is %d\n",number,fact);
}
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);
}
#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");
}
}
#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;
}
#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;
}
#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;
}
#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;
}
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]);