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

2D EXAMPLE INSERTION SORT

Saturday, July 4, 2009 0 comments

#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;
}

Read the full story

WAP for finding a square number

Thursday, July 2, 2009 0 comments

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,p;
clrscr();
printf("ENTER THE NUMBER: ");
scanf("%d",&n);
{
p=pow(n,2);
printf("SQUARE OF %d IS: %d",n,p);
}
getch();
}

Read the full story

C Introduction and Programs

 

C is sometimes referred to as a ``high-level assembly language.'' Some people think that's an insult, but it's actually a deliberate and significant aspect of the language. If you have programmed in assembly language, you'll probably find C very natural and comfortable (although if you continue to focus too heavily on machine-level details, you'll probably end up with unnecessarily nonportable programs). If you haven't programmed in assembly language, you may be frustrated by C's lack of certain higher-level features. In either case, you should understand why C was designed this way: so that seemingly-simple constructions expressed in C would not expand to arbitrarily expensive (in time or space) machine language constructions when compiled. If you write a C program simply and succinctly, it is likely to result in a succinct, efficient machine language executable. If you find that the executable program resulting from a C program is not efficient, it's probably because of something silly you did, not because of something the compiler did behind your back which you have no control over. In any case, there's no point in complaining about C's low-level flavor: C is what it is.

 

A programming language is a tool, and no tool can perform every task unaided. If you're building a house, and I'm teaching you how to use a hammer, and you ask how to assemble rafters and trusses into gables, that's a legitimate question, but the answer has fallen out of the realm of ``How do I use a hammer?'' and into ``How do I build a house?''. In the same way, we'll see that C does not have built-in features to perform every function that we might ever need to do while programming.

 

As mentioned above, C imposes relatively few built-in ways of doing things on the programmer. Some common tasks, such as manipulating strings, allocating memory, and doing input/output (I/O), are performed by calling on library functions. Other tasks which you might want to do, such as creating or listing directories, or interacting with a mouse, or displaying windows or other user-interface elements, or doing color graphics, are not defined by the C language at all. You can do these things from a C program, of course, but you will be calling on services which are peculiar to your programming environment (compiler, processor, and operating system) and which are not defined by the C standard. Since this course is about portable C programming, it will also be steering clear of facilities not provided in all C environments.

 

Another aspect of C that's worth mentioning here is that it is, to put it bluntly, a bit dangerous. C does not, in general, try hard to protect a programmer from mistakes. If you write a piece of code which will (through some oversight of yours) do something wildly different from what you intended it to do, up to and including deleting your data or trashing your disk, and if it is possible for the compiler to compile it, it generally will. You won't get warnings of the form ``Do you really mean to...?'' or ``Are you sure you really want to...?''. C is often compared to a sharp knife: it can do a surgically precise job on some exacting task you have in mind, but it can also do a surgically precise job of cutting off your finger. It's up to you to use it carefully.

Read the full story

WAP TO SUM OF FIVE ELEMENTS OF AN ARRAY


#include<stdio.h>
#include<conio.h>
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 the five Elements: %d",sum);
getch ();
}

OUTPUT
Enter Element: 12
Enter Element: 21
Enter Element: 34
Enter Element: 25
Enter Element: 50

Some of the five elements: 142

Read the full story

WAP CALCULATE THE POWER IN WATTS


#include <stdio.h>
#include <conio.h>
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;
}

Read the full story

QUICK SORT

#include "stdio.h"

#define MAXARRAY 10

void quicksort(int arr[], int low, int high);

int main(void)
{
int array[MAXARRAY] = {0};
int i = 0;

/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;

/* print the original array */
printf("Before quicksort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

quicksort(array, 0, (MAXARRAY - 1));

/* print the `quicksorted' array */
printf("After quicksort: ");
for(i = 0; i < MAXARRAY; i++) {
printf(" %d ", array[i]);
}
printf("\n");

return 0;
}

/* sort everything inbetween `low' <-> `high' */
void quicksort(int arr[], int low, int high)
{
int i = low;
int j = high;
int y = 0;
/* compare value */
int z = arr[(low + high) / 2];

/* partition */
do {
/* find member above ... */
while(arr[i] < z) i++;

/* find element below ... */
while(arr[j] > z) j--;

if(i <= j)
{
/* swap two elements */
y = arr[i];
arr[i] = arr[j];
arr[j] = y;
i++;
j--;
}
} while(i <= j);

/* recurse */
if(low < j)
quicksort(arr, low, j);

if(i < high)
quicksort(arr, i, high);
}

Read the full story

WAP TO CONVERT CELCIUS TO FAHRENHEIT

#include<stdio.h>
#include<conio.h>
void main ()
{
float c,f;
clrscr ();
printf ("Enter the value of celcius: ");
scanf ("%f",&c);
f=(float) 1.8*c+32;
printf ("\nFahrenheit is %.2f",f);
getch ();
}

Read the full story

C PROGRAM TO CALCUATE INTEREST AND TOTAL AMOUNT AT THE 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 <stdio.h>
#include <conio.h>
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();
}

Read the full story

REVERSING A LINKED LIST

 

#include<stdio.h>
#include<stdlib.h>

#define MAX 10 /* max of 10 elements */

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

/* add a lnode at the beginning of the list */
void llist_add_begin(struct lnode **n, int val);
/* reverse the whole list */
void llist_reverse(struct lnode **n);
/* display the whole linked list */
void llist_display(struct lnode *n);

int main(void)
{
struct lnode *new = NULL;
int i = 0;

/* insert some numbers */
for(i = 0; i <= MAX; i++)
llist_add_begin(&new, i);

printf("linked list before reversal:");
llist_display(new);
llist_reverse(&new);
printf("linked list after reversal:");
llist_display(new);

return 0;
}

/* add a lnode at the beginning of the list */
void llist_add_begin(struct lnode **n, int val)
{
struct lnode *temp = NULL;

/* add new node */
temp = malloc(sizeof(struct lnode));
temp->number = val;
temp->next = *n;
*n = temp;
}

/* reverse the whole list */
void llist_reverse(struct lnode **n)
{
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
a = *n, b = NULL;

while(a != NULL)
{
c = b, b = a, a = a->next;
b->next = c;
}

*n = b;
}

/* display the whole linked list */
void llist_display(struct lnode *n)
{
while(n != NULL)
printf(" %d", n->number), n = n->next;

printf("\n");
}

Read the full story

WAP TO COUNT NUMBER OF VOWELS

#include<stdio.h>
#include<conio.h>
void main ()
{
char s[20],vw=0,i;
clrscr();
printf ("Enter any string: ");
gets (s);
for (i=0;i<=strlen(s);i++)
{
switch (s[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vw++;
}
}
printf ("There are %d vowels in the string",vw);
getch ();
}

OUTPUT
Enter any string: SARTAJ HUSAIN
There are 4  vowels in the string

Read the full story

WAP TO FIND OUT TOTAL MARKS OF THREE SUBJECTS


#include<stdio.h>
#include<conio.h>
void main ()
{
int m1,m2,m3,tm;
clrscr ();
printf ("Enter M1:  ");
scanf ("%d",&m1);
printf ("Enter M2:  ");
scanf ("%d",&m2);
printf ("Enter M3:  ");
scanf ("%d",&m3);
tm=m1+m2+m3;
printf ("\nTotal Marks are:  %d",tm);
getch ();
}

OUTPUT
Enter M1:  70
Enter M1:  64
Enter M1:  75

Total Marks are: 209

Read the full story

WAP TO FIND OUT YEAR IS LEAP OR NOT

WAP TO FIND OUT YEAR IS LEAP OR NOT
#include<stdio.h>
#include<conio.h>

void main ()
{
int a;
clrscr ();
printf ("Enter the Year: ");
scanf("%d",&a);
if (a%4==0)
{
printf ("\nYear is Leap");
}
else
{
printf("\nYear is not Leap");
}
getch ();
}

OUTPUT
Enter the Year: 2004
Year is Leap

Read the full story

PRINT A DOUBLE PYRAMID

#include<stdio.h>
#include<conio.h>
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();
}

Read the full story

PROGRAM TO MULTIPLY OF TWO MATRICES

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("\nEnter the element of matrix a:\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the element of matrix b:\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
printf("\n");
}
printf("\nThe multiple matrix of a and b matrices is\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%d",c[i][j]);
}
printf("\n");
}
getch();
}

                                       OUTPUT
Enter the element of matrix a:
2 3 4
2 3 4
2 3 4
Enter the element of matrix b:
4 3 2
4 3 2
4 3 2
The multipal of a and b matrices is:
36 27 18
36 27 18
36 27 18

Read the full story

WAP Bubble sort

# include<stdio.d>
# include<conio.h>
void bubblesort(int array[],int size);
void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);
}
void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

Read the full story

PROGRAM FOR CONVERSION OF DECIMAL TO ROMAN NUMBER

#include<stdio.h>
#include<conio.h>
void 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();
}

Read the full story

WAP TO REVERSE OF ANY NUMBER USING WHILE LOOP

 

#include<stdio.h>
#include<conio.h>
void main ()
{
int no,a,res;
clrscr ();
printf ("Enter any value: ");
scanf ("%d",&no);
a=res=0;
while (no>0)
{
a=no%10;
no=no/10;
res=(res*10)+a;
}
printf ("\nReverse is %d",res);
getch ();
}

OUTPUT
Enter any value: 315
Reverse is 513

Read the full story

Program for finding the square root


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long float n,s;
clrscr();
printf("Enter a number : ");
scanf("%lf",&n);
s=sqrt(n);
printf("Square root is : %lf\n",s);
getch();
}

Read the full story

WAP TO FIND THAT NUMBER IS PRIME OR NOT


#include<stdio.h>
#include<conio.h>
void main ()
{
int no,i=2;
clrscr ();
printf ("Enter Number: ");
scanf ("%d",&no);
while (i<=no)
{
if (no%i==0)
break;
i++;
}
if (i==no)
printf ("Number is Prime");
else
printf ("Number is not Prime");
getch ();
}

OUTPUT
Enter Number: 11
Number is Prime

Enter Number: 6
Number is not  Prime

Read the full story

WAP TO SUM, SUBTRACT, MULTIPLY & DIVISION OF TWO NUMBERS (5 VARIABLES)

#include <stdio.h>
#include <conio.h>
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 ();
}

Read the full story