Upload
malleesvari-senthil-kumar
View
214
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Cp Program
Citation preview
REG.NO:
/*PROGRAM TO PERFORM BASIC ARITHMETRIC OPERATORS*/
SOURCE CODE
#include <stdio.h>void main(){
int num1,num2;int sum,sub,mult,div,mod;printf("\nEnter First Number :");scanf("%d",&num1);printf("\nEnter Second Number :");scanf("%d",&num2);sum = num1 + num2;printf("\nAddition is : %d",sum);sub = num1 - num2;printf("\nSubtraction is : %d",sub);mult = num1 * num2;printf("\nMultiplication is : %d",mult);div = num1 / num2;printf("\nDivision is : %d",div);mod = num1 % num2;printf("\nModulus is : %d",mod);getch();
}
OUTPUT
Enter First Number : 10Enter Second Number : 5Addition is : 15Subtraction is : 5Multiplication is : 50Division is : 2Modulus is : 0
/*PROGRAM TO FIND AREA AND CIRCUMFERENCE OF A CIRCLE*/
SOURCE CODE
#include<stdio.h>void main() {
REG.NO:
int rad;float PI = 3.14, area, ci;printf("\nEnter radius of circle: ");scanf("%d", &rad);area = PI * rad * rad;printf("\nArea of circle : %f ", area);ci = 2 * PI * rad;printf("\nCircumference : %f ", ci);getch();
}
OUTPUT
Enter radius of circle : 1Area of circle : 3.14Circumference : 6.28
/*PROGRAM TO FIND BIGGEST AMONG TWO NUMBER USING TERNARY OPERATORS*/
SOURCE CODE
#include<stdio.h> #include<conio.h> void main() {
int a,b,c;clrscr();printf("Enter the values of a and b:"); scanf("%d%d" ,&a,&b);
REG.NO:
c = a>b ? a : b; //ternaty operator printf("Larger number=%d" ,c);getch();
}
OUTPUT
Enter the values of a and b:3090Largest number=90
/*PROGRAM TO SWAP TWO NUMBERS WITHOUT USING THIRD VARIABLE*/
SOURCE CODE
#include<stdio.h>void main() {
int a, b;printf("\nEnter value for num1 & num2 : ");scanf("%d %d", &a, &b);a = a + b;b = a - b;a = a - b;printf("\nAfter swapping value of a : %d", a);printf("\nAfter swapping value of b : %d", b);getch();
}
REG.NO:
OUTPUT
Enter value for num1 & num2 : 10 20After swapping value of a : 20After swapping value of b : 10
/*PROGRAM TO FIND MAXIMUM NUMBER FROM THREE NUMBERS*/
SOURCE CODE
# include <stdio.h> # include <conio.h> void main() {
int a, b, c ; clrscr() ; printf("Enter three numbers : ") ; scanf("%d %d %d", &a, &b, &c) ; if(a > b) {
if(a > c) printf("\n%d is the biggest number", a) ;
else printf("\n%d is the biggest number", c) ;
} else {
if(c > b) printf("\n%d is the biggest number", c) ;
else printf("\n%d is the biggest number", b) ;
}
REG.NO:
getch() ; }
OUTPUT
Enter three numbers : 5 15 215 is the biggest number
/*PROGRAM TO FIND THE ROOT OF A QUADRATIC EQUATION*/
SOURCE CODE
#include<stdio.h>#include<conio.h>#include<math.h>void main(){
int a,b,c;float r1,r2,d;printf("Enter value of a : ");scanf("%d", &a);printf("Enter value of b : ");scanf("%d", &b);printf("Enter value of c : ");scanf("%d", &c);d=(b*b)-(4*a*c);if(d>0){
printf("\n ROOTS ARE REAL ROOTS\n");r1 = ((-b) + sqrt(d)) /(2*a);r2 = ((-b) - sqrt(d)) /(2*a);printf("\n ROOTS : %f, %f\n", r1, r2);
}else if(d==0){
printf("\n ROOTS ARE EQUAL\n");r1 = (-b/(2*a));printf("\n ROOT IS...: %f\n", r1);
}else
printf("\n ROOTS ARE IMAGINARY ROOTS\n");
REG.NO:
getch();}
OUTPUTEnter value of a : 6Enter value of b : -13Enter value of c : 6ROOTS ARE REAL ROOTSROOTS : 1.500000, 0.666667
Enter value of a : 1Enter value of b : 1Enter value of c : -2ROOTS ARE REAL ROOTSROOTS : 1.000000, -2.000000
REG.NO:
/*PROGRAM TO CHECK THE GIVEN NUMBER IS EVEN OR ODD*/
SOURCE CODE
#include <stdio.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();}
OUTPUT
Enter an integer you want to check: 2525 is odd.
Enter an integer you want to check: 1212 is even.
/*PROGRAM TO CHECK THE GIVEN NUMBER IS POSITIVE, NEGATIVE OR ZERO*/
REG.NO:
SOURCE CODE
#include <stdio.h> void main() {
int num; printf("Enter any number: "); scanf("%d", &num); if(num>0) {
printf("Number is POSITIVE"); } else if(num<0) {
printf("Number is NEGATIVE"); } else {
printf("Number is ZERO"); } getch();
}
OUTPUT
Enter any number: 10Number is POSITIVE
Enter any number: 0Number is ZERO
Enter any number: -5Number is NEGATIVE
SOURCE CODE
#include<stdio.h>void main()
/*PROGRAM TO SOLVE FOLLOWING SERIES: SUM=1+2+3+….+N*/
REG.NO:
{int n,i;int sum=0;printf("Enter the n i.e. max values of series: ");scanf("%d",&n);sum = (n * (n + 1)) / 2;printf("Sum of the series: ");for(i =1;i <= n;i++){
if (i!=n)printf("%d + ",i);
elseprintf("%d = %d ",i,sum);
}getch();
}
OUTPUT
Enter the n i.e. max values of series: 5Sum of the series: 1 + 2 + 3 + 4 + 5 = 15
/*PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER*/
SOURCE CODE
#include<stdio.h>void main(){
int i=1,f=1,num;printf("Enter a number: ");scanf("%d",&num);while(i<=num){
f=f*i;
REG.NO:
i++;}printf("Factorial of %d is: %d",num,f);getch();
}
OUTPUT
Enter a number: 5Factorial of 5 is: 120
/*PROGRAM TO PRINT FIBONACCI SERIES*/
SOURCE CODE
#include<stdio.h>void main(){
int a=0,b=1,c,i,n;printf("Enter how many elements?");scanf("%d",&n);printf("\n%d %d",a,b);for(i=2;i<n;++i){
c=a+b;printf(" %d",c);a=b;b=c;
}getch();
}
REG.NO:
OUTPUTEnter how many elements? 80 1 1 2 3 5 8 13
/*PROGRAM TO CHECK THE GIVEN NUMBER IS PRIME OR NOT*/
SOURCE CODE
#include <stdio.h>void main(){
int n, i, flag=0;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();
}
OUTPUTEnter a positive integer: 29
REG.NO:
29 is a prime number.
/*PROGRAM TO FIND SUM OF DIGITS OF A GIVEN NUMBER*/
SOURCE CODE
#include<stdio.h>void main(){
int num,sum=0,r;printf("Enter a number: ");scanf("%d",&num);while(num){
r=num%10;num=num/10;sum=sum+r;
}printf("Sum of digits of number: %d",sum);getch();
}
OUTPUTEnter a number: 123Sum of digits of number: 6
REG.NO:
/*PROGRAM TO CHECK THE GIVEN NUMBER IS PALINDROME OR NOT*/
SOURCE CODE
#include<stdio.h>void main(){
int num,r,sum=0,temp;printf("Enter a number: ");scanf("%d",&num);temp=num;while(num){
r=num%10;num=num/10;sum=sum*10+r;
}if(temp==sum)
printf("%d is a palindrome",temp);else
printf("%d is not a palindrome",temp);getch();
}
OUTPUT
Enter a number: 131131 is a palindrome
REG.NO:
/*PROGRAM TO FIND SUM OF 1-D ARRAY*/
SOURCE CODE
#include<stdio.h>void main() {
int i, arr[50], sum, num;printf("\nEnter no of elements :");scanf("%d", &num);printf("\nEnter the values :");for (i = 0; i < num; i++)
scanf("%d", &arr[i]);sum = 0;
for (i = 0; i < num; i++)sum = sum + arr[i];
for (i = 0; i < num; i++)printf("\n a[%d]=%d", i, arr[i]);
printf("\n Sum=%d", sum);getch();
}
OUTPUT
Enter no of elements : 3Enter the values : 11 22 33a[0]=11a[1]=22a[2]=33Sum=66
/*PROGRAM TO FIND LARGEST AND SMALLEST NUMBER FROM 1-D ARRAY*/
SOURCE CODE
REG.NO:
#include<stdio.h>#include<conio.h>void main(){
int a[50],i,n,large,small;clrscr();printf(“\nHow many elements:”);scanf(“%d”,&n);printf(“\nEnter the Array:”);for(i=0;i<n;++i)
scanf(“%d”,&a[i]);large=small=a[0];for(i=1;i<n;++i){
if(a[i]>large)large=a[i];
if(a[i]<small)small=a[i];
}printf(“\nThe largest element is %d”,large);printf(“\nThe smallest element is %d”,small);getch();
}
OUTPUTHow many elements: 6Enter the Array: 2 4 7 3 9 5The largest element is 9The smallest element is 2
/*PROGRAM TO SORT 1-D ARRAY IN ASCENDING ORDER*/
SOURCE CODE
#include<stdio.h>#include<conio.h>void main(){
int a[10],i,j,n,temp;clrscr();
REG.NO:
printf("enter no of element");scanf("%d",&n);printf("enter %d elements of array\n",n);for(i=0;i<n;i++){
scanf("%d",&a[i]);}for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];a[i]=a[j];a[j]=temp;
}}
}printf("\nafter sorting");for(i=0;i<n;i++){
printf("\n%d",a[i]);}getch();
}
OUTPUT
Enter no of element 5Enter 5 elements of array3 4 5 7 2
After sorting2 3457
REG.NO:
/*PROGRAM FOR ADDITION OF TWO MATRICES*/
SOURCE CODE
#include<stdio.h>void main(){
int a[3][3],b[3][3],c[3][3],i,j;clrscr();printf("Enter the First matrix->");for(i=0;i<2;i++)for(j=0;j<2;j++)scanf("%d",&a[i][j]);printf("\nEnter the Second matrix->");for(i=0;i<2;i++)for(j=0;j<2;j++)scanf("%d",&b[i][j]);printf("\nThe First matrix is\n");for(i=0;i<2;i++){
printf("\n");for(j=0;j<2;j++)printf("%d\t",a[i][j]);
}
REG.NO:
printf("\nThe Second matrix is\n");for(i=0;i<2;i++){
printf("\n");for(j=0;j<2;j++)printf("%d\t",b[i][j]);
}for(i=0;i<2;i++)for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];printf("\nThe Addition of two matrix is\n");
for(i=0;i<2;i++){
printf("\n");for(j=0;j<2;j++)
printf("%d\t",c[i][j]);}getch();
}
OUTPUT
Enter the First matrix->5634Enter the Second matrix->9752The First matrix is5 63 4The Second matrix is9 75 2
The Addition of two matrix is14 138 6
REG.NO:
/*PROGRAM FOR MULTIPLICATION OF TWO MATRICES*/
SOURCE CODE
#include<stdio.h>void main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,r1,c1,r2,c2;clrscr();printf("\nEnter the row and column of first matrix:");
scanf("%d %d",&r1,&c1); printf("\nEnter the row and column of second matrix:"); scanf("%d %d",&r2,&c2); if(c1!=r2) { printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
} else { printf("\nEnter the First matrix->"); for(i=0;i<r1;i++) for(j=0;j<c1;j++)
scanf("%d",&a[i][j]); printf("\nEnter the Second matrix->"); for(i=0;i<r2;i++) for(j=0;j<c2;j++)
scanf("%d",&b[i][j]); for(i=0;i<r1;i++) {
for(j=0;j<c2;j++) {
sum=0;for(k=0;k<c1;k++)
sum=sum+a[i][k]*b[k][j];c[i][j]=sum;
}
REG.NO:
} printf("\n The multiplication of two matrix is\n"); for(i=0;i<r1;i++) {
for(j=0;j<c2;j++)printf("%d\t",c[i][j]);
printf("\n"); } } getch();}
OUTPUT
Enter the row and column of first matrix: 2 2Enter the row and column of second matrix: 2 2
Enter the First matrix-> 3 4 5 6
Enter the Second matrix-> 6 8 5 7
The multiplication of two matrix is 38 52 60 82
REG.NO:
/*PROGRAM (MENU DRIVEN) TO IMPLEMENT INBUILT FUNCTIONS OF STRING*/
SOURCE CODE
#include<stdio.h>#include<conio.h>#include<string.h>void main(){
char str[20],str1[20];int ch,i,j;clrscr();do{
printf("\n****MENU****");printf("\n1:Find Length");printf("\n2:Copy the Strings");printf("\n3:Compare the Strings");printf("\n4:Concatenate the Strings");printf("\n5:Exit");printf("\nEnter your choice: ");scanf("%d",&ch);switch(ch){
case 1: printf("\nEnter the string: ");
scanf("%s",&str); i=strlen(str);printf("\nThe Length of given string is: %d",i);break;
case 2: printf("\nEnter the first string: ");
scanf("%s",&str); printf("\nEnter the second string: "); scanf("%s",&str1); strcpy(str,str1); printf("\nThe Copied string is: %s",str); break;
case 3: printf("\nEnter the first string: "); scanf("%s",&str); printf("\nEnter the second string: "); scanf("%s",&str1); j=strcmp(str,str1); if(j==0) {
printf("\nThe string is same"); }
REG.NO:
else {
printf("\nThe string is not same"); }break;
case 4:printf("\nEnter the first string: ");scanf("%s",&str);printf("\nEnter the second string: ");scanf("%s",&str1);strcat(str,str1);printf("\nThe Concatenated string is: %s",str);break;
case 5:exit(0);break;
}while(ch!=5);getch();
}
OUTPUT
****MENU****1:Find Length2:Copy the Strings3:Compare the Strings4:Concatenate the Strings5:ExitEnter your choice: 1
Enter the string: HELLOThe Length of given string is: 5
****MENU****1:Find Length2:Copy the Strings3:Compare the Strings4:Concatenate the Strings5:ExitEnter your choice: 2
Enter the first string: HELLOEnter the second string: WORLDThe Copied string is: WORLD
****MENU****
REG.NO:
1:Find Length2:Copy the Strings3:Compare the Strings4:Concatenate the Strings5:ExitEnter your choice: 3
Enter the first string: HELLOEnter the second string: HELLOThe string is same
****MENU****1:Find Length2:Copy the Strings3:Compare the Strings4:Concatenate the Strings5:ExitEnter your choice: 4
Enter the first string: HELLOEnter the second string: WORLDThe Concatenated string is: HELLO WORLD
****MENU****1:Find Length2:Copy the Strings3:Compare the Strings4:Concatenate the Strings5:ExitEnter your choice: 5
/*PROGRAM TO REVERSE THE STRING AND CHECK WHETHER IT IS PALINDROME OR NOT*/
SOURCE CODE
#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char str[100],strd[100]; int i,l,j=0; clrscr(); printf("\nEnter the string :"); gets(str); l= strlen(str);
for(i=l-1;i>=0;i--) {
REG.NO:
strd[j]=str[i]; j++; } strd[j]='\0'; if(strcmp(str,strd)==0)
printf("string is palindrome"); else
printf("string is not palindrome"); getch();}
OUTPUT
Enter the string: madamString is palindrome
Enter the string: helloString is not palindrome