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

43 C Programmes With Ans

The document contains 15 code snippets showing C programs for calculating area and circumference of a circle, volume of a sphere, lateral surface area of a cone, selling price with profit percentage, interest calculation, displaying data type sizes, temperature conversion from Celsius to Fahrenheit, finding roots of a quadratic equation, finding largest of three numbers, sales commission calculation, multiplication tables using loops, digit sum of a number, sum of natural numbers, number reversal, and checking for prime numbers.

Uploaded by

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

43 C Programmes With Ans

The document contains 15 code snippets showing C programs for calculating area and circumference of a circle, volume of a sphere, lateral surface area of a cone, selling price with profit percentage, interest calculation, displaying data type sizes, temperature conversion from Celsius to Fahrenheit, finding roots of a quadratic equation, finding largest of three numbers, sales commission calculation, multiplication tables using loops, digit sum of a number, sum of natural numbers, number reversal, and checking for prime numbers.

Uploaded by

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

1. Finding the area and circumference of a circle of given radius.

#include<stdio.h>
void main()
{
float r,a,c;
printf("\n Enter the radius:");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf("\n The area of circle is : %f \n",a);
printf(" \n The circumference of circle is : %f ",c);
}
OUTPUT:
enter the radius:3
the area of circle is: 28.260000
the circumference of circle: 18.840000

2. Finding the volume of a sphere of given radius.


#include<stdio.h>
void main()
{
float r,v;
printf("\nenter the radius:");
scanf("%f",&r);
v=3.14*r*r*r*4/3;
printf("\n volume of the sphere:%f",v);
}
OUTPUT:
enter the radius:5
volume of the sphere:523.333313

3. Finding the lateral surface area of a right circular cone of given base radius and height.
#include<stdio.h>
void main()
{
int r,a,l;
printf("enter the radius and slant height");
scanf("%d%d",&r,&l);
a=3.14*r*l;
printf("the lsa of right circular cone is %d",a);
}
OUTPUT
enter the radius and slant height5 4
the lsa of right circular cone is 62*/

4. Finding selling price of an item, given its cost price and profit percent.
#include<stdio.h>
void main()
{
float c,p,s;
printf("enter cost price,profit percentage");
scanf("%f,%f",&c,&p);
s=c+c*p/100;
printf("the selling price of item is:%f",s);
}
OUTPUT:
enter cost price,profit percentage500,5
the selling price of item is:525.000000
5. Finding the interest on a given principal for a given period of time at a given rate of per
year.
#include<stdio.h>
void main()
{
float p,r,i;
int t;
printf("enter the principle,rate,time period:");
scanf("%f,%f,%d",&p,&r,&t);
i=p*t*r/100;
printf("the interest:%f",i);
}
OUTPUT
enter the principle,rate,time period:1000 5 3
the interest:150.000000
6. Write a C program to display all the sizes of data types in C.
#include<stdio.h>
void main()
{
printf("the size of char: ");
printf(" %d\n",sizeof(char));
printf("the size of signed char:");
printf("%d\n", sizeof(signed char));

printf("the size of unsigned char:");


printf("%d\n", sizeof(unsigned char));

printf("the size of int:");


printf("%d\n", sizeof(int));

printf("the size of float:");


printf("%d\n",sizeof(float));

printf("the size of double");


printf("%d\n",sizeof(double));
printf("the size of long int");
printf("%d\n",sizeof(long int));

printf("the size of short int");


printf("%d\n",sizeof(short int));

printf("the size of signed int");


printf("%d\n",sizeof(signed int));

printf("the size of unsigned int");


printf("%d\n",sizeof(unsigned int));

printf("the size of unsigned long int");


printf("%d\n",sizeof(unsigned long int));

printf("signed long int");


printf("%d\n",sizeof(signed long int));
}
OUTPUT
the size of char: 1
the size of signed char:1
the size of unsigned char:1
the size of int:4
the size of float:4
the size of double8
the size of long int4
the size of short int2
the size of signed int4
the size of unsigned int4
the size of unsigned long int4
signed long int4
7. Write a C program to find the temperature in Celsius to Fahrenheit
#include<stdio.h>
void main()
{
float f,c;
printf("enter temperature in Celsius::");
scanf("%f",&c);
f=1.8*c+32;
printf("\n the temperature in Fahrenheit:%f",f);
}
OUTPUT
enter temperature in celsius::50

the temperature in farenheit:122.000000


