Frequently asked C-Programs


1)Program find sum of digits of a number

#include<stdio.h>
#include<conio.h>
void main()
{
int n,temp,rem,ans=0;
clrscr();
printf("enter a no");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
ans+=rem;
n=n/10;
}
printf("the sum of digits of %d is %d\n",temp,ans);
getch();
}

OUTPUT:


enter a no234



the sum of digits of 234 is 9

2)Program to print Fibonacci series
void main()
{
int i,n,f,f1,f2;
printf("enter the range");
scanf("%d",&n);
f=0;
f1=1;
f2=1;
do
{
i++;
printf("%d\n",f);
f1=f2;
f2=f;
f=f1+f2;
}
while(i<=n);
}
OUTPUT:

Enter the range 9
0 1 1 2 3 5 8 13 21

 3)Program to generate prime number till nth number
void main()
{
int n,i,fact,j;
printf("enter the range");
scanf("%d",&n);
printf(“Prime numbers are\n”);
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=n;j++)
{
if(i%j==0)
fact++;
if(fact==2)
printf("%d  “,i);
}
getch();
}
OUTPUT:
Enter the range 10
Prime numbers are
3  5  7

  4)To find the sum of the terms of the given series by the input value x.

The given series is 1-X2/2! + X4/4! –X6/6! +X8/8! –X10/10!

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int p,i,j;
float x,sum=0,fact=1;
clrscr();
printf("enter the value of x\t");
scanf("%f",&x);
for(i=0,p=0;p<=10;i++,p+=2)
{
for(j=1;j<=p;j++)
{
fact=fact*j;
}
sum=sum+(pow(-1,i)*pow(x,p))/fact;
}
printf("\nThe sum of series 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!");
printf("\n when x value %f is %f",x,sum);
getch();
}
OUTPUT:

Input: enter the value of x   2
           The sum of series 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!



           when x value 2.000000 is – 0.668518
  
5)Program to calculate the roots of a Quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,rp,ip;
clrscr();
printf("\n enter a b c values");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf("not a QE");
else
{
d=b*b-4*a*c;
if(d>=0)
{
printf("\n roots are real");
r1=(-b+sqrt(d))/(2*a);
r2=(b-sqrt(d))/(2*a);
printf("\n roots are %f and %f",r1,r2);
}
else
{
printf("\n roots are imaginary");
rp=-b/(2*a);
ip=sqrt(-d)/(2*a);
printf("\n roots are (%f,%f) and (%f,%f)",rp,ip,rp,-ip);
}
}
getch();
}
OUTPUT:

 enter a b c values3 5 2
 roots are real
 roots are -0.666667 and 0.666667
6)Program to calulate the factorial of the given number

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long fact=1;
clrscr();
printf("\n enter n integer");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("factorial is %ld\n",fact);
getch();
}

OUTPUT:

 enter n integer5
factorial is 120

 7)Program to find factorial using recursive and non-recursive functions
C-Program:   using non – recursive technique.
#include<stdio.h>
#include<conio.h>
void main()
{
void ff(int);
int n;
clrscr();
printf("enter the value of n\t");
scanf("%d",&n);
ff(n);
getch();
}
void ff(int n)
{
int i, fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\nThe %d! is %d",n,fact);
} 
  
8)To find the sum of the terms of the given series by the input value x.
The given series is 1-X2/2! + X4/4! –X6/6! +X8/8! –X10/10!

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int p,i,j;
float x,sum=0,fact=1;
clrscr();
printf("enter the value of x\t");
scanf("%f",&x);
for(i=0,p=0;p<=10;i++,p+=2)
{
for(j=1;j<=p;j++)
{
fact=fact*j;
}
sum=sum+(pow(-1,i)*pow(x,p))/fact;
}
printf("\nThe sum of series 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!");
printf("\n when x value %f is %f",x,sum);
getch();
}
OUTPUT:

Input: enter the value of x   2
The sum of series 1-x2/2!+x4/4!-x6/6!+x8/8!-x10/10!
when x value 2.000000 is – 0.668518

9)Program to calculate the roots of a Quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2,rp,ip;
clrscr();
printf("\n enter a b c values");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf("not a QE");
else
{
d=b*b-4*a*c;
if(d>=0)
{
printf("\n roots are real");
r1=(-b+sqrt(d))/(2*a);
r2=(b-sqrt(d))/(2*a);
printf("\n roots are %f and %f",r1,r2);
}
else
{
printf("\n roots are imaginary");
rp=-b/(2*a);
ip=sqrt(-d)/(2*a);
printf("\n roots are (%f,%f) and (%f,%f)",rp,ip,rp,-ip);
}
}
getch();
}
OUTPUT:

 enter a b c values3 5 2
 roots are real
 roots are -0.666667 and 0.666667

