Professional Documents
Culture Documents
CP - Experiments - All 2
CP - Experiments - All 2
Exp: 1 to 18
Experiment No.1
• Write a program to accept the distance between two cities in meters. Calculate and represent the distance in kilometers and meters.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,k,mt;
printf("Enter the distance between two cities in metre:\n");
scanf("%d",&m);
k= m/1000;
mt= m%1000;
printf("Kilometer=%d\n",k);
printf("meter=%d\n",mt);
return 0;
}
Eg: Input -1200 meter
Output- 1 Kilometer 200 meters
Experiment No.2
Write a program to find the largest of the 4 numbers using conditional operator.
#include <stdio.h>
#include <stdlib.h>
/* Write a program to find the largest of the 3 numbers using conditional operator.*/
int main()
{
int a,b,c,l;
/*int a,b,c,d,l;*/
printf("Enter the number;\n");
scanf("%d%d%d",&a,&b,&c);
/*scanf("%d%d%d%d",&a,&b,&c,&d);*/
/* l=a>b&&a>c&&a>d?a:b>c&&b>d?b:c>d?c:d;*/
l=a>b&&a>c?a:b>c?b:c;
printf("Largest number is %d\n",l);
return 0;
}
Experiment No.3
Write a program to illustrate increment/decrement operators.
#include <stdio.h>
#include <stdlib.h>
/*Write a program to illustrate increment/decrement operators.*/
int main()
{
int a,b,c,d;
a=b=c=d=10;
printf("Step:1 \t a=%d\t b=%d\t c=%d\t d=%d \n",a,b,c,d);
a= b++ + c++ + d++ ;
printf("Step:2 \t a=%d\t b=%d\t c=%d\t d=%d \n",a,b,c,d);
b= --a + --b + --c ;
printf("Step:3 \t a=%d\t b=%d\t c=%d\t d=%d \n",a,b,c,d);
c= a++ * ++b % --d ;
printf("Step:4 \t a=%d\t b=%d\t c=%d\t d=%d \n",a,b,c,d);
d = a*b % ++c - c++ ;
printf("Step:5 \t a=%d\t b=%d\t c=%d\t d=%d \n",a,b,c,d);
return 0;
}
Experiment No.4
• Write a program to implement the following math library functions:
• Floor()
• Ceil()
• Sqrt()
• Pow()
Experiment No.4
#include <stdio.h>
#include <stdlib.h>
# include<math.h>
/*program to implement the following math library functions:*/
int main()
{
float a=7.2,b;
int m,n;
m=floor(a);
n=ceil(a);
printf("result of floor of %f=%d",a,m);
printf("\n result of ceil of %f=%d",a,n);
b=sqrt(a);
printf("\n result of square root of %f=%f",a,b);
b=pow(m,n);
printf("\n%d raised to %d is =%f",m,n,b);
return 0;
}
Experiment No.5
• #include <stdio.h>
• #include <stdlib.h>
• /* Program to check whether given year is Leap year or not */
• int main()
• {
• int y;
• printf(" Enter the year:");
• scanf(" %d", &y);
• if (y%4==0 && y%100 !=0 || y%400==0)
• else
• return 0;
• }
Experiment No.6
• #include <stdio.h>
• #include <stdlib.h>
• /* Program to display the grade of given pointer */
• int main()
• {
• float p;
• printf(" enter the pointer\n");
• scanf("%f", & p);
• if (p==10.0)
• printf(" O Grade");
• else
• if (p>=9.0)
• printf("A Grade");
• else
• if (p>=8.0)
• printf("B Grade");
• else
Experiment No.6 Cont.
• if (p>=7.0)
• printf("C Grade");
• else
• if (p>=6.0)
• printf("D Grade");
• else
• if (p>=5.0)
• printf("E Grade");
• else
• if (p>=4.0)
• printf("P Grade");
• else
• printf("F Grade");
• return 0;
• }
Experiment No.7
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:7 Menu driven program to perform arithmetic operation as per the menu stated */
• int main()
• {
• int a,b,k;
/* read Input */
• printf(" Enter the numbers:\n");
• scanf("%d %d",&a,&b);
• /* Display Menu */
• printf("\n Press 1 for addition");
• printf("\n Press 2 for Subtraction");
• printf("\n Press 3 for Multiplication");
• printf("\n Press 4 for Division");
• printf("\n Press 5 for Modulus");
• printf("\n Press 6 for Exit");
• /* Read Choice */
• printf(" \n Enter your choice; \n");
• scanf("%d",&k);
Experiment No.7 Cont.
• /* Perform calculation */
• switch (k)
• {
• case 1: printf(" Addition is:%d", a+b);
• break;
• case 2: printf(" Subtraction is:%d", a-b);
• break;
• case 3: printf(" Multiplication is:%d", a*b);
• break;
• case 4: printf(" Division is:%d", a/b);
• break;
•
Experiment No.7 Cont.
• case 5: printf(" Modulus is:%d", a%b);
• break;
• case 6: break;
• default: printf(" Invalid Entry");
• }
• return 0;
• }
Experiment No.8
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:8 Write a program to display reverse of a number using while loop.*/
• int main()
• {
• int n,r,rev=0;
• #include <stdlib.h>
• /* Exp:9 Write a program to find area of circle. The program should restart if user wants to calculate the area for a new radius based on an exit choice.*/
• int main()
• {
• int k;
• float r,a;
• do
• {
• scanf("%f",&r);
• a=3.14*r*r;
• printf("Do you wish to continue?\n Press 1 for Yes & \n Press 2 for No \n");
• scanf("%d",&k);
• }while (k==1);
• return 0;
• }
Experiment No.10
• #include <stdio.h>
• #include <stdlib.h>
• #include <math.h>
• /* Exp: 10 Write a program to calculate the value of the following series.*/
• int main()
• {
• float n,i;
• float sum=0;
• printf("Enter the number:");
• scanf("%f",&n);
• for(i=1;i<n+1;i++)
• {
• printf("\nSum = %f",sum);
• sum = sum + (i/(i+1));
• }
• printf("Sum = %f",sum);
• return 0;
• }
Experiment No.11
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:11 Write a program to display the stars for the users specified number of lines.*/
• int main()
• {
• int n,r,c;
• printf("How many rows?\n");
• scanf("%d",&n);
• for(r=1;r<=n;r++)
• {
• for(c=1;c<=r;c++)
• {
• printf("*");
• }
• printf("\n");
• }
• return 0;
• }
Experiment No.12
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:12 Write a program to check if the entered number is prime number or not.*/
• int main()
• {
• int n,i,flag;
• printf("Enter any number:");
• scanf("%d",&n);
• flag=0;
• for(i=2;i<=n-1; i++)
• {
• if (n%i==0)
• {flag=1;
• break;
• }
• }
Experiment No.12 Cont.
• if(flag==0)
• printf("Entered number is a prime number\n");
• else
• printf("Entered number is not a prime number\n");
• return 0;
• }
Experiment No.13
• Write a program to find value of y using user defined function, where y=xn
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:13 Write a program to find value of y using user defined function, where y=x^n */
• int main()
• {
• int x,y; /* Input data*/
• double power(int, int); /* Prototype declaration */
• printf("Enter x,y:");
• scanf("%d %d",&x,&y);
• printf("%d to power %d is %f \n", x,y,power(x,y));
• }
• double power (int x, int y)
• {
• double p;
• p=1.0; /* x to power zero */
• if(y>=0)
• while(y--) /* computes positive powers */
• p*=x;
• else
• while(y++) /* computes negative powers */
• p/=x;
• return(p); /* returns double type */
• }
Experiment No.14
• #include <stdio.h>
• #include <stdlib.h>
• /* Write a program to find factorial of a given number, using a recursive function.*/
• int fact(int);
• int main()
• {
• int n,f;
• f=fact(n);
• printf("\n Factorial Value==%d",f);
• return 1;
• }
Experiment No.14 Cont.
• int fact(int num)
• {
• int f;
• if(num==1)
• {
• return(1);
• }
• else
• {
• f=num*fact(num-1);
• }
• return(f);
• }
Experiment No.14a
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:14 Write a program to find Fibonacci series of a given number, using a recursive function.*/
• int main()
• {
• int n,i=0,c;
• printf("\n Enter value of n:");
• scanf("%d",&n);
• int main()
• {
• int array[10],i,j,temp;
• printf("Enter 10 elements for array:");
• for(i=0;i<10;i++)
• {
• scanf("%d",&array[i]);
• }
• for(i=0;i<9;i++)
• {
• for(j=i+1;j<10;j++)
• {
Experiment No.15 Cont.
• if(array[j]<array[i])
• {
• temp=array[i];
• array[i]=array[j];
• array[j]=temp;
• }
• }
• }
• #include <stdlib.h>
• int main()
• {
• int m,n;
• int matrix1[m][n],matrix2[m][n],matrix3[m][n];
• int i=0,j=0;
• scanf("%d%d",&m,&n);
• for(i=0;i<m;i++)
• {
• for(j=0;j<n;j++)
• {
• scanf("%d",&matrix1[i][j]);
• }
• }
• {
• for(j=0;j<n;j++)
• {
• printf("%d\t",matrix2[i][j]);
• }
• printf("\n\t\t");
• }
• for(i=0;i<m;i++)
• {
• for(j=0;j<n;j++)
• {
• matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
• printf("%d\t",matrix3[i][j]);
• }
• printf("\n\t\t");
• }
• return 0;
• }
Experiment No.17
• #include <stdio.h>
• #include <stdlib.h>
• int main()
• {
• getstrlen();
• strconcat();
• }
• void getstrlen()
• {
• char s1[20];
• int i=0;
• gets(s1);
• while(s1[i]!='\0')
• {
• i++;
• }
• }
Experiment No.17 Cont.
• void strconcat()
• {
• int i,j;
• char str1[20],str2[20];
• printf("\n\n Enter first string:");
• gets(str1);
• printf("\n\n Enter second string:");
• gets(str2);
• for(i=0;str1[i]!='\0';++i);// This loop would concatenate the string sr2 at the end of str1
• for(j=0;str2[j]!='\0';++j,++i)
• {
• str1[i]=str2[j];
• }
• str1[i]='\0';// \0 represents end of string
• printf("\nOutput:%s\n",str1);
• return 0;
• }
Experiment No.18
• #include <stdio.h>
• #include <stdlib.h>
• /* Exp:18 Write a program to check whether the entered string is palindrome or not using
string functions.*/
• int main()
• {
• int len=0,i=0;
• char str[10];
• int flag=0;
• printf("Enter string:");
• scanf("%s",str);
• len=strlen(str);
Experiment No.18 Cont.
• while(i<len/2)
• {
• if (str[i]!=str[len-i-1])
• {
• flag=1;
• break;
• }
• i++;
• }
• if(flag==0)
• return 0;
• }
Experiment No.19
• Write a program to read the details of ‘N’ employees (ID, Name, Salary) and display the
details of those employees whose salary is above Rs.10,000/-.
Experiment No.20
• #include <stdio.h> void main()
• {
• int n,*p,sum=0,i; float avg;
• printf("\nEnter Total Numbers: "); scanf("%d",&n);
•
• for(i=0;i<n;i++)
• {
• printf("\nENTER NUMBER %d: ",i+1); scanf("%d",(p+i));
• }
• for(i=0;i<n;i++)
• sum=sum+*(p+i); avg=(float)sum/n;
• printf("\nTHE AVERAGE OF THE NUMBERS IS %0.2f",avg);
• }
Experiment No.20
1. #include <stdio.h>
2. #include <conio.h>
3. void main()
4. {
5. int n,*p,sum=0,i;
6. float avg;
7. clrscr();
8. printf("\nHOW MANY NUMBERS: ");
9. scanf("%d",&n);
10. p=(int *) malloc(n*2);
Experiment No.20
1. if(p==NULL)
2. {
3. printf("\nMEMORY ALLOCATION UNSUCCCESSFUL");
4. exit();
5. }
6. for(i=0;i<n;i++)
7. {
8. printf("\nENTER NUMBER %d: ",i+1);
9. scanf("%d",(p+i));
10. }
11. for(i=0;i<n;i++)
12. sum=sum+*(p+i);
13. avg=(float)sum/n;
14. printf("\nTHE AVERAGE OF THE NUMBERS IS %0.2f",avg);
15. getch();
16. }