8. Write a C program to find the roots and nature of the roots of a quadratic equation, given
its coefficients.
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,x,y;
printf("Enter the coefficients:");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf("The nature of roots:real and equal");
x=y=(-b)/(2*a);
printf("\nThe roots are %f and %f\n ",x,y);
}
else if(d>0)
{
printf("The nature of roots:real and +ve");
x=(-b+sqrt(b*b - 4*a*c))/(2*a);
y=(-b-sqrt(b*b - 4*a*c))/(2*a);
printf("\nThe roots are %f and %f\n ",x,y);
}
else
{
printf("The nature of roots:imaginary");
}
}
OUTPUT:
enter the coefficients 2 -4 2
The nature of roots:real and equal
the roots are 1.000000 and 1.000000
9. Write a C program for finding the largest of three given numbers.
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf(" The largest number is %d ",a);
}
else if(b>c)
{
printf(" The largest number is %d ",b);
}
else
{
printf(" The largest number is %d ",c);
}
}
OUTPUT:
enter three numbers12
13
11
The largest number is 13

10. A salesman gets a commission of 5% on the sales he makes if his sales is below
Rs.5000/- and a commission of 8% on the sales that exceeds Rs.5000/- together with
Rs.250/-. Write an algorithm or a flowchart and develop C program for computing the
commission of the salesman, given his sales.
#include<stdio.h>
void main()
{
int sales;
float comm;
printf("enter the amount of sales");
scanf("%d",&sales);
if(sales<5000)
{
comm=(float)5*sales/100;
}
else
{
comm=(float)8*sales/100+250;
}
printf("%f is the commission",comm);
}

OUTPUT
enter the amount of sales 4999
249.949997 is the commission

11. Write three C programs to print a multiplication table for a given number using while, do-
while, and for loops.
Using FOR Loop:
#include<stdio.h>
void main()
{
int i=1,n,c;
printf("enter the number of table:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
c=n*i;
printf("%d * %d = %d\n",n,i,c);
}
}
OUTPUT
enter the number of table:5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Using WHILE Loop:
#include<stdio.h>
void main()
{
int i=1,n,c;
printf("enter the number of table:");
scanf("%d",&n);
while(i<=10)
{
c=n*i;
printf("%d * %d = %d\n",n,i,c);
i++;
}
}
OUTPUT
enter the number of table:5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Using DO-WHILE Loop:


#include<stdio.h>
void main()
{
int i=1,n,c;
printf("enter the number of table:");
scanf("%d",&n);
do
{
c=n*i;
printf("%d * %d = %d\n",n,i,c);
i++;
} while(i<=10);
}
OUTPUT
enter the number of table:5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

12. Write a C program to find the sum of the digits of a positive integer.
#include<stdio.h>
void main()
{
int sum=0,n,rem;
printf("enter the number");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("the sum of digits of given number %d",sum);
}
OUTPUT
enter the number321
the sum of digits of given number 6
13. Write a C Program to Calculate the Sum of Natural Numbers
#include<stdio.h>
void main()
{
int sum=0,i,n;
printf("enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("\n Sum=%d",sum);
}
OUTPUT
enter the value of n 3
Sum=6
14. Write a C Program to Reverse a Number
#include<stdio.h>
void main()
{
int sum=0,n,rem;
printf("enter the value of n");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
sum=sum*10+rem;
n=n/10;
}
printf("\n Reverse of a number =%d",sum);
}

OUTPUT
enter the value of n123

Reverse of a number =321