10)Program to calulate the factorial of the given number


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long fact=1;
clrscr();
printf("\n enter n integer");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
printf("factorial is %ld\n",fact);
getch();
}

OUTPUT:


 enter n integer5
factorial is 120

11)Program to find factorial using recursive and non-recursive functions
C-Program:   using non – recursive technique.
#include<stdio.h>
#include<conio.h>
void main()
{
void ff(int);
int n;
clrscr();
printf("enter the value of n\t");
scanf("%d",&n);
ff(n);
getch();
}
void ff(int n)
{
int i, fact=1;
for(i=1;i<=n;i++)
}
OUTPUT:

 enter the value of n 6
 the 6! Is 720
12)Using recursive technique.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
clrscr();
printf("enter the value of n\t");
scanf("%d",&n);
f=factorial(n);
printf("\nThe %d! is %d",n,f);
getch();
}
factorial(int x)
{
if(x==1)
return 1;
else
return (x*factorial(x-1));
}
           OUTPUT:

 enter the value of n 6
             the 6! Is 720
 13)Program to find GCD using recursive and non-recursive functions .C-Program using non – recursive technique.

#include<stdio.h>
void main ()
{
int x,y,z;
printf("enter x,y");
scanf("%d%d",&x,&y);
z=gcdr(x,y);
printf("recursive-z=%d",z);
z=gcdn(x,y);
printf("non-recursive-z=%d",z);
}
int gcdr(int a,int b)
{
int r;
r=a%b;
if(r==0)
return(b);
else
return(a);
}
int gcdn(int a,int b)
{
int r;
if(a>b)
{
r=a%b;
while(r!=0)
{
a=b;
b=r;
r=a%b;
}
return(b);
}
else
{
r=b%a;
while(r!=0)
{
b=a;
a=r;
r=b%a;
}
return(a);
  }
  }
OUTPUT:

enter x,y 69
 13
recursive-z=69non-recursive-z=1


14)Program to solve Towers of Hanoi Problem
#include<stdio.h>
int i=0;
void hanoi(int n,char source,char by,char dest)
{
if(n==1)
printf("\n %d more disk from %c to %c \n",++i,source,dest);
else
{
hanoi(n-1,source,dest,by);
hanoi(1,source,by,dest);
hanoi(n-1,by,source,dest);
}
}
void main()
{
char s='a',b='b',d='c';
int n;
clrscr();
printf("enter n value");
scanf("%d",&n);
hanoi(n,s,b,d);
getch();
}

