0% found this document useful (0 votes)
59 views41 pages

CP I Modified

The document contains C code snippets for various mathematical and logical operations like calculating sum of two numbers, area of a circle, finding the largest of three numbers, checking if a year is a leap year, converting Celsius to Fahrenheit, determining if a number is even or odd, finding the square root of a number, checking if a number is Armstrong, checking if a number is a palindrome, finding the factorial of a number, finding the roots of a quadratic equation, reversing digits of a number, finding the sum of digits, generating the Fibonacci series, implementing Pascal's triangle, finding the largest and smallest elements of an array, sorting an array in ascending and descending order, adding and multiplying matrices. Each code is accompanied by

Uploaded by

harinee
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)
59 views41 pages

CP I Modified

The document contains C code snippets for various mathematical and logical operations like calculating sum of two numbers, area of a circle, finding the largest of three numbers, checking if a year is a leap year, converting Celsius to Fahrenheit, determining if a number is even or odd, finding the square root of a number, checking if a number is Armstrong, checking if a number is a palindrome, finding the factorial of a number, finding the roots of a quadratic equation, reversing digits of a number, finding the sum of digits, generating the Fibonacci series, implementing Pascal's triangle, finding the largest and smallest elements of an array, sorting an array in ascending and descending order, adding and multiplying matrices. Each code is accompanied by

Uploaded by

harinee
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/ 41

TO CALCULATE SUM OF TWO NUMBERS

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

int a,b,sum;
printf(Enter any two numbers\n);
scanf(%d%d,&a,&b);
sum=a+b;
printf(The sum of a and b is :%d,sum);
getch();

OUTPUT:
Enter any two numbers
2
3
The sum of a and b is : 5

TO CALCULATE THE AREA OF A CIRCLE


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

int r;
float circle;
printf(Enter the radius of the circle\n);
scanf(%d,&r);
circle=3.14*r*r;
printf(The area of the circle is:%f\n,circle);
getch();

OUTPUT:
Enter the radius of the circle
10
The area of the circle is:314.000000

BIGGEST OF THREE NUMBERS


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter any three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
{
printf("A is big\n");
}
else if(b>c)
{
printf("B is big\n");
}
else
{
printf("C is big\n" );
}
getch();
}

OUTPUT:
Enter any three numbers: 10
40
30
B is big

TO FIND THE LEAP YEAR OR NOT


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

int year;
printf(Enter the year);
scanf(%d,&year);
if(year%4==0)

{
printf(Given the year is leap year\n);
}
else
{
printf(Given the year is not a leap year\n);
}
getch();
}

OUTPUT:
Enter the year
2012
Given the year is leap year
Enter the year
2010
Given the year is not a leap year

TO CONVERT CELICIUS TO FARENHEIT