15. C Program to Check Whether a Number is Prime or Not
#include<stdio.h>
void main()
{
int i,n,c=0;
printf("enter any number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}

if(c==2)
{
printf("given %d is a prime number",n);
}
else
{
printf("given %d is not a prime number",n);
}
}

OUTPUT
enter any number5
given 5 is a prime number
16. Write a C Program to Check Whether a Number is Palindrome or Not
#include<stdio.h>
void main()
{
int sum=0,n,rem,t;
printf("enter the value of n");
scanf("%d",&n);
t=n;
while(n!=0)
{
rem=n%10;
sum=sum*10+rem;
n=n/10;
}
if(t==sum)
printf("\n given %d is a palindrome number ",t);
else
printf("\n given %d is not a palindrome number ",t);

OUTPUT
enter the value of n 121

given 121 is a palindrome number


17. Write a C Program to Check Armstrong Number ex:153=13+53+33
#include<stdio.h>
void main()
{
int sum=0,n,rem,t;
printf("enter the value of n");
scanf("%d",&n);
t=n;
while(n!=0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(t==sum)
printf("\n given %d is a Armstrong number ",t);
else
printf("\n given %d is not a Armstrong number ",t);

OUTPUT
enter the value of n 153

given 153 is a Armstrong number


18. Write a C Program to Display Fibonacci Sequence
#include<stdio.h>
void main()
{
int n,i=0,c,a=0,b=1;
printf("enter any number ");
scanf("%d",&n);
printf("%d %d ",a,b);
while(i<n-1)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
i++;
}
}
OUTPUT:
enter any number
5
01123
19. Write a C Program to Display nth Fibonacci number
#include<stdio.h>
void main()
{
int n,i=0,c,a=0,b=1;
printf("enter any number ");
scanf("%d",&n);
while(i<n-1)
{
c=a+b;
a=b;
b=c;
i++;
}

printf("%dth term %d ",n,c);


}
OUTPUT:
enter any number
5
5th term 3
20. Write a C Program to Find Factorial of a Number.
#include<stdio.h>
void main()
{
int fact=1,i,n;
printf("enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\n Factorial of a given %d! = %d",fact);
}
OUTPUT
enter the value of n 5
Factorial of a given 5! = 120
21. A function that takes an integer n as argument and returns 1 if it is a prime number and 0
otherwise.
#include<stdio.h>
int findprime(int);
void main()
{
int temp,n;
printf("enter any number");
scanf("%d",&n);
temp=findprime(n);
if(temp==1)
printf("prime");
else
printf("not a prime");
}
int findprime(int x)
{
int i,c=0;
for(i=1;i<=x;i++)
{
if(x%i==0)
{
c++;
}
}
if(c==2)
return 1;
else
return 0;
}
OUTPUT
enter any number 9
not a prime
22. A function that takes a real number x and a positive integer n as arguments and returns
xn. Date:18/10/16
#include<stdio.h>
#include<math.h>
float power(float x,int n);
void main()
{
float x;
int n;
printf("enter the values of n,x");
scanf("%d%f",&n,&x);
printf("\npower of (%d,%f) is %f",n,x,power(x,n));
}
float power(float x,int n)
{
float c;
c=pow(x,n);
return c;
}
OUTPUT
enter the values of n,x 2 3

power of (2,3.000000) is 9.000000


23. A function that takes a positive integer n as an argument and returns the nth Fibonacci
number.
#include<stdio.h>
int fib(int);
void main()
{
int n;
printf("enter the value of n");
scanf("%d",&n);
printf("\n the nth fibonacci series is =%d",fib(n));
}
int fib(int n)
{
int a=0,b=1,c,i;
if(n==1)
return a;
else if(n==2)
return b;
else
{
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
}
return c;
}
OUTPUT:
enter the value of n 5

the nth fibonacci series is =3


24. Write a C program in the menu driven style to perform the operations +, -, *, /, %
between two given integers using functions.
#include<stdio.h>
#include<stdlib.h>
int add(int,int);
int sub(int,int);
int mul(int,int);
float divi(int,int);
void main()
{
int a,b,c,choice;
float d;
while(1)
{
printf("\nenter the values of a,b");
scanf("%d%d",&a,&b);
printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit");
printf("\nEnter ur choice");
scanf("%d",&choice);
switch(choice)
{
case 1:c=add(a,b);
printf("%d+%d=%d",a,b,c);
break;
case 2:c=a-b;
printf("%d-%d=%d",a,b,c);
break;
case 3:c=a*b;
printf("%d*%d=%d",a,b,c);
break;
case 4:d=divi(a,b);
printf("%d/%d=%f",a,b,d);
break;
case 5:exit(0);
break;
default:printf("INVALID CHOICE");
}
}
}

int add(int x,int y)


{
return x+y;
}

int sub(int x,int y)


{
return x-y;
}

int mul(int x,int y)


{
return x*y;
}

float divi(int x,int y)


{
return (float)x/y;
}
OUTPUT
enter the values of a,b5
5
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter ur choice1
5+5=10
25. Factorial of a non-negative integer n using recursion.
#include<stdio.h>
int fact(int);
void main()
{
int n,result;
printf("enter the value ");
scanf("%d",&n);
result=fact(n);
printf("%d",result);
}
int fact(int n)
{
if(n==0||n==1)
return 1;
else
return n*fact(n-1);
}
OUTPUT
enter the value 5
120
26. Greatest Common Divisor of two integers using recursion.
#include<stdio.h>
int find_gcd(int,int);
void main()
{
int num1,num2,gcd,lcm;
printf("\nEnter two numbers:\n ");
scanf("%d %d",&num1,&num2);
gcd=find_gcd(num1,num2);
printf("\n\nGCD of %d and %d is: %d\n\n",num1,num2,gcd);
}

int find_gcd(int n1,int n2)


{
if(n1!=n2)
{
if(n1>n2)
return find_gcd(n1-n2,n2);
else
return find_gcd(n1,n2-n1);
}
return n1;
}
OUTPUT:
Enter two numbers 10 5

GCD of 10 and 5 is: 5


27. Define macros for the following and use them to find sum of the squares of the minimum
and maximum of two given numbers.
1. Larger of two numbers.
2. Smaller of two numbers.
3. Sum of the squares of two numbers.
#include<stdio.h>
#define max(a,b) (a>b)?a:b
#define min(a,b) (a<b)?a:b
#define squares(a,b) (a*a)+(b*b)
void main()
{
int a,b;
printf("enter a and b values");
scanf("%d%d",&a,&b);
printf("max=%d\n",max(a,b));
printf("min=%d\n",min(a,b));
printf("sum of squares=%d\n",squares(a,b));
}
OUTPUT
enter a and b values 2 3
max=3
min=2
sum of squares=8
28. Write a program to a c program to search an element in an array using linear search.
#include<stdio.h>
void main()
{
int a[10],i,n,key,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
29. Write a C program to sort the numbers using bubble sort.
#include<stdio.h>
void main()
{
int arr[50],temp,i,j,n;
printf("\nEnter any Value less Than 50:");
scanf("%d",&n);
printf("\n Enter The Values into ARRAY: ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(arr[j] >arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n Sorted Series:");
for(i=0;i<n;i++)
{
printf("\n %d",arr[i]);
}
}
Output:
Enter any Value less Than 50: 5
Enter The Values into ARRAY: 1 5 0 3 2
Sorted Series: 0 1 2 3 5
30. To find the mean deviation of given values from the mean and variance.
#include<stdio.h>
#include<math.h>
void main()
{
int a[10],sum=0,size,i;
float mean,temp,var,sd;
printf("enter the size of array");
scanf("%d",&size);
printf("enter array elements");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
mean=sum/size;
temp=0;
for(i=0;i<size;i++)
{
temp=temp+((a[i]-mean)*(a[i]-mean));
}
var=temp/size;
sd=sqrt(var);
printf("mean=%f\n variance=%f",mean,var);
printf("standard deviation :%f",sd);
}
OUTPUT
enter the size of array 5
enter array elements1 2 3 4 5
mean=1.000000
variance=6.000000standard deviation :2.449490
31. To find the location of maximum and minimum values and swap them and display the
result.
#include<stdio.h>
void main()
{
int a[10],min,max,size,max_loc=0,min_loc=0,temp,i;
printf("enter the size of array");
scanf("%d",&size);
printf("enter the elements of array:");
for(i=0;i<size;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=0;i<size;i++)
{
if(a[i]>max)
{
max=a[i];
max_loc=i;
}
if(a[i]<min)
{
min=a[i];
min_loc=i;
}
}
printf("maximum=%d,location=%d",max,max_loc);
printf("minimum=%d,location=%d",min,min_loc);
temp=a[max_loc];
a[max_loc]=a[min_loc];
a[min_loc]=temp;
printf("\n the elements of the array after swapping");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
}
OUTPUT
enter the size of array 5
enter the elements of array:1 2 3 4 5
maximum=5,location=4 minimum=1,location=0
the elements of the array after swapping5 2 3 4 1
32. To find the addition and subtraction of given two matrices.
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],r1,r2,c1,c2;
printf("enter the order of matrix::A");
scanf("%d%d",&r1,&c1);
printf("enter the order of matrix::B");
scanf("%d%d",&r2,&c2);
if(r1==r2&&c1==c2)
{
printf("enter the elements of matrix ::A");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("enter the elements of matrix::B");
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<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}

printf("the elements of matrix c:: resultant matrix");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
printf("the elements of matrix d:: resultant matrix");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",d[i][j]);
}
printf("\n");
}
}
else
printf("\n Not possible");
}
OUTPUT:
enter the order of matrix::A 2 2
enter the order of matrix::B 2 2
enter the elements of matrix ::A 1 2 3 4
enter the elements of matrix::B 1 2 3 4
the elements of matrix c:: resultant matrix
24
68
the elements of matrix d:: resultant matrix
00
00
33. To find the multiplication of two matrices.
#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,r1,r2,c1,c2;
printf("enter the dimensions of matrix A");
scanf("%d%d",&r1,&c1);
printf("enter the dimensions of matrix B");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("enter the elements of matrix A");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of matrix B");
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++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("the resultant matrix c\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
}
else
{
printf("invalid");
}
}
OUTPUT:
enter the dimensions of matrix A 2 3
enter the dimensions of matrix B 3 2
enter the elements of matrix A 1 2 3 4 5 6
enter the elements of matrix B 1 2 3 4 5 6
the resultant matrix c
22 28
49 64
34. To find whether the given matrix is symmetric matrix are not.
#include<stdio.h>
void main()
{
int a[5][5];
int i,j,r1,c1,f=0;
printf("enter the order of matrix:: a");
scanf("%d%d",&r1,&c1);
printf("enter elements of Matrix A:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(a[i][j]!=a[j][i])
{
f=1;
break;
}
}
}
if(f==0)
printf("\t the given matrix is symmetric");
else
printf("\t the given matrix is not symmetric ");

}
OUTPUT:
enter the order of matrix:: a 2 2
enter elements of Matrix A:
1
0
0
1