OUTPUT:


 enter n value3
 1 move disk from a to c
 2 move disk from a to b
 3 move disk from c to b
 4 move disk from a to c
 5 move disk from b to a
 6 move disk from b to c
 7 move disk from a to c


 16) The total distance traveled by vehicle in ‘t’ seconds is given by
           distance = ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.)4 and    
           acceleration (m/sec2). Write C program to find the distance trav*-` 0eled at  
           regular intervals of time given the values of ‘u’ and ‘a’. The program 
           should provide the  flexibility to the user to select his own time intervals
           and repeat the calculations for different values of ‘u’ and ‘a’.
#include<stdio.h>
/* to complete distance */
void main()
{
 float s,u,t,a,time,interval;
 char c;
 clrscr();
 while(1)
 {
 printf("\n enter initial velocity u(mts/sec);");
 scanf("%f",&u);
 printf("\n enter accleration a(mts/sec2);");
 scanf("%f",&a);
 printf("\n enter time(secs);");
 scanf("%f",&time);
 printf("\n enter time interval(secs);");
 scanf("%f",interval);
 printf("\n time distance \n");
 for(t=interval;t<=time;t+=interval)
 {
 s=u*t+0.5*a*t*t;
 printf("\n %10.4f %10.4f \n",t,s);
}
printf("\n press any key to continue / space bar to quit");
c=getch();
if(c=="   ")
break;
}
}

OUTPUT:

enter initial velocity u(mts/sec);3
 enter accleration a(mts/sec2);2
 enter time(secs);5.
 enter time interval(secs);1
      time     distance
     1.0000     4.0000
     2.0000    10.0000
     3.0000    18.0000
     4.0000    28.0000
     5.0000    40.0000
press any key to continue / space bar to quit
17)Using switch-case statement, write a C program that takes two operands  
and one operator from the user, performs the operation and then prints   
 the answer. (consider operators +,-,/,* and %).
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
printf("enter a , b values \n");
scanf("%d%d",&a,&b);
printf("enter choice\n");
scanf(" %d",&ch);
switch(ch)
{
case 1:
printf("addtion of a,b is%d",a+b);
break;
 case 2:
printf("subsraction of a,b is%d",a-b);
break;
 case 3:
printf("multiplication  of a,b is %d",a*b);
break;
     case 4:
printf("division of a,b is%d",a/b);
break;
               case 5:
printf("modulas of a,b is%d",a%b);
break;
  default:
  printf("enter right choice:");
  }
  }
OUTPUT:
enter a , b values
5
6
enter choice
3
multiplication  of a,b is 30
enter a , b values
4
2
enter choice
4
division of a,b is2
enter a , b values
2
2
enter choice
5
modulas of a,b is0
enter a , b values
2
3
enter choice
6
enter right choice:
18)Program to find both smallest and largest number in a list of integers
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,min,max,n;
clrscr();
printf("enter n value");
scanf("%d",&n);
printf("enter elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
min=max=a[0];
for(i=0;i<n;i++)
{
 if(max<a[i])
   max=a[i];
 if(min>a[i])
   min=a[i];
}
printf("the max value is %d and the min value is %d",max,min);
getch();
}
OUTPUT:

 enter n value  5
 enter elements 5 2 3 1 6
the max value is 6 and the min value is 1

19)Addition of  two matrices


 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 int a[5][5],b[5][5],c[5][5],i,j,n,m,p,q;
 clrscr();
 printf("enter first matrix size\n");
 scanf("%d%d",&m,&n);
 printf("enter second matrix size\n");
 scanf("%d%d",&p,&q);
 if(m!=p||n!=q)
  printf("size mismatch,Addition is  not possible");
  else
  {
    printf("enter first matrix elements\n");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
               scanf("%d",&a[i][j]);
    printf("enter second matrix elements\n");
    for(i=0;i<p;i++)
    for(j=0;j<q;j++)
               scanf("%d",&b[i][j]);
    printf("addtion of two matrices is\n");
    for(i=0;i<m;i++)
    {
            for(j=0;j<n;j++)
            c[i][j]=a[i][j]+b[i][j];
    }
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
            printf("%4d",c[i][j]);
    printf("\n");
    }
    }
    getch();
    return 0;
    }
OUTPUT:

enter first matrix size
2
3
enter second matrix size
2
3
enter first matrix elements
1
2
3
4
5
6
enter second matrix elements
6
5
4
3
2
1
addtion of two matrices is
   7   7   7
   7   7   7
 20)Multiplecation of  two matrices

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
 int a[5][5],b[5][5],c[5][5],i,j,n,k,m,p,q;
 clrscr();
 printf("enter first matrix size\n");
 scanf("%d%d",&m,&n);
 printf("enter second matrix size\n");
 scanf("%d%d",&p,&q);
 if(n!=p)
  printf("Not compatible,Multiplication  is  not possible");
  else
  {
    printf("enter first matrix elements\n");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
               scanf("%d",&a[i][j]);
    printf("enter second matrix elements\n");
    for(i=0;i<p;i++)
    for(j=0;j<q;j++)
               scanf("%d",&b[i][j]);
    printf("product of two matrices is\n");
    for(i=0;i<m;i++)
    {
            for(j=0;j<q;j++)
            c[i][j]=0;
            for(k=0;k<n;k++)
              c[i][j]+=a[i][k]*b[k][j];
    }
    for(i=0;i<m;i++)
    {
    for(j=0;j<q;j++)
            printf("%4d",c[i][j]);
    printf("\n");
    }
    }
    getch();
    return 0;
    }

OUTPUT:


enter first matrix size
2
3
enter second matrix size
4
3
Not compatible,Multiplication  is  not possible
enter first matrix size
2
3
enter second matrix size
3
1 enter first matrix elements
1
2
3
4
5
6
enter second matrix elements
1
2
3
product of two matrices is
14



32
21)To insert a sub string into a given main string from a given position
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[40],str2[50],substr[10];
int i=0,j=0,k=0,n;
clrscr();
printf("enter a string \n");
scanf("%s",str1);
printf(" enter a sub string to insert\n");
scanf("%s",substr);
printf("enter the position to insert substring\n");
scanf("%d",&n);
while(str1[i]!='\0')
{
str2[j]=str1[i];
j++;
if(i==n)
{
while(substr[k]!='\0')
{
str2[j]=substr[k];
j++;
k++;
}
}
i++;
}
str2[j]='\0';
printf("the string after inserting substring is %s",str2);
getch();
}
OUTPUT:

enter a string
cvsrcollege
 enter a sub string to insert
eng
enter the position to insert substring
3
the string after inserting substring is cvsrengcollege
22)To delete n characters from a given position in a given string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,m,n;
char x[10];
clrscr();
printf("Enter a string \n");
scanf("%s",x);
printf("Enter the starting and ending position of a string to be deleted \n");
scanf("%d%d",&m,&n);
for(i=m;i<=n;i++)
{
printf("%c",x[i]);
getch();
}
}

OUTPUT:

Enter a string
tshewagr
Enter the starting and ending position of a string to be deleted
2 8
Hewagr

23)Write a c program to determine if the given string is palindrome or not
#include<stdio.h>
#include<string.h>
enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
int left,right,len=strlen(string);
enum Boolean matched=true;
if(len==0)
return 0;
left=0;
right=len-1;
/* Compare the first and last letter,second & second last & so on */
while(left<right&&matched)
{
if(string[left]!=string[right])
matched=false;
else
{
left++;
right--;
}
}
return matched;
}
int main()
{
char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);
else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;

OUTPUT:


 Enter a string:liril

The given string liril is a palindrome

 24)Program that displays the position or index in the string S where the string T begins, or -1 if S doesn’t contain T.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[10];
int i;
clrscr(); 
printf("Enter a string \n");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='t'||str[i]=='T')
{
printf("%d",i);
break;
}
}
getch();
}
OUTPUT:

Enter a string
sachintendulkar
6
26)Program to count the lines, words and characters in a given text
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[100],ch;
int w=0,c=0,lines=0,end=0,i;
clrscr();
printf("Enter the text (press enter to exit)\n");
while(end==0)
{
c=0;
while((ch=getchar())!='\n')
{
text[c]=ch;
c++;
}
text[c]='\0';
if(text[0]=='\0')
break;
else
{
w++;
for(i=0;text[i]!='\0';i++)
{
if(text[i]==' ' || text[i]=='\t')
w++;
}
}
lines++;
for(i=0;text[i]!='\0';i++)
{
ch++;
}
}
printf("\n");
printf("Number of lines =%d\n",lines);
printf("Number of words = %d\n ",w);
//printf("Number of characters = %d\n",chars);
printf("Number of characters = %d\n",ch);
getch();
}

OUTPUT:


Enter the text (press enter to exit)
sehwag is a key player for india
dhoni is a cool captain for india
Number of lines =2
Number of words = 14



 Number of characters = 10


27)Program to generate pascal Triangle.
#include<stdio.h>
void main()
{
int binom=1,p,q=0,r,x;
clrscr();
printf("enter the no of rows");
scanf("%d",&p);
while(q<p)
{
for(r=40-3*q;r>0;--r)
{
printf(" ");
}
for(x=0;x<=q;++x)
{
if(x==0||q==0)
binom=1;
else
binom=(binom*(q-x+1)/x);
printf("%6d",binom);
}
printf("\n");
++q;
}
printf("\n");        
  }

OUTPUT:

enter the no of rows5
                                             1
                                          1     1
                                       1     2     1
                                    1     3     3     1
                                 1     4     6     4     1
27)Program to construct a pyramid of numbers
#include<stdio.h>
void main()
{
int p,m,q,n;
clrscr();
printf("enter the no of lines");
scanf("%d",&n);
for(p=1;p<=n;p++)
{
for(q=1;q<=n-p;q++)
printf(" ");
m=p;
for(q=1;q<=p;q++)
printf("%d",m++);
m-=2;
for(q=1;q<p;q++)
printf("%d",m--);
printf("\n\n");
}
}
OUTPUT:

enter the no. of lines5
    1
   232
  34543
 4567654
567898765
28)Program to read two numbers x&n and then sompute sum of this Geometric Expression
1+x+x2+x3+-------+xn
#include<stdio.h>
#include<conio.h>
#define out 0
#define in 1
void main()
{
int c,x,n,i,sum,term;
while(1)
{
clrscr();
sum=1;
term=1;
printf("\n enter n");
scanf("%d",&n);
if(n<0)
{
printf("\n error press any key to come out");
getch();
continue;
}
printf("\n enter x");
scanf("%d",&x);
for(i=0;i<n;i++)
{
term*=x;
sum+=term;
}
printf("\n x=%d,n=%d,sum=%d\n",x,n,sum);
printf("\n press any key to continue / spacebar to quick");
c=getch();
if(c==' ')
break;
}
}
OUTPUT:
 enter n
3
 enter x5
 x=5,n=3,sum=156
 press any key to continue / spacebar to quick

29)Program to find the 2’s complement of a binary number


#include<stdio.h>
#include<conio.h>
#include<string.h>
void complement(char bn[],int c)
{
if(bn[i]=='1')
bn[i]='0';
else
bn[i]='1';
}
void tows_complement(char st[])
{
int i,flg=0;
i=strlen(st)-1;
while(i>=0)
{
if(flg)
complement(st,i);
if((!flg)&&(st[i]=='1'))
flg=1;
i--;
}
}
void main()
char s[25],c;
while(1)
{
clrscr();
printf("\n enter the binary no");
gets(s);
tows_complement(s);
printf("\n the 2s complement is %s",s);
printf("\n press anykey to continue/space bar to quit...");
c=getch();
if(c==' ')
break;
}
}

OUTPUT:
Enter a binary no:101
The 2s complement is:011
Pres any key to continye/space bar to quit
30)write a c program to convert a roman numeral to its decimal equilent
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i<len;i++)
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();

OUTPUT:


Enter the Roman Numeral:III


Its Decimal Equivalent is:3



31)Write a C program that uses functions to perform the following operations:
  i) Reading a complex number
 ii) Writing a complex numbe 
 iii) Addition of two complex number
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)
#include<stdio.h>
#include<math.h>
void arithmetic(int opern);
struct comp
{
double realpart;
double imgpart;
};
void main()
{
int opern;
clrscr();
printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
switch(opern)
{
case 0:
exit(0);
case 1:
case 2:
arithmetic(opern);
default:
printf("Invalid choice\n");
exit(0);
}
getch();
}
void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy)\n Real Part(x) of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part(y) of First Number:");
scanf("%lf",&w1.imgpart);
printf("You Entered complex number %2lf + %2lfi",w1.realpart,w1.imgpart);
printf("\n Real Part(x) of Second Number:");
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part(y) of Second Number:");
scanf("%lf",&w2.imgpart);
printf("You Entered complex number %2f+%2fi",w2.realpart,w2.imgpart);
switch(opern)
{
/*addition of complex number*/
case 1:
w.realpart = w1.realpart+w2.realpart;
w.imgpart = w1.imgpart+w2.imgpart;
break;
/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
break;
}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart);
else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart);
getch();
}

OUTPUT:

***** MAIN MENU *****
Select your option:
1 : ADD
2 : MULTIPLY
0 : EXIT
Enter your Option [ 1]
Enter two Complex Numbers (x+iy)
Real Part(x) of First Number:2
Imaginary Part(y) of First Number:3
You Entered complex number 2.000000 + 3.000000i
Real Part(x) of Second Number:3
Imaginary Part(y) of Second Number:4
You Entered complex number 3.000000+4.000000i
Answer = 5.000000+7.000000iInvalid choice





32)Name of the experiment: 
Area of the circle

Program:

#include<stdio.h>


#define PIE 3.14


main()

{

float r, area;

Printf(“enter r value”);
Scanf(“%d”,&r);
area=PIE*r*r;
printf(“area of the circle %f”,area);
}
Output:
enter r value
3
area of the circle is 28.285

33)Name of the experiment:

Area of the triangle


Program:

#include<math.h>
void main()
{
                  int a,b,c;
                  float s,area;
                  printf("enter the values of a,b,c");
                  scanf("%d%d%d",&a,&b,&c);
                  s=(a+b+c)/2;
                  area=sqrt(s*(s-a)*(s-b)*(s-c));
                  printf("the area of a trangle is =%f",area);
}

Output:

enter the values of a,b,c   10    20    30
The area of a triangle is = 0.000000

34)Name of the experiment

Binary to decimal

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int bin,binary,decimal=0,digits,base=1;
printf(" Enter input binary number ");
scanf("%d",&binary);
bin=binary
while(binary)
{
digit=binary%10;
decimal=digit*base;
base*=2;
binary/=10;
}
printf(“decimal equalant of binary number%d=%d\n”,bin,decimal);
}

Output:

 input a binary no
1001
decimal equalent of binary no 1001= 9

35)Name of the experiment: 

Matrix transpose


Program:



#include< stdio.h>




int main()


{

   int m, n, c, d, matrix[10][10], transpose[10][10];


   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }
   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }
   printf("Transpose of entered matrix :-\n");
   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      } 
      printf("\n");
   }
   return 0;
}
Output:
Eneter the number of rows and coloums:2 3
Enter  the elements of the matrix:
1          2          3
4          5          6
Transpose of entered matrix:
1          4
2          5
3          6

Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment