43 C Programmes With Ans
43 C Programmes With Ans
#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
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));
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
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
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
OUTPUT
enter the value of n 153
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];
}
}
}
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
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 branch::IT
enter branch::ece
enter branch::ece
*****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
#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 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");
}
}