Saturday, July 31, 2010

Program for demonstration of Tree Operations - INSERTION, INORDER .

# include
# include
# include

struct node
{
struct node *left;
int data;
struct node *right;
};

void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();

printf("Enter the number of terms you want to add to the tree.");
scanf("%d",&will);

/* Getting Input */
for(i=0;i
{
printf("Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}

getch();
printf("INORDER TRAVERSAL");
inorder(ptr);
getch();
printf("PREORDER TRAVERSAL");
preorder(ptr);
getch();
printf("POSTORDER TRAVERSAL");
postorder(ptr);
getch();
}



void insert(struct node **p,int num)
{


if((*p)==NULL)
{ printf("Leaf node created.");
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{ if(num==(*p)->data)
{
printf("REPEATED ENTRY ERROR
VALUE REJECTED");
return;
}
if(num<(*p)->data)
{
printf("
Directed to left link.");
insert(&((*p)->left),num);
}
else
{
printf("Directed to right link.");
insert(&((*p)->right),num);
}
}
return;
}


void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("Data :%d",p->data);
inorder(p->right);
}
else
return;
}


void preorder(struct node *p)
{
if(p!=NULL)
{
printf("Data :%d",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}


void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("
Data :%d",p->data);
}
else
return;
}

Friday, July 30, 2010

Hsort, heap sort

/* array of MAXARRAY length ... */
#define MAXARRAY 5

/* preform the heapsort */
void heapsort(int ar[], int len);
/* help heapsort() to bubble down starting at pos[ition] */
void heapbubble(int pos, int ar[], int len);

int main(void) {
int array[MAXARRAY];
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 heapsort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

heapsort(array, MAXARRAY);

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

return 0;
}

void heapbubble(int pos, int array[], int len)
{
int z = 0;
int max = 0;
int tmp = 0;
int left = 0;
int right = 0;

z = pos;
for(;;) {
left = 2 * z + 1;
right = left + 1;

if(left >= len)
return;
else if(right >= len)
max = left;
else if(array[left] > array[right])
max = left;
else
max = right;

if(array[z] > array[max])
return;

tmp = array[z];
array[z] = array[max];
array[max] = tmp;
z = max;
}
}

void heapsort(int array[], int len)
{
int i = 0;
int tmp = 0;

for(i = len / 2; i >= 0; --i)
heapbubble(i, array, len);

for(i = len - 1; i > 0; i--)
{
tmp = array[0];
array[0] = array[i];
array[i] = tmp;
heapbubble(0, array, i);
}
}

Thursday, July 29, 2010

History Of C

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

Wednesday, July 28, 2010

Hsort, heap sort

/* array of MAXARRAY length ... */
#define MAXARRAY 5

/* preform the heapsort */
void heapsort(int ar[], int len);
/* help heapsort() to bubble down starting at pos[ition] */
void heapbubble(int pos, int ar[], int len);

int main(void) {
int array[MAXARRAY];
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 heapsort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

heapsort(array, MAXARRAY);

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

return 0;
}

void heapbubble(int pos, int array[], int len)
{
int z = 0;
int max = 0;
int tmp = 0;
int left = 0;
int right = 0;

z = pos;
for(;;) {
left = 2 * z + 1;
right = left + 1;

if(left >= len)
return;
else if(right >= len)
max = left;
else if(array[left] > array[right])
max = left;
else
max = right;

if(array[z] > array[max])
return;

tmp = array[z];
array[z] = array[max];
array[max] = tmp;
z = max;
}
}

void heapsort(int array[], int len)
{
int i = 0;
int tmp = 0;

for(i = len / 2; i >= 0; --i)
heapbubble(i, array, len);

for(i = len - 1; i > 0; i--)
{
tmp = array[0];
array[0] = array[i];
array[i] = tmp;
heapbubble(0, array, i);
}
}

Tuesday, July 27, 2010

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();
}

Monday, July 26, 2010

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

Sunday, July 25, 2010

Qserch , string, dynamic pointer array

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void sortstrarr(void *array, unsigned n);
static int cmpr(const void *a, const void *b);

int main (void)
{
char **strarray = NULL;
int i = 0, strcount = 0;
char line[1024];

while((fgets(line, 1024, stdin)) != NULL)
{
if(strlen(line) == 1)
continue;

strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *));
strarray[strcount++] = strdup(line);
}

printf("### Before ###\n");
for(i = 0; i < strcount; i++)
printf("%2d: %s", i, strarray[i]);

sortstrarr(strarray, strcount);

printf("### After ###\n");
for(i = 0; i < strcount; i++)
printf("%2d: %s", i, strarray[i]);

/* free mem... */
for(i = 0; i < strcount; i++)
free(strarray[i]);

free(strarray);
return 0;
}

static int cmpr(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}

void sortstrarr(void *array, unsigned n)
{
qsort(array, n, sizeof(char *), cmpr);
}

Saturday, July 24, 2010

count occurrences of values in an array

#include
void print_arr(int grades[], int elements);
int count_passes(int grades[], int elements,int value);

int main(void)
{
int grades[10] = {70,80,95,65,35,85,54,78,45,68};
int result;
print_arr(grades,10);
result = count_passes(grades,10,70);
if(result == 1)
printf("There was %d pass.\n",result);
else
printf("There were %d passes.\n",result);
return 0;
}

void print_arr(int grades[], int elements)
{
int i;

for(i = 0;i < elements;i++)
{
printf("%d ",grades[i]);
}
printf("\n");
}

int count_passes(int grades[], int elements,int value)
{
int i ,passes = 0 ;
for(i = 0;i < elements;i++)
{
if(grades[i] >= value)
passes++;
}
return(passes);
}

Friday, July 23, 2010

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

Thursday, July 22, 2010

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

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();
}

Wednesday, July 21, 2010

Bubble sort in string array

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define MAX 50
#define N 2000

void sort_words(char *x[], int y);
void swap(char **, char **);

int main(void)
{
char word[MAX];
char *x[N];
int n = 0;
int i = 0;

for(i = 0; scanf("%s", word) == 1; ++i)
{
if(i >= N)
printf("Limit reached: %d\n", N), exit(1);

x[i] = calloc(strlen(word)+1, sizeof(char));
strcpy(x[i], word);
}

n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);

return(0);
}

void sort_words(char *x[], int y)
{
int i = 0;
int j = 0;

for(i = 0; i < y; ++i)
for(j = i + 1; j < y; ++j)
if(strcmp(x[i], x[j]) > 0)
swap(&x[i], &x[j]);
}

void swap(char **p, char **q)
{
char *tmp;

tmp = *p;
*p = *q;
*q = tmp;
}

Tuesday, July 20, 2010

bubble sort

#include stdio.h>

void bubble_sort(int a[], int size);

int main(void) {
int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
int i = 0;

printf("before:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");

bubble_sort(arr, 10);

printf("after:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");

return 0;
}

void bubble_sort(int a[], int size)
{
int switched = 1;
int hold = 0;
int i = 0;
int j = 0;

size -= 1;

for(i = 0; i < size && switched; i++)
{
switched = 0;
for(j = 0; j < size - i; j++)
if(a[j] > a[j+1])
{
switched = 1;
hold = a[j];
a[j] = a[j + 1];
a[j + 1] = hold;
}
}
}

Monday, July 19, 2010

Bubble sort - linked list

#define MAX 10

struct lnode
{
int data;
struct lnode *next;
} *head, *visit;

/* add a new entry to the linked list */
void llist_add(struct lnode **q, int num);
/* preform a bubble sort on the linked list */
void llist_bubble_sort(void);
/* print the entire linked list */
void llist_print(void);

int main(void) {
/* linked list */
struct lnode *newnode = NULL;
int i = 0; /* a general counter */

/* load some random values into the linked list */
for(i = 0; i < MAX; i++)
{
llist_add(&newnode, (rand() % 100));
}

head = newnode;
printf("Before bubble sort:\n");
llist_print();
printf("After bubble sort:\n");
llist_bubble_sort();
llist_print();

return 0;
}

/* adds a node at the end of a linked list */
void llist_add(struct lnode **q, int num)
{
struct lnode *tmp;

tmp = *q;

/* if the list is empty, create first node */
if(*q == NULL) {
*q = malloc(sizeof(struct lnode));
tmp = *q;
} else {
/* go to last node */
while(tmp->next != NULL)
tmp = tmp->next;

/* add node at the end */
tmp->next = malloc(sizeof(struct lnode));
tmp = tmp->next;
}

/* assign data to the last node */
tmp->data = num;
tmp->next = NULL;
}

/* print the entire linked list */
void llist_print(void)
{
visit = head;

while(visit != NULL)
{
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}

/* preform a bubble sort on the linked list */
void llist_bubble_sort(void) {
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *e = NULL;
struct lnode *tmp = NULL;

/*
// the `c' node precedes the `a' and `e' node
// pointing up the node to which the comparisons
// are being made.
*/
while(e != head->next)
{
c = a = head;
b = a->next;
while(a != e)
{
if(a->data > b->data)
{
if(a == head)
{
tmp = b -> next;
b->next = a;
a->next = tmp;
head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else
{
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}

Sunday, July 18, 2010

Binary search

#define TRUE 0
#define FALSE 1

int main(void)
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int left = 0;
int right = 10;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;

printf("ARRAY: ");
for(i = 1; i <= 10; i++)
printf("[%d] ", i);
printf("\nSearch for Number: ");
scanf("%d", &number);

while(bsearch == FALSE && left <= right)
{
middle = (left + right) / 2;

if(number == array[middle])
{
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}

if(bsearch == FALSE)
printf("-- Number Not found --\n");

return 0;
}

AddThis

Saturday, July 17, 2010

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

Friday, July 16, 2010

Arrange the elements in array in Descending order

main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}

AddThis

Thursday, July 15, 2010

Area of Circle

WAP to find out areA of circle

#include

void main ()

{

float r,c;

clrscr();

printf ("Enter Radius: ");

scanf ("%f",&r);

c=3.14*r*r;

printf ("\nArea is : %.2f",c);

getch ();

}


Output

Wednesday, July 14, 2010

A simple example showing some comparison operators

#include
int main()
{
int number1 , number2;
printf("Enter the number1 number to compare.\n");
scanf("%d",&number1);
printf("Enter the number2 number to compare.\n");
scanf("%d",&number2);
printf("number1 > number2 has the value %d\n", number1 > number2);
printf("number1 < number2 has the value %d\n", number1 < number2);
printf("number1 == number2 has the value %d\n", number1 == number2);
return 0;
}

Tuesday, July 13, 2010

A Bubblesort Routine

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

Monday, July 12, 2010

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 < i =" 0;">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 <>number) {
one = one->next;
two = two->next;
}

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

Saturday, July 10, 2010

Generation of Fibonacci Sequence using Recursion.

Write a recursive function to obtain the first 25 numbers of a Fibonacci
sequence. In a Fibonacci sequence the sum of two successive terms gives the
third term. Following are the first few terms of the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 ...



#include
main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);


}



fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;


if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}

Friday, July 9, 2010

C Program to Calculate Factorial of a number using Recursion

Write a program to calculate the factorial of a number. Use the concept of recursion
instead of using loops.


#include

main()

{

int a, fact;



printf("\nEnter any number: ");

scanf ("%d", &a);



fact=rec (a);

printf("\nFactorial Value = %d", fact);



}


rec (int x)

{

int f;



if (x==1)

return (1);

else

f=x*rec(x-1);



return (f);

}

Thursday, July 8, 2010

Transforming Numerical Systems

Binary Numbers

Base system uses base B, and includes numbers 0, 1, 2, ... , B‑1

For example in decimal system B=10, and numbers included are 0, 1, 2, ..., 8 & 9.

If the base B=2, then the system is binary, and its numbers are 0 & 1.

From English term BInary digiT, comes the name for lowest amount of information BIT.

Example:

5710 = 5 * 101 + 7 * 100 = 1*25 + 1*24 + 1*23 + 0*22 + 0*21 + 1*20 = 1 1 1 0 0 1 2

Number 57 in decimal is shown by two numbers, while in binaries six figures are necessary to show data. Binaries compared to other numbering systems, logically use most amount of elements (data) to give us information. Number of BITS used to write down a number is limited due to technical reasons.

Machines used to process and save binary information, are usually constructed form electronic elements with 2 stable states (bistables), which are efficient and cheap to produce.

Switching from decimal to binary numbers:

Binary number is made by leftovers we get from dividing (reading upwards)

57 : 2 = 28 1 1 1 0 0 1

1

28 : 2 = 14

0

14 : 2 = 7

0

7 : 2 = 3

1

3 : 2 = 1

1

1 : 2 = 0

1



Negative Binary Numbers

Operation 7 – 5 , using computer with 4 bit length registry will be processed like

7 + (-5). Binary result of -5 can be achieved:

Positive Number 0 1 0 1

Complement till base -1 1 1 1 1

( one komplement) - 0 1 0 1

1 0 1 0


Complement till base 1 0 1 0

(dual complement) + 0 0 0 1

1 0 1 1



Proof that the result is - 5 Excluding operation 7 - 5

1 0 1 1 (-5)

+ 0 1 0 1 (+5)
---------------
0 0 0 0

extra 1



0 1 1 1 ( 7)

+ 1 0 1 1 (-5)
---------------
0 0 1 0

extra 1

If we have n=3 bit length registry, (first bit reserved for telling us if its –/+), next numbers can be shown:

Decades Binaries
0 000
1 001
2 010
3 011

-4 100
-3 101
-2 110
-1 111


For n = 3 we get interval [‑22, 22 ‑ 1], in general [‑2n‑1, 2n‑1 ‑ 1].

For n = 8 we get interval [‑27, 27‑1], exact range [‑128, 127].

Octal System

Base of the system is B=8 and numbers used are 0, 1, 2, 3, 4, 5, 6, 7.

This system is used for shorter description of binaries when its needed.

Example:

36‑bit number 001 110 000 101 111 001 010 011 111 000 100 001

Octal equivalent 1 6 0 5 7 1 2 3 7 0 4 1

Hexadecimal System

Base of the system is B=16 and numbers used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. This system is used for shorter description of binaries when it’s needed.

Example:

16‑bit number 0111 1011 0011 1110

Hexadecimal equivalent. 7 B 3 E


Rational numbers

Rational numbers have binary “spot” which is similar to decimal spot used by decimal numbers.

Example: conversion of rational number

5.75 10 = 5 * 100 + 7 * 10‑1 + 5 * 10‑2 =

= 1*22 + 0*21 + 1*20 + 1*2‑1 + 1*2‑2 = 1 0 1 . 1 1 2

Conversion of decimal number to binary number

1.25 = 1 + .25

.25 * 2 1 . 0 1

0.50

.5 * 2

1.0


Example: conversion of this decimal number - when conversed, it has infinite number of fractions in its binary state

13.3 = 13 + 0.3

.3 * 2 1 1 0 1 . 0 1 0 0 1 1 0 0 1 ...
0.6

.6 * 2
1.2

.2 * 2
0.4

.4 * 2
0.8

.8 * 2
1.6

.6 * 2
1.2

....


Notice that finite decimal number is shown as infinite periodic binary number.

Binary Number is mounted with exponent of Base 2, in a way that a binary dot is moved left or right, depending if exponent is positive or negative

Example: 1 . 1 1 * 22 = 1 1 1

Wednesday, July 7, 2010

Sum of First and Last Digits of a Four-Digit number.

This program is based on the "sum of digits" program discussed previously.
This program also has some usage of the Modulus Operator.


If a four-digit number is input through the keyboard,
write a program to obtain the sum of the first and the last digit of this number.

/*HINT: If a number is divided using % , then the number to the right side of the decimal point

is the result. (This applies only to integers.) */






#include

main ()

{

int number, last_digit, first_digit, total;

printf (" Enter the number which is to be operated on: ");

scanf ("%d", &number);



last_digit = number % 10;

total = last_digit;



first_digit = (number / 1000) % 10;

total = total + first_digit;



printf ("The total of the first and the last digit of the entered number is: %d", total);



}





/*This program is based on the "Sum of Digits" Program */

Tuesday, July 6, 2010

Sum of digits of a Five Digit Number.

This program is a bit of a tricky one. The Program clears our concepts about using the "Modulus Operator" . Once done, there is no stopping us then. The other programs follow the same path and almost have the same logic with some minor changes.

If a five-digit number is input through the keyboard, write a program to
calculate the sum of its digits.
(Hint: Use the Modulus Operator '%')


/*If a five-digit number is input through the keyboard, write a program to
calculate the sum of its digits.
(Hint: Use the Modulus Operator '%') */





/*Is 12345 / 100 % 10 not 3?
The divide by 100 strips the 45 and the remainder of 123 / 10 would be 3.

unit digit 5 would be 12345 % 10
tens digit would be (12345 / 10) % 10
hundreds digit would be (12345 / 100) % 10 ...
*/



/* Using / ..the normal division opeator returns the quotient.
Using % ..the modulus Operator returns the Remainder. */



#include
main ()
{
int number, last_digit, next_digit, total;

printf ("Enter the number whose sum of digits is to be calculated: ");
scanf ("%d", &number);

last_digit = number%10;
total = last_digit;

next_digit = (number/10) % 10;

total = total + next_digit;

next_digit = (number/100) % 10;

total = total + next_digit;

next_digit = (number/1000) %10;

total = total + next_digit;

next_digit = (number/10000) %10;

total = total + next_digit;


printf ("The sum of the digits of the entered number is: %d", total);

}

Monday, July 5, 2010

C Program to Convert Celcius to Fahrenheit and vice versa

This Program converts the Temperature from Fahrenheit to Celcius and vice versa.
Mind you, the formula for the conversion is just a multiplication by a factor one.

The conversion if non-linear and the logic is included in the program itself.

Temperature of a city in Fahreinheit degrees is input through the keyboard.
Write a program to convert this temerature into Centigrade degrees.



/*To convert Fahrenheit to Celsius, subtract 32 degrees and divide by 1.8.
To convert Celsius to Fahrenheit, multiply by 1.8 and add 32 degrees.*/



#include

main ()
{
float temp_c, temp_f;

printf ("Enter the value of Temperature in Celcius: ");

scanf ("%f", &temp_c);

temp_f = (1.8 * temp_c) + 32;

printf ("The value of Temperature in Fahreinheit is: %f", temp_f);

}

Sunday, July 4, 2010

Reversing a Five Digit Number (Integer)

This program reverses a Five Digit number. The number is entered through the keyboard and worked upon by the program.

If a five-digit number is input through the keyboard, write a program to reverse the number.

/*Use an integer variable to store the reverse (say rev_num).
take out the first (rightmost) digit of the number.
Give it to rev_num.
Take out the 2nd digit from right.
Now multiply rev_num by 10 and add this new digit to it.
Do this till the last digit.


*/





/*To understand the program, assume that the five digit number -- 12345 is entered
Calculate the values manually and check against the given number.

The final result (reversed number) is going to be 54321*/




#include
main()
{
int number, rev_num, next_digit,last_digit;

printf ("Enter the number that is to be reversed: ");
scanf("%d", &number);

last_digit = number - ((number / 10) * 10); /*units place*/

rev_num = last_digit; /* 5 */

next_digit = (number / 10) - ((number / 100) * 10); /*tenth's place*/

rev_num = (rev_num * 10) + next_digit; /*54*/

next_digit = (number / 100) - ((number / 1000) * 10); /*hundred's place*/

rev_num = (rev_num * 10) + next_digit; /*543*/

next_digit = (number / 1000) - ((number / 10000) * 10); /*thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*5432*/

next_digit = (number / 10000) - ((number / 100000) * 10); /*ten thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*54321*/

printf ("The Reversed Number is: %d",rev_num);
}

Saturday, July 3, 2010

Calculation of Simple Interest

This Program calculates the value of Simple Interest. The Values of Principle, Rate of Interest and the number of years is input through the keyboard.


Write a program to calculate the simple interest.
The values of principle, rate of interest and the number of years are input from the keyboard.
Display the value of the calculated Simple interest on a new line.



#include
main ()

{
int p, n;
float r, si;
printf(" Enter the values of p, n, r: ");

scanf("%d %d %f", &p, &n, &r);

si=p*n*r/100;

printf("%f", si);
}

Friday, July 2, 2010

Calculation of Total and Percentage

Calculates the total and percentage of the marks obtained in five subjects by a student. The marks obtained are to be entered by the student.


If the marks obtained by a student in five different subjects are input
through the keyboard, find out the aggregate marks and percentage marks obtained
by the student. Assume that the maximum marks that can be obtained by a student in each
subject is 100.


#include
main()
{

int m1, m2, m3, m4, m5, total;
float percentage;

printf ("Enter the marks obtained by the student in all the five subjects: ");
scanf ("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);

total= m1+m2+m3+m4+m5;
percentage = total/5;

printf ("\nThe aggregate marks obtained by the student are: %d", total);
printf ("\nThe percentage obtained by the student are %f", percentage);

}

Thursday, July 1, 2010

Interchanging Two Numbers

This program interchanges the two numbers inputted from the keyboard. Just yet another simple program...


Two numbers are input through the keyboard into two locations C and D.
Write a program to interchange the contents of C and D.


#include
main ()

{
int A,C,D;
printf ("Enter the value of C and D: ");

scanf ("%d %d", &C, &D);

A=C;

C=D;
D=A;

printf ("\nThe exchanged values of C and D are: %d and %d ", C, D);

}

Related Posts Plugin for WordPress, Blogger...