#include<stdio.h>
#include<conio.h>
void main()
{
float celsi,fahren;
clrscr();
printf("enter the celius value:");
scanf("%f",&celsi);
fahren=(1.8*celsi)+32;
printf("the fahrenheit value of the given %f celsius
value is %f",celsi,fahren);
getch();
}

OUTPUT:
Enter the celius value:28
The fahrenheit value of the given 28.000000 Celsius value is
82.400002

TO FIND WHETHER GIVEN NUMBER IS EVEN OR ODD


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

int n;
printf("Enter any numbers\n);
scanf("%d",&n);
if(n%2==0)
{
printf("Given number is Even\n);
}
else
{
printf("Given number is Odd\n);
}
getch();

OUTPUT:
Enter any numbers
5
Given number is Odd
Enter any numbers
6
Given number is Even

TO FIND THE SQUARE ROOT OF A NUMBER

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

int n,square;
printf("Enter any numbers\n);
scanf("%d",&n);
square=n*n;
printf("The square of the given number is :%d\n,square);
getch();

OUTPUT:
Enter any numbers
4
The square of the given number is: 16

TO CHECK WHETHER THE GIVEN NUMBER IS AMSTRONG OR NOT


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,m,sum,arms;
clrscr();
printf("Enter the numbers:");
scanf("%d",&n);
arms=n;
sum=0;
while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(arms==sum)
{
printf("Given number is an Amstrong\n);
}
else
{
printf("Given number is not an Amstrong\n);
}
getch();
}

OUTPUT:
Enter the numbers
153
Given number is an Amstrong

TO FIND NUMBER IS PALINDROME OR NOT


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,Rev,pal;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
pal=n;
Rev=0;
while(n>0)
{
r=n%10;
Rev=Rev*10+r;
n=n/10;
}
if(pal==Rev)
{
printf("Given number is palidrome");
}
else
{
printf("Given number is not a palidrome");
}
getch();
}
OUTPUT:
Enter the number:345
Given number is not a palidrome
Enter the number:151
Given number is palidrome

TO FIND FACTORIAL OF A GIVEN NUMBER


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,fact,i;
clrscr();
printf("Enter the numbers:");
scanf("%d",&n);
fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial value is:%d",fact);
getch();
}

OUTPUT:
Enter the numbers:6
The factorial value is:720

TO FIND ROOTS OF QUADRATIC EQUATION


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
clrscr();
printf("Enter the value A:");
scanf("%d",&a);
printf("Enter the value of B:");
scanf("%d",&b);
printf("Enter the value of C:");
scanf("%d",&c);
d=(b*b)-(4*a*c);
if(d>=0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(+b+sqrt(d))/(2*a);
printf("The Root 1 value is:%f\n",r1);
printf("The Root 2 value is:%f",r2);
}
else
{
printf("The Roots are imaginary);
}
getch();
}

OUTPUT:
Enter the value A:5
Enter the value of B:20
Enter the value of C:6
The Root 1 value is:-0.326680
The Root 2 value is:3.673320
Enter the value A:20
Enter the value of B:6
Enter the value of C:5
The Roots are imaginary
Enter the value A:4
Enter the value of B:4
Enter the value of C:1
The Root 1 value is:-0.500000
The Root 2 value is:0.500000

TO REVERSE THE DIGITS OF A GIVEN NUMBER


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,Rev,pal;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
pal=n;
Rev=0;
while(n>0)
{
r=n%10;
Rev=Rev*10+r;
n=n/10;
}
printf(Reverse of given number is %d,Rev);
getch();

OUTPUT:
Enter the number:345
Reverse of given number is 543

TO FIND THE SUM OF DIGITS OF A GIVEN NUMBERS


#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
sum=0;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf(The sum of digits of a given number is :
%d\n,sum);
getch();
}

OUTPUT:
Enter the number
12345
The sum of digits of a given number is :15

TO GENERATE FIBONNACI SERIES


#include<stdio.h>
#include<conio.h>
void main()
{
int n,fib,x,y,i;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
x=0;
y=1;
printf("Generarte the fibonacci series\n");
printf("%d\n%d\n",x,y);
for(i=2;i<n;i++)
{
fib=x+y;
x=y;
y=fib;
printf("%d\n",fib);
}
getch();
}
OUTPUT:
Enter the limit:5
Generarte the fibonacci series
0
1
1
2
3

IMPLEMENTATION OF PASCAL TRIANGLE


#include<stdio.h>
#include<conio.h>
void main()
{
int noline,i,j,temp;
clrscr();
printf("Enter the number of lines to print:");
scanf("%d",&noline);
printf("\n");
for(i=1;i<=noline;i++)
{
for(j=1;j<=noline-i;j++)
printf("

");

temp=i;
for(j=1;j<=i;j++)
printf("%4d",temp++);
temp=temp-2;
for(j=1;j<i;j++)
printf("%4d",temp--);
printf("\n\n");
}
getch();
}

OUTPUT:
Enter the number of lines to print:5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

LARGEST AND SMALLEST ElEMENT OF A GIVEN ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,large,small,no;
clrscr();
printf("Lagest & Smallest of an array\n\n");
printf("-------------------------------\n\n");
printf("In how many numbers do u want to find:");
scanf("%d",&no);
printf("Enter the elements of the array:");
for(i=0;i<no;i++)
scanf("%d",&a[i]);
printf("The Elements Of The Array:\n");
for(i=0;i<no;i++)
printf("%d\t",a[i]);
small=a[0];
large=a[0];
for(i=0;i<no;i++)
{
if(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
}
printf("The largest of the given array is %d\n",large);
printf("The smallest of the given array is %d\n",small);
getch();
}

OUTPUT:
Lagest & Smallest of an array
------------------------------In how many numbers do u want to find:5
Enter the elements of the array:12
34
56
87
43
The Elements Of The Array:
12 34 56 87 43
The largest of the given array is 87
The smallest of the given array is 12

TO FIND ASCENDING & DESCENDING ORDER OF ELEMENTS


USING ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
int num[100],no,i,j,a;
clrscr();
printf("Enter the no. of elements for the array:");
scanf("%d",&no);
printf("Enter the numbers:");
for(i=0;i<no;i++)
scanf("%d",&num[i]);
for(i=0;i<no-1;i++)
{
for(j=i+1;j<no;j++)
{
if(num[i]<num[j])
{
a=num[i];
num[i]=num[j];
num[j]=a;
}
}
}
printf("\nThe ascending order of the given numbers-");
for(i=0;i<no;i++)
printf("\n%d",num[i]);
printf("\nThe descending number of the given numbers-");
for(j=no-1;j>=0;j--)
printf("\n%d",num[j]);
getch();
}

OUTPUT:
Enter the no. of elements for the array:5
Enter the numbers:10
30
50
60
20
The ascending order of the given numbers60
50
30
20
10
The descending number of the given numbers10
20
30
50
60

MATRIX ADDITION
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,row,col;
clrscr();
printf("Enter the row Size:");
scanf("%d",&row);
printf("Enter the column Size:");
scanf("%d",&col);
printf("Enter the A matrix:");
for (i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the B matrix:");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}

printf("A matrix :\n");


for(i=1;i<=row;i++)
{
printf("\t");
for(j=1;j<=col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("B matrix :\n");
for(i=1;i<=row;i++)
{
printf("\t");
for(j=1;j<=col;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The addition of two matrix is:\n");
for(i=1;i<=row;i++)
{
printf("\t");
for(j=1;j<=col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT:
Enter the row Size:3
Enter the column Size:3
Enter the A matrix:1
2
3
4
5
6
7
8
9
Enter the B matrix:1
2
3
4
5
6
7
8
9
A matrix :
1

B matrix :

The addition of two matrix is:


2

10

12

14

16

18

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

int a[10][10],b[10][10],c[10][10],i,j,k,row,col;
clrscr();
printf("Enter the row Size:");
scanf("%d",&row);
printf("Enter the column Size:");
scanf("%d",&col);
printf("Enter the A matrix:");
for (i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the B matrix:");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
c[i][j]=0;
for(k=1;k<=row;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];

}
}
}
printf("\nA matrix-");
for(i=1;i<=row;i++)
{
printf("\t");
for(j=1;j<=col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nB matrix-");
for(i=1;i<=row;i++)
{
printf("\t");
for(j=1;j<=col;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The multiplication of two matrix is:\n");
for(i=1;i<=row;i++)
{
printf("\t");
for(j=1;j<=col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();}

OUTPUT:
Enter the row Size:3
Enter the column Size:3
Enter the A matrix:1
2
3
4
5
6
7
8
9
Enter the B matrix:1
2
3
4
5
6
7
8
9

A matrix1

B matrix-

The multiplication of two matrix is:


30

36

42

66

81

96

102

126

150

Transpose of a Matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[25][25],row,col;
clrscr();
printf("\nEnter the number of rows and columns for the
matrix:");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrix:");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
printf("The given matrix-\n");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("\t%d",a[i][j]);
}
printf("\n\nThe transpose of the given matrix-\n");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("\t%d",a[j][i]);
}
getch();
}

OUTPUT:
Enter the number of rows and columns for the matrix:3
3
Enter the elements of the matrix:1
2
3
4
5
6
7
8
9
The given matrix1

The transpose of the given matrix1

MENU DRIVEN PROGAM USING SWITCH CASE


#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int a,b,c,n;
clrscr();
printf("--MENU--\n");
printf("1.ADDITION\n");
printf("3.SUBTRACTION\n");
printf("4.DIVISION\n");
printf("5.EXIT\n");
printf("Enter your choice:");
scanf("%d",&n);
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
switch(n)
{
case 1:
c=a+b;
printf("Addition:%d\n",c);
break;
case 2:
c=a-b;
printf("Subtraction:%d\n",c);
break;
case 3:
c=a*b;
printf("Multiplication:%d\n",c);
break;
case 4:
c=a/b;

printf("Division:%d\n",c);
break;
case 5:
exit(0);
break;
default:
printf("Invalid operation code");
}
getch();
}
OUTPUT:
--MENU-1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.DIVISION
5.EXIT
Enter your choice:1
Enter two numbers:3
4
Addition:7
--MENU-1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.DIVISION
5.EXIT
Enter your choice:
2
Enter two numbers:7
3
Subtraction:4

--MENU-1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.DIVISION
5.EXIT
Enter your choice:3
Enter two numbers:4
5
Multiplication:20
--MENU-1.ADDITION
2.SUBTRACTION
3.MULTIPLICATION
4.DIVISION
5.EXIT
Enter your choice:
4
Enter two numbers:20
4
Division:5

TO FIND GIVEN NUMBER IS PRIME OR NOT


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
printf("The Given number is not a prime\n");
getch();
exit(0);
}
}
printf("The Given number is a prime\n");
getch();
}
OUTPUT:
Enter the number
5
The Given number is a prime
Enter the number
4
Given number is not a prime

TO DISPLAY STUDENT DETAILS USING STRUCTURE


#include<stdio.h>
#include<conio.h>
typedef struct stud
{
int rno,mark;
char name[10];
}student;
student s[10];
void main()
{
int i,n;
clrscr();
printf("Enter the No. of students:");
scanf("%d",&n);
printf("Enter the student details\n");
printf("Enter the roll no ,name and mark:");
for(i=1;i<=n;i++)
{
printf(\nStudent %d\n,i);
scanf("%d%s%d",&s[i].rno,&s[i].name,&s[i].mark);
}
printf("\n");
printf("Student details are:\n\n");
printf("\t rollno\tname\t\tmarks\n");
for(i=1;i<=n;i++)
printf("\t%d\t\t%s\t\t
%d\n",s[i].rno,s[i].name,s[i].mark);
getch();
}

OUTPUT:
Enter the No. of students:4
Enter the student details
Enter the roll no ,name and mark:
Student 1
1
Mani
80
Student 2
2
Shankar
56
Student 3
3
Anu
97
Student 4
4
Deepika
76
Student details are:
Rollno

Name

Marks

mani

80

Shankar

56

anu

97

Deepika

76

IMPLEMENTATION OF UNION
#include<stdio.h>
#include<conio.h>
union marks
{
float perc;
char grade;
};
void main()
{
union marks student;
stud.perc=98.5;
stud.grade='A';
clrscr();
printf("\nMark %f stored in address %lu\n",stud.perc,
&stud.perc);
printf("\nGrade %c is stored in address%lu\n",stud.grade,
&stud.grade);
getch();
}

OUTPUT:
Mark 98.500496 is stored in address 4325362
Grade A is stored in address 4325362

Swapping of Two Variables using Call By Value


#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\nEnter the two numbers: ");
scanf("%d %d",&x,&y);
printf("\nBefore Swapping\n\nThe value of x=%d

y=

%d\n",x,y);
display(x,y);
getch();
}
display(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("\nAfter swapping\n\nThe value of x=%d y=%d",a,b);
}
OUTPUT:
Enter the two numbers:3
5
Before Swapping
The value of x=3 y=5
After swapping
The value of x=5 y=3

Swapping of Two Variables using Call By Reference


#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\nEnter the two numbers: ");
scanf("%d %d",&x,&y);
printf("\nBefore Swapping\n\nThe value of x=%d y=
%d\n",x,y);
display(&x,&y);
printf("\nAfter swapping\n\nThe value of x=%d y=%d",x,y);
getch();
}
display(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}

OUTPUT:
Enter the two numbers: 3 4
Before Swapping
The value of x=3

y=4

After swapping
The value of x=4

y=3

You might also like