Saturday, March 6, 2010

NUMBERS DIVIDED BY 7

WAP to print numbers from 1-50 which are divided by 7
void main ()
{
int a;
clrscr ();
a=1;
while (a<=50)
{
if (a%7==0)
printf ("%d\n",a);
a++;
}
getch ();
}

ODD SERIES

WAP to print Odd numbers from 1 to 20
void main ()
{
int a;
clrscr ();
a=1;
while (a<=20)
{
if (a%2==1)
printf ("\n%d",a);
a++;
}
getch ();
}

SQUARE & CUBE

WAP to print series from 1 to 10 & find its square and cube

void main ()
{
int a=1,sqr=0,cube=0;
clrscr ();
while (a<=10)
{
sqr=pow(a,2);
cube=pow(a,3);
printf ("%d\t %d\t %d\n",a,sqr,cube);
a++;
}
getch ();
}

SKIP 5 & 7

WAP to print series from 1 to 10 and skip 5 & 7

void main ()
{
int a;
clrscr ();
for (a=1;a<=10;a++)
{
if (a==5 || a==7)
continue;
printf ("%d\n",a);
}
getch ();
}

SERIES 20 TO 1

WAP to print series from 20 to 1
#include
void main ()
{
int a;
clrscr ();
a=20;
while (a>=1)
{
printf ("\n%d",a);
a--;
}
getch ();
}

PRINT SERIES (VARIABLE)

WAP to print series from start to end using do-while loop
void main ()
{
int a,b;
clrscr ();
printf ("Enter Start: ");
scanf ("%d",&a);
printf ("Enter End: ");
scanf ("%d",&b);
do
{
printf ("%d\n",a);
a++;
}
while (a<=b);
getch ();
}

PRINT STARS

WAP to print Stars on screen
void main ()
{
int i,j;
clrscr();
for (j=1;j<4;j++)
{
for (i=1;i<=5;i++)
{
printf ("*");
}
printf ("\n");
}
getch ();
}

TABLE OF 5

WAP to print table of 5

void main ()
{
int a,tab;
clrscr ();
a=1,tab=0;
while (a<=10)
{
tab=5*a;
a++;
printf ("%d\n",tab);
}
getch ();
}

PASSWORD VERIFICATION

WAP to print the detail of the programmer
if the given number is 464
void main ()
{
int pass;
clrscr();
do
{
printf ("Enter Password to see the detail of programmer:\n");
scanf ("%d",&pass);
}
while (pass!=464);
printf ("\nJagjeet Singh");
printf ("\nB.Sc. (I.T.)\nPunjab Technical University");
getch ();
}

VALUE OF DATA TYPE

WAP to print value of multiple data types
#include
#include
void main ()
{
int a=10;
float d=40.50;
char ch='A';
double dbl=78.9786;
long lng=7897711;
char nm [10]="JIMMY";
clrscr ();
printf ("\nInteger value is %d",a);
printf ("\nFloat value is %.2f",d);
printf ("\nCharacter value is %c",ch);
printf ("\nDouble value is %.4lf",dbl);
printf ("\nLong value is %ld",lng);
printf ("\nString value is %s",nm);
getch ();
}

SUM,SUB,PRODUCT,DIVISION

WAP to Sum, Subtract, Multiply & Division of two numbers (5 Variables)

#include

void main ()
{
int a,b,c,d,e,f;
clrscr();
printf ("Enter A: ");
scanf ("%d",&a);
printf ("Enter B: ");
scanf ("%d",&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf ("\nSum is : %d",c);
printf ("\nSubtraction is : %d",d);
printf ("\nMultiplication is : %d",e);
printf ("\nDivision is : %d",f);
getch ();
}

SUM OF ARRAY

WAP to sum of five elements of an array
void main ()
{
int no[5],i,sum;
clrscr ();
for (i=0;i<=4;i++)
{
printf ("Enter Element: ");
scanf ("%d",&no[i]);
}
sum=no[0]+no[1]+no[2]+no[3]+no[4];
printf ("\nSum of five Elements: %d",sum);
getch ();
}

SWAP NUMBERS

SWAP NUMBERS

WAP to SWAP the three digit number
void main ()
{
int b,r,n,r1,r2;
clrscr ();
printf ("Enter the Value: ");
scanf ("%d",&n);
r=n%10;
n=n/10;
r1=n%10;
n=n/10;
r2=n%10;
n=n/10;
b=(r*100)*(r2*10)+(r2);
printf ("%d%d%d",r,r1,r2);
getch ();
}



 

 

 

FIND THE SUM OF DIGIT THREE Numbers

FIND THE SUM OF DIGIT THREE Numbers

 

/* FIND THE SUM OF DIGIT THREE NO'S*/
#include "math.h"
main()
{
int d,d1,d2,d3,r1,r2,sum;
clrscr();
printf("\n enter any three digit no's");
scanf("%d",&d);
d1=d/100;
r1=d%100;
if(r1!=0)
{
d2=r1/10;
r2=r1%10;
if(r2!=0)
d3=r2;
else
d3=0;
}
else
d2=0;
d3=0;
}
sum=d1+d2+d3;
printf("\n sum of 3 digit no is %d",sum);
getch();
}

Factorial Function In C

Factorial Function In C

#include "stdio.h"
#include "conio.h"
long int factorial(int n);
void main()
{
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("You have this series:- 1/1! + 2/2! + 3/3! + 4/4!");
printf("To which term you want its sum? ");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++)
{
s=s+((float)i/(float)factorial(i));
}
printf("The sum of %d terms is %f",n,s);
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();
}

long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}

Basic example showing constants usage in C

#include
/*constants for bonus rates and sales*/
#define BONUSRATE1 0.1
#define BONUSRATE2 0.15
#define BONUSRATE3 0.2
#define SALES1 2000
#define SALES2 5000
#define SALES3 10000
int main()
{
int sales;
double commission;
/*get employees sales*/
printf("Please enter your total sales to the nearest dollar.\n");
scanf("%d", &sales);
/*calculate employees bonus based on info*/
if(sales <=2000)
{
commission = sales * BONUSRATE1;
printf("%g\n" , commission);
}
else if(sales > 2000 && sales <=5000)
{
commission = sales * BONUSRATE2;
printf("%g\n" , commission);
}
else
{
commission = sales * BONUSRATE3;
printf("%g\n" , commission);
}

return 0;
}

calculate the power in watts

#include
int main()
{
float power,voltage,current;
voltage = current = 0;

printf("Power calculator.\n");
printf("This will calculate the power in watts , ");
printf("when you input the voltage and current.");
/*get the voltage*/
printf("Enter the voltage in volts.\n");
scanf("%f",&voltage);
/*get the current*/
printf("Enter the current in amps.\n");
scanf("%f",&current);
/*calculate the power*/
power = voltage * current;
printf("The power in watts is %.2f watts\n",power);

return 0;
}

Progam that gives length of side of a Triangle

//Progam that gives all details of a Triangle given the lengths of its sides
#include
#include
#include
#include

main()
{
clrscr();
float a,b,c,S,D,A,B,C,Area,R;
printf("Enter the lengths of the three sides of the triangle :");
scanf("%f%f%f",&a,&b,&c);

S = (a+b+c)/2.0; // S is the semiperimeter of the triangle
D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
if(D<=0)
{
printf("The triangle cannot be formed");
getch();
exit(0);
}

if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
// this complex logic is to eliminate interpretting a triangle with all
three
// sides equal as both isosceles and equilateral.
printf("The triangle is ISOSCELES");
if(a==b && b==c && c==a)
printf("The triangle is EQUILATERAL Type");
if(a!=b && b!=c && c!=a)
printf("The triangle is SCALENE");

Area = sqrt(D);
R = (a*b*c)/(4.0*Area);
printf("PERIMETER = %.2f units",(2.0*S));
printf("AREA = %.2f sq.units",Area);
printf("CIRCUM RADIUS = %.2f units",R);
// using sine rule,we get...
A = (180.0/3.1415926)*asin(a/(2.0*R));// value of pi should be upto 7
B = (180.0/3.1415926)*asin(b/(2.0*R));// decimal places of accuracy and
also
C = (180.0/3.1415926)*asin(c/(2.0*R));// note that the 7th decimal place
// 6 and not 7 as it had to be if were
if(A==90.0 || B==90.0 || C==90.0)
// approximated to 7 decimalplaces
printf("The triangle is RIGHT ANGLED");
if(A<90.0 && B<90.0 && C<90.0)
printf("The triangle is ACUTE ANGLED");
if(A>90.0 || B>90.0 || C>90.0)
printf("The triangle is OBTUSE ANGLED");

printf("The angles are as follows :");
printf("A = %.2f degrees",A);
printf("B = %.2f degrees",B);
printf("C = %.2f degrees",C);
printf("Where A,B,C stand for angles opposite to sides%.2f,%.2f,%.2f",a,b,c);
printf(" respectively");


getch();
return 0;
}

Ohms law example In C

#include
#include
#include
int main()
{
char ch;
float voltage , current , resistance , result;
printf("Ohms law calculator.\n");
printf("Please choose from following calculcations.\n");
printf("1. choose 1 to calculate the voltage.\n");
printf("2. choose 2 to calculate the current.\n");
printf("3. choose 3 to calculate the resistance.\n");
printf("Anything else to quit.\n");
scanf("%c",&ch);
switch(ch)
{
case '1' :
printf("please enter the current in amps.\n");
scanf("%f",&current);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = current * resistance;
printf("The voltage is %0.2f volts.\n",result);
break;
case '2' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = voltage / resistance;
printf("The current is %0.2f amps.\n",result);
break;
case '3' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the current in amps.\n");
scanf("%f",&current);
result = voltage / current;
printf("The resistance is %0.2f ohms.\n",result);
break;
default :
exit(0);
break;
}
return 0;
}

Print a double pyramid

void main(void)
{
clrscr();
int i,j,k,l,b,n;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=0;i
{
printf("");
for(l=0;l
printf(" ");
for(j=i+1;j<=n;j++)
printf("%d",j);
for(k=n-1;k>i;k--)
printf("%d",k);
}
b=n-1;
for(i=0;i
{
printf("");
for(l=n-2;l>i;l--)
printf(" ");
for(j=b;j<=n;j++)
printf("%d",j);
for(k=n-1;k>=b;k--)
printf("%d",k);
b--;
}
getch();
}
AddThis

Friday, March 5, 2010

C Program to calcuate interest and total amount at the end of each year

Write a

c program

calculate interest and total amount at da end of each year

Note: Output is not in the form of table and rate is taken as 2%. It calculates amount of each year



#include <>
#include <>
void main()
{
int t=1;
int r=2;
int y;
int y1=0;
long int p,a;
float i1;
double total;;
clrscr();
printf("enter starting amount&year");
scanf("%ld""%d",&p,&y);
while(y1<2009)
{
printf("enter current year");
scanf("%d",&y1);
printf("enter amount to be deposited");
scanf("%ld",&a);
i1=(p*r*t)/100;
total=i1+a+p;
printf("1%d",y);
printf("starting amount is %ld",p);
p=p+a;
printf("current year is %d",y1);
printf("interest is %f",i1);
printf("total amount is %lf",total);
}
getch();
}

REVERSE NUMBER

WAP to Reverse of any number using while loop
void main ()
{
int no,r,res;
clrscr ();
printf ("Enter any value: ");
scanf ("%d",&no);
r=res=0;
while (no>0)
{
r=no%10;
no=no/10;
res=(res*10)+r;
}
printf ("\nReverse is %d",res);
getch ();
}

Program for conversion of Decimal to Roman Number

#include

main()
{
int a,b,c,d,e;
clrscr();
printf("Input a number (between 1-3000):");
scanf("%d",&e);
while (e==0||e>3000)
{
printf ("ERROR: Invalid Input!");
printf ("Enter the number again:");
scanf ("%d",&e);
}
if (e>3000)
printf("Invalid");
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf("M");
else if (a ==2000)
printf("MM");
else if (a ==3000)
printf("MMM");

if (b == 100)
printf("C");
else if (b == 200)
printf("CC");
else if (b == 300)
printf("CCC");
else if (b == 400)
printf("CD");
else if (b ==500)
printf("D");
else if (b == 600)
printf("DC");
else if (b == 700)
printf("DCC");
else if (b ==800)
printf("DCCC");
else if (b == 900)
printf("CM");


if (c == 10)
printf("X");
else if (c == 20)
printf("XX");
else if (c == 30)
printf("XXX");
else if (c == 40)
printf("XL");
else if (c ==50)
printf("L");
else if (c == 60)
printf("LX");
else if (c == 70)
printf("LXX");
else if (c ==80)
printf("LXXX");
else if (c == 90)
printf("XC");

if (d == 1)
printf("I");
else if (d == 2)
printf("II");
else if (d == 3)
printf("III");
else if (d == 4)
printf("IV");
else if (d ==5)
printf("V");
else if (d == 6)
printf("VI");
else if (d == 7)
printf("VII");
else if (d ==8)
printf("VIII");
else if (d == 9)
printf("IX");
getch();
}

Matrix Multiplication

void main()
{
int row1=0,
col1=1,
row2=0,
col2=0,
**matrix1,
**matrix2,
**result;

clrscr();
printf(" Enter number of row for first matrix ");
scanf("%d",&row1);

while (col1!=row2)
{
printf(" Enter number of column for first matrix ");
scanf("%d",&col1);

printf(" Enter number of row for second matrix ");
scanf("%d",&row2);

if (col1!=row2)
{
clrscr();
printf("Column number of first matrix must be same as the row number of second matrix");
}


}


printf(" Enter number of column for second matrix ");
scanf("%d",&col2);

matrix1=init(matrix1,row1,col1);
matrix2=init(matrix2,row2,col2);
/* setting values in matrix */
printf("First matrix \n");
set(matrix1,row1,col1);
printf("Second matrix \n");
set(matrix2,row2,col2);
/* printint matrix */
clrscr();
printf(" [ First matrix ]\n");
get(matrix1,row1,col1);
printf(" [ Second matrix ]\n");
get(matrix2,row2,col2);

printf(" [ Multiplication Result ]\n");
result=mul(matrix1,matrix2,row1,col2,col1);
get(result,row1,col2);
printf("\n\t\t Thanks from debmalya jash");
getch();
free(matrix1);
free(matrix2);
fress(result);


} /* end main */


/* to initialize matrix */
int** init(int** arr,int row,int col)
{
int i=0,
j=0;

arr=(int**)malloc(sizeof(int)*row*col);

for(i=0;i
{
for(j=0;j
{
*((arr+i)+j)=(int*)malloc(sizeof(int));
*(*(arr+i)+j)=0;
}
}
return arr;
}

/* to set value in matrix */
int** set(int** arr,int row,int col)
{
int i=0,
j=0,
val=0;

for(i=0;i
{
for(j=0;j
{
printf("Enter value for row %d col %d :",(i+1),(j+1));
scanf("%d",&val);
*(*(arr+i)+j)=val;
}
}
return arr;
}


/* print values of the passed matrix */
void get(int** arr,int row,int col)
{
int i=0,
j=0;

for(i=0;i
{
for(j=0;j
{
printf("%d\t",*(*(arr+i)+j));
}
printf("\n");
}
}

/* mutiply two matrices and return the resultant matrix */
int** mul(int** arr1,int** arr2,int row,int col,int col1)
{
int **result,
i=0,
j=0,
k=0;

result=init(result,row,col);

for(i=0;i
{
for(j=0;j
{
for(k=0;k
{
printf("%dX%d(%d)",*(*(arr1+i)+k),*(*(arr2+k)+j),(*(*(arr1+i)+k))*(*(*(arr2+k)+j)));
*(*(result+i)+j)+=(*(*(arr1+i)+k))*(*(*(arr2+k)+j));

if (k!=(col1-1))
printf("+");
}
printf("\t");
}
printf("\n");
}
return result;

2d example insertion sort

#include stdio.h
#include stdlib.h

struct node
{
int number;
struct node *next;
};

struct node *head = NULL;

/* insert a node directly at the right place in the linked list */
void insert_node(int value);

int main(void)
{
struct node *current = NULL;
struct node *next = NULL;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i = 0;

/* insert some numbers into the linked list */
for(i = 0; i < 10; i++)
insert_node(test[i]);

/* print the list */
printf(" before after\n"), i = 0;
while(head->next != NULL)
{
printf("%4d\t%4d\n", test[i++], head->number);
head = head->next;
}

/* free the list */
for(current = head; current != NULL; current = next)
next = current->next, free(current);

return 0;
}

void insert_node(int value)
{
struct node *temp = NULL;
struct node *one = NULL;
struct node *two = NULL;

if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node *));
head->next = NULL;
}

one = head;
two = head->next;

temp = (struct node *)malloc(sizeof(struct node *));
temp->number = value;

while(two != NULL && temp->number < two->number) {
one = one->next;
two = two->next;
}

one->next = temp;
temp->next = two;
}

c programme

Write a

c program

calculate interest and total amount at da end of each year

Note: Output is not in the form of table and rate is taken as 2%. It calculates amount of each year



#include <>
#include <>
void main()
{
int t=1;
int r=2;
int y;
int y1=0;
long int p,a;
float i1;
double total;;
clrscr();
printf("enter starting amount&year");
scanf("%ld""%d",&p,&y);
while(y1<2009)
{
printf("enter current year");
scanf("%d",&y1);
printf("enter amount to be deposited");
scanf("%ld",&a);
i1=(p*r*t)/100;
total=i1+a+p;
printf("1%d",y);
printf("starting amount is %ld",p);
p=p+a;
printf("current year is %d",y1);
printf("interest is %f",i1);
printf("total amount is %lf",total);
}
getch();
}

c programme

#include
void main()
{
printf("welcome to world of c);
}