Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Thursday, August 19, 2010

C Programming - read value using scanf () and assign them to structure variables

#include<stdio.h>
#include<conio.h>
main()
{
struct book1
{
char book[30];
int pages;
float price;
};
struct book1 b;
clrscr();
printf("\n enter book name,pages,price:");
scanf("\n %s",b.book);
scanf("\n %d",&b.pages);
scanf("\n %f",&b.price);
printf("\n bookname:%s",b.book);
printf("\n no of pages:%d",b.pages);
printf("\n book price:%f",b.price);
getch();
}

output:-enter book name,pages,price:c
500
450
book name:c
no. of pages:500
price:450

C Programming - find the HCF & LCM of two numbers

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,r,c,d;
clrscr();
printf("\n enter two value a & b=");
scanf("\n %d%d",&a,&b);
c=a*b;
r=b%a;
while(r!=0)
{
b=a;
a=r;
r=b%a;
}
printf("\n hcf=%d",a);
d=c/a;
printf("\n lcm=%d",d);
getch();
}

output:-enter two value=12
36
hcf=12
lcm=36

C Programming - check the number is prime

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,count=0;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&n);
for(i=2;i {
if(n%i==0)
count++;
}
if(count==0)
printf("\n %d is prime number",n);
else
printf("%d is not prime number",n);
getch();
}

output:-enter the number =23
23 is prime number

C Programming - calculate simpleI & compound interest

#include<stdio.h>
#include<conio.h>
main()
{
int p,t;
float r,si,ci;
clrscr();
printf("\n enter the amount,time and rate=");
scanf("\n %d%d%f",&p,&t,&r);
si=(p*r*t)/100;
printf("\n simple interest=%f",si);
ci=p+si;
printf("\n compound interest=%f",ci);
getch();
}

output:-enter the amount,time and rate=3000
4
5
simple interest =600
compound interest =3600

C Programming - display numbers in ascending and descending orders

#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
printf("\n numbers is ascending order :");
for(i=1;i<=10;++i)
{
printf("%3d",i);

}
printf("\n numbers in descending order:");
for(i=10;i>0;i--)
{
printf("%3d",i);
}
getch();
}

output:-
numbers in ascending order :1 2 3 4 5 6 7 8 9 10
numbers in descending order:10 9 8 7 6 5 4 3 2 1

C Programming - calculate the roots of quadratic equation

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,e,x1,x2,d;
clrscr();
printf("\n enter the coefficient=");
scanf("\n %f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("\n imaginary root=");
}
if(d==0)
{
printf("\n roots are equal andis x1=%fandx2=%f",x1,x2);
}
else if(d>0)
{
e=sqrt(d);
x1=(-b+e)/(2*a);
x2=(-b-e)/(2*a);
printf("\n the roots are x1=%fandx2=%f",x1,x2);
}
getch();
}

output:-enter the coefficient=1
-5
6
the root are x1=3.000000 and x2=2.000000

C Programming - calculate sum of range of number

#include<stdio.h>
#include<conio.h>
main()
{
int r1,r2,i,sum=0;
clrscr();
printf("\n enter the range of number=");
scanf("\n %d%d",&r1,&r2);
for(i=r1;i<=r2;i++)
{
sum=sum+i;
}
printf("\n sum of range of number=%d",sum);
getch();
}



Output:-
Enter the range of number=1
10
Sum of range of number=55;

C Programming - find the transpose of given matrix

#include<stdio.h>
#include<conio.h>
main()
{
int mat[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
clrscr();
printf("\n show the matrixs=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",mat[i][j]);
}
printf("\n");
}
printf("\n Transpose of matrixs=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%5d",mat[j][i]);
}
printf("\n");
}
getch();
}

output:-show the matrix=1 2 3
4 5 6
7 8 9
transpose of matrix=1 4 7
2 5 8
3 6 9

C Programming - calculate factorial of given number

#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact=1,i;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
for(i=fact;i<=num;i++)
{
fact=fact*i;
}
printf("\n factoral number=%d",fact);
getch();
}

output:-enter the number=5
factorial number=120

C Programming - convert decimal numbers into its octal

#include<stdio.h>
#include<conio.h>
main()
{
int p=1,num=0,oct=0,d;
clrscr();
printf("\n enter the decimal number=");
scanf("\n %d",&num);
while(num)
{
d=num%8;
oct=oct+p*d;
p=p*10;
num=num/8;
}
printf("\n octal number %d",oct);
getch();
}

output:-enter the decimal number=8
octal number=10

C Programming - check given is an Armstrong number

#include<stdio.h>
#include<conio.h>
main()
{
int num,d,sum=0,a;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
a=num;
while(a)
{
d=a%10;
sum=sum+d*d*d;
a=a/10;
}
if(num==sum)
printf("\n this is armistrong number");
else
printf("\n this is not armistrong number");
getch();
}

output:-enter the number=153
this is armistrong number

C Programming - check given number is palindrome

#include<stdio.h>
#include<conio.h>
main()
{
int num,d,a,r=0;
clrscr();
printf("\n enter the number=");
scanf("\n%d",&num);
a=num;
while(a)
{
d=a%10;
r=r*10+d;
a=a/10;
}
if(num==r)
printf("\n this is a palindrome");
else
printf("\n this is not palindrome");
getch();
}

output:- enter the number=111
this is a palindrome number

C Programming - find the reverse of given number

#include<stdio.h>
#include<conio.h>
main()
{
int num,d,rev=0;
clrscr();
printf("\n enter the number=");
scanf("\n %d",&num);
while(num)
{
d=num%10;
rev=rev*10+d;
num=num/10;
}
printf("\n reverse number=%d",rev);
getch();
}

output:-enter the number=654
reverse number=456

C Programming - find the greatest number among two numbers

#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("\n enter the two number=");
scanf("\n %d%d",&a,&b);
if(a>b)
printf("\n greatest number=%d",a);
else
printf("\n greatest number=%d",b);
getch();
}

output:-enter the two number=12
54
greatest number=54

Sunday, April 11, 2010

c - create personal library in TurboC

first create a header file in which function prototype is written
/* ex.h */
int fact(int);

source file in which function is defined
/* ex.c */
int fact(int x)
{
int i,f;
for(i=x;i>1;i--)
f=f*i;
return f;
}

now in main program we can call fact() function use ex.h header file
/* factorial.c */
#include"ex.h"
void main()
{
int n;
printf("Enter number:");
scanf("%d",&n);
printf("factorial of %d is %d",n,fact(n));
}

/*library file creation */
goto command prompt
c:\tc\bin
/* first create obj file of source file command is as */
c:\tc\bin\tcc -c ex.c /* object file ex.obj created */
c:\tc\bin\tlib ex + ex.obj /* ex.lib library file created */
c:\tc\bin\tcc ex.lib factorial.c /* library file with main program file is connected */
c:\tc\bin\tcc -c factorial.c /*program compile */
factorial /* program run simple writing program and then enter output come out */
enter number 5
factorial of 5 is 120

//* this program is checked and successfully run