1 0
0 1
the given matrix is symmetric
35. To find whether the given matrix is unit matrix are not.
#include<stdio.h>
void main()
{
int a[5][5];
int i,j,r1,c1,f=0;
printf("enter the order of matrix:: a");
scanf("%d%d",&r1,&c1);
printf("enter elements of Matrix A:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(a[i][j]!=1)
{
f=1;
break;
}
}
}
if(f==0)
{
printf("the given matrix is a unit matrix");
}
else
{
printf("the given matrix is not a unit matrix");
}
}
OUTPUT:
enter the order of matrix:: a 2 2
enter elements of Matrix A:
1
0
0
1
1 0
0 1
the given matrix is not a unit matrix
36. Write a function to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *,int *);
void main()
{
int a,b;
printf("enter two numbers");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("the values of a and b after swapping::%d %d",a,b);
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
OUTPUT
enter two numbers 2 3
the values of a and b after swapping::3 2
37. Write a function to compute area and circumference of a circle, having area and
circumference as pointer arguments and radius as an ordinary argument.
#include<stdio.h>
void area(float *,float);
void circumference(float *,float);
void main()
{
float r,a,c;
printf("enter the value of radius");
scanf("%f",&r);
area(&a,r);
circumference(&c,r);
printf("the area of circle is::%f",a);
printf("the circumference of circle is::%f",c);
}
void area(float *pa,float r)
{
*pa=3.14*r*r;
}
void circumference(float *pc,float r)
{
*pc=3.14*2*r;
}
OUTPUT:
enter the value of radius 2
the area of circle is::12.560000
the circumference of circle is::12.560000
38. Define functions- length of a string, copy, concatenate, convert into uppercase letters, and
compare two strings for alphabetical order- over strings and implement in a program.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char str1[15],str2[15],str3[10];
int n,c,len,i;
printf("enter the string1:");
gets(str1);
puts(str1);
printf("\n enter the string2:");
gets(str2);
puts(str2);
printf("\n enter the string3:");
scanf("%s",str3);
printf("%s",str3);
printf("\n *************** ");
printf("\n 1.string length ");
printf("\n 2.string copy ");
printf("\n 3.string comparision ");
printf("\n 4.string concat ");
printf("\n 5.upper case ");
printf("\n *************** ");
printf("\n enter the choice you want to perform");
scanf("%d",&n);
switch(n)
{
case 1:len=strlen(str1);
printf("\n the length of the string entered is %d",len);
break;
case 2:strcpy(str1,str2);
printf("\n 1st string=%s,2nd string=%s",str1,str2);
break;
case 3:c=strcmp(str1,str2);
if (c==0)
printf("\n both are equal");
else
printf("\n both are different");
break;
case 4:printf("\n the resultant string is %s",strcat(str1,str2));
break;
case 5:for (i=0;i<strlen(str1);i++)
str1[i]=toupper(str1[i]);
printf("%s",str1);
break;
default:printf("\n enter choice correct");
}
}

OUTPUT:
enter the string1:srinidhi
srinidhi

enter the string2:college


college

enter the string3:engineering


engineering
***************
1.string length
2.string copy
3.string comparision
4.string concat
5.upper case
***************
enter the choice you want to perform5
SRINIDHI
39. Define a structure student having members roll no., name, class, section, marks. Create
an array of 5 students give the data and find the average marks, section-wise.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[15];
char rno[15];
char branch[15];

int marks[3];
int sum;
float avg;
}s[3];

void main()
{
int i,j;
for(i=0;i<3;i++)
{
s[i].sum=0;
printf("\n*******enter student details******",i+1);
printf("\n enter name::");
scanf("%s", s[i].name);
printf("\n enter roll no::");
scanf("%s",s[i].rno);
printf("\n enter branch::");
scanf("%s",s[i].branch);

for(j=1;j<=3;j++)
{
printf("\n enter marks in %d subject",j);
scanf("%d", &s[i].marks[j-1]);
s[i].sum=s[i].sum+s[i].marks[j-1];
}

s[i].avg=s[i].sum/3.0;
}
printf("\n *****student details********",i);
printf("\n");
printf("name\t rno\t branch\t avg");
printf("\n");

for(i=0;i<3;i++)
{
if(strcmp(s[i].branch,"IT")==0)
{
printf("%s", s[i].name);
printf("\t %s ",s[i].rno);
printf("\t %s ",s[i].branch);
printf("\t %f",s[i].avg);
printf("\n");
}
}

for(i=0;i<3;i++)
{
if(strcmp(s[i].branch,"ece")==0)
{
printf("%s", s[i].name);
printf("\t %s ",s[i].rno);
printf("\t %s ",s[i].branch);
printf("\t %f",s[i].avg);
printf("\n");
}
}
}
OUTPUT:
*******enter student details******
enter name::mani

enter roll no::120

enter branch::IT

enter marks in 1 subject 12

enter marks in 2 subject 11

enter marks in 3 subject 11

*******enter student details******


enter name::sufia

enter roll no::121

enter branch::ece

enter marks in 1 subject 11

enter marks in 2 subject 11

enter marks in 3 subject 11

*******enter student details******


enter name::jaswanth

enter roll no::122

enter branch::ece

enter marks in 1 subject 11

enter marks in 2 subject 11

enter marks in 3 subject 11

*****student details********
name rno branch avg
mani 120 IT 11.333333
sufia 121 ece 11.000000
jaswanth 122 ece 11.000000
40. Write a program to Copy a file into some other file.
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fpt1,*fpt2;
char ch;
fpt1=fopen("input.txt","r");
if(fpt1==NULL)
{
printf("\n file doesnot exist");
exit(1);
}
else
{
fpt2=fopen("output.txt","w");
while((ch=fgetc(fpt1))!=EOF)
{
fputc(ch,fpt2);
}
printf("File copied to output.txt file");
}
fcloseall();
}
OUTPUT:
File copied to output.txt file

Input.txt output.txt
HELLO SREENIDHI HELLO SREENIDHI

41) write a c program to print the days in switch.

#include<stdio.h>
main()
{
int a;
printf("enter the number");
scanf("%d",&a);
switch(a)
{
case 0:
printf("Sunday\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
default:
printf("wrong choice\n");
}
}

OUTPUT: enter the number


2
Tuesday

42) write a c program to print the months in switch.


#include<stdio.h>
main()
{
int a;
printf("enter the number");
scanf("%d",&a);
switch(a)
{
case 1:
printf("jan\n");
break;
case 2:
printf("feb\n");
break;
case 3:
printf("mar\n");
break;
case 4:
printf("apr\n");
break;
case 5:
printf("may\n");
break;
case 6:
printf("jun\n");
break;
case 7:
printf("jul\n");
break;
case 8:
printf("aug\n");
break;
case 9:
printf("sep\n");
break;
case 10:
printf("oct\n");
break;
case 11:
printf("nov\n");
break;
case 12:
printf("dec\n");
default:
printf("wrong choice\n");
}
}

output:
enter the number 5
may

43) write a c program to calculate add,sub,mul,div,mod of given two no’s using switchcase.

#include<stdio.h>
main()
{
int a,b,c,choice;
printf("enter the values");
scanf("%d%d%d",&a,&b,&choice);
switch(choice)
{
case 1:
printf("add\n");
c=a+b;
printf("%d\n",c);
break;
case 2:
printf("sub\n");
c=a-b;
printf("%d\n",c);
break;
case 3:
printf("mul\n");
c=a*b;
printf("%d\n",c);
break;
case 4:
printf("div\n");
c=a/b;
printf("%d\n",c);
break;
case 5:
printf("mod\n");
c=a%b;
printf("%d\n",c);
break;
default:
printf("wrong choice\n");
}
}

Output: enter the values34 32 1


add
66

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy