C Programming With Solutions
C Programming With Solutions
Output
None
3.ENUMERATORS:
void main()
{
enum Location {
LocUSA, LocUK, LocGermany, LocIndia, LocChina, LocJapan,
LocPhilippines, LocMalaysia, LocSingapore, LocThailand,
LocIndonesia, LocOthers
};
enum Random {
randData1 = -23, randData2, randData3, randData4 = 10, randData5
};
printf("USA: %d\n", LocUSA);
printf("India: %d\n", LocIndia);
printf("randData2: %d\n", randData2);
printf("randData5: %d\n",
randData5);
}
Output
USA: 0
India: 3
randData2: -22
randData5: 11
Press any key to continue . . .
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <ctype.h>
static double numbers[] =
{
10.11, 10.12, 10.13, 10.14
};
void main()
{
int numElements = sizeof(numbers) / sizeof(numbers[0]);
for(int i = 0; i <numElements; i++)
printf("%d\n", numbers[i]);
Output
10.11
10.12
10.13
10.14
<stdio.h>
<stdlib.h>
<conio.h>
<bios.h>
<ctype.h>
Output
USA
UK
India
Dubai
Singapore
Australia
Malaysia
Phillipines
Manila
Ethopia
Canada
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <ctype.h>
void GetCountryNames(char ***p, int *n)
{
static char *arrayCountryNames[] = {
"USA", "UK", "India", "Dubai", "Singapore", "Australia",
"Malaysia", "Phillipines", "Manila", "Ethopia", "Canada"
};
*p = arrayCountryNames;
*n = sizeof(arrayCountryNames) / sizeof(arrayCountryNames[0]);
};
void main()
{
int numElements = 0;
char **p = 0;
GetCountryNames(&p, &numElements);
Output
USA
UK
India
Dubai
Singapore
Australia
Malaysia
Phillipines
Manila
Ethopia
Canada
<stdio.h>
<stdlib.h>
<conio.h>
<bios.h>
<string.h>
void main()
{
const char *p1 = "victor";
char *p2 = "victor";
const char * const p3 = "victor";
char * const p4 = "victor";
p1 =
p2 =
//p3
//p4
0;
0;
= 0; // Compilation Error
= 0; // Compilation Error
// strcpy(p1, "victor");
strcpy(p2, "victor"); //
// strcpy(p3, "victor");
strcpy(p4, "victor"); //
//
//
//
//
p1
p2
p3
p4
// Compilation Error
Compilation Error
// Compilation Error
Compilation Error
Output
None
printf("Germany");
break;
}
case LocIndia:
{
printf("India");
break;
}
case LocChina:
{
printf("China");
break;
}
case LocPhilippines:
{
printf("Philippines");
break;
}
default:
{
printf("Others");
break;
}
};
Output
India
}
int main()
{
DisplayStatus();
EnterFunction();
DisplayStatus();
ExitFunction();
DisplayStatus();
}
return 0;
Output
Not initialized
Function Enters
Function Exits
printf(msg);
// Accessing the msg static variable in sub function in the current file
}
int main()
{
// Accessing the msg static variable in main function in the
current file
printf(msg);
DisplayWelcomeMessage();
}
Output
Welcome to my static function
Welcome to my static function
return msg;
int main()
{
// GetWelcomeMessage can only be accessed in the current .CPP file
printf(GetWelcomeMessage());
// It will display Welcome to my static function as output
}
Output
Welcome to my static function
return 0;
Output
10
20
30
40
50
60
70
80
90
100
10
20
30
40
50
60
70
80
90
100
Output
2. Fibonacci Series
Source Code
// Program for Fibonacci Number
#include <stdio.h>
void main()
{
int f1 = 0, f2 = 1, f3, n;
printf("Program for Fibonacci Series\n");
printf("Enter the maximum number for Fibonacci Series: ");
scanf("%d", &n);
printf("\nPrinting Fibonacci Series from 0 - %d\n", n);
printf("%d\n%d\n", f1, f2);
while(1)
{
f3 = f1 + f2;
if(f3 > n)
break;
printf("%d\n", f3);
f1 = f2;
f2 = f3;
}
}
Output
Program for Fibonacci Series
Enter the maximum number for Fibonacci Series:
Printing Fibonacci Series from 0 - 1000
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
Output
Using while loop
1
2
3
4
5
Using do while loop
1
2
3
4
5
Using for loop
1
2
3
4
5
Press any key to continue . . .
Output
#include <stdio.h>
int main()
{
printf("Program to find ODD or Even Number\n");
while(1)
{
int n = 0;
printf("\nEnter a number(-1 for Exit): ");
scanf("%d",&n);
if( n == -1)
break;
if((n % 2) == 0)
{
printf("%d is a EVEN number.\n", n);
}
else
{
printf("%d is a ODD number.\n", n);
}
}
return 0;
}
Output
Program to find ODD or Even Number
Enter a number(-1 for Exit): 1
1 is a ODD number.
Enter a number(-1 for Exit): 2
2 is a EVEN number.
Enter a number(-1 for Exit): 3
3 is a ODD number.
Enter a number(-1 for Exit): 4
4 is a EVEN number.
Enter a number(-1 for Exit): 5
5 is a ODD number.
Enter a number(-1 for Exit): 6
6 is a EVEN number.
Enter a number(-1 for Exit): 7
7 is a ODD number.
Enter a number(-1 for Exit): -1
return true;
}
Output
None
int main()
{
char src[] = "victor";
char dst[32];
Output
victor
rotciv
Source Code
#include <stdio.h>
int main()
{
int i,j,n;
printf("Enter a number: ");
scanf("%d", &n);
printf("\n");
for(i = n; i > 1; i--)
{
for(j = 1; j <= i; j++)
printf("*");
printf("\n");
}
for(i = 1; i <= n; i++)
{
for(j = 1; j <= i; j++)
printf("*");
printf("\n");
}
printf("\n");
for(i = 1; i < n; i++)
{
for(j = 1; j <= i; j++)
printf("%d",j);
printf("\n");
}
for(int i = n; i >= 0; i--)
{
for(int j = 1; j <= i; j++)
printf("%d",j);
printf("\n");
}
printf("\n");
return 0;
Output
Enter a number: 6
******
*****
****
***
**
*
**
***
****
*****
******
1
12
123
1234
12345
123456
12345
1234
123
12
1
10.
Source Code
#include <stdio.h>
#include <string.h>
bool HelperConvertNumberToText(int num, char *buf, int len)
{
static char *strones[] = {
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
};
static char *strtens[] = {
"Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
};
char result[1024];
int single, tens, hundreds;
if(num > 1000)
return false;
hundreds = num / 100;
num = num - hundreds * 100;
if( num < 20)
{
tens = 0; // special case
single = num;
}
else
{
tens = num / 10;
num = num - tens * 10;
single = num;
}
memset(result, 0, 1024);
if(hundreds > 0)
{
strcat(result,
strcat(result,
}
if(tens > 0)
{
strcat(result,
strcat(result,
}
if(single > 0)
{
strcat(result,
strcat(result,
}
strones[hundreds-1]);
" Hundred ");
strtens[tens-1]);
" ");
strones[single-1]);
" ");
return true;
}
if( num == 0)
{
printf(" %d \t Zero\n", num);
return false;
}
memset(result, 0, 1024);
if(num < 1000)
{
HelperConvertNumberToText(num, (char*) &tres, 1024);
strcat(result, tres);
}
else
{
thousands = num / 1000;
temp = num - thousands * 1000;
Output
-1
0
5
10
15
19
20
21
25
33
49
50
72
99
100
101
117
199
200
214
517
589
999
1000
1010
1018
1200
9890
10119
13535
57019
99999
100000
100001
11.
Not Supported
Zero
Five
Ten
Fifteen
Nineteen
Twenty
Twenty One
Twenty Five
Thirty Three
Fourty Nine
Fifty
Seventy Two
Ninety Nine
One Hundred
One Hundred One
One Hundred Seventeen
One Hundred Ninety Nine
Two Hundred
Two Hundred Fourteen
Five Hundred Seventeen
Five Hundred Eighty Nine
Nine Hundred Ninety Nine
One Thousand
One Thousand Ten
One Thousand Eighteen
One Thousand Two Hundred
Nine Thousand Eight Hundred Ninety
Ten Thousand One Hundred Nineteen
Thirteen Thousand Five Hundred Thirty Five
Fifty Seven Thousand Nineteen
Ninety Nine Thousand Nine Hundred Ninety Nine
One Hundred Thousand
Not Supported
Source Code
#include <stdio.h>
#include <string.h>
int StringToNumber(const char *buffer)
{
int result = 0;
int startIndex = 0;
int negativeNumber = 0;
int i, digit;
if(buffer[0] == '-')
{
negativeNumber = 1;
startIndex = 1;
}
for(i = startIndex; i < strlen(buffer); i++)
{
if(buffer[i] >= '0' && buffer[i] <= '9')
{
digit = buffer[i] - '0';
result = result * 10 + digit;
}
else
return 0;
}
if(negativeNumber == 1)
result *= -1;
return result;
}
int main()
{
char buffer[128];
int number = -1;
printf("String to Number Conversion Program\n");
while(1)
{
printf("Enter a String (-1 to exit): ");
scanf("%s", buffer);
number = StringToNumber(buffer);
printf("Converted Number: %d\n", number);
if(number == -1)
break;
}
return 0;
Output
2nd Iteration: 5 * 10 + 6 = 56
3rd Iteration: 56 * 10 + 5 = 564
4th Iteration: 564 * 10 + 7 = 5647
12.
Source Code
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
void main()
{
int i, j, k;
int n = 0;
printf("Program for displaying pattern of *.\n");
printf("Enter the maximum number of *: ");
scanf("%d", &n);
printf("\nHere is the Diamond of Stars\n");
for (i = 1; i <= n; i++)
{
for (j = 0; j < (n - i); j++)
printf(" ");
for (j = 1; j <= i; j++)
printf("*");
for (k = 1; k < i; k++)
printf("*");
printf("\n");
}
for (i = n - 1; i >= 1; i--)
{
for (j = 0; j < (n - i); j++)
printf(" ");
for (j = 1; j <= i; j++)
printf("*");
for (k = 1; k < i; k++)
printf("*");
printf("\n");
}
printf("\n");
}
Output
13Left
Source Code
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <string.h>
void main()
{
int i, j, k;
int n = 0;
printf("Program for displaying pattern of *.\n");
printf("Enter the maximum number of *: ");
scanf("%d", &n);
printf("\nPattern 1 - Left Aligned:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
printf("*");
printf("\n");
}
printf("\nPattern 2 - Right Aligned:\n");
for (i = n; i >= 1; i--)
{
for(j = 0; j < n - i; j++)
printf(" ");
Output
Program for displaying pattern of *.
Enter the maximum number of *: 9
Pattern 1 - Left Aligned:
*
**
***
****
*****
******
*******
********
*********
Pattern 2 - Right Aligned:
*********
********
*******
******
*****
****
***
**
*
Press any key to continue . . .
14.
Source Code
#include
#include
#include
#include
#include
<stdio.h>
<math.h>
<conio.h>
<process.h>
<string.h>
void main()
{
int i, n, diff;
int x = rand() % 100;
int bMoveHigher = 0;
int bGuessedCorrectly = 0;
printf("Welcome to Guess a Word Program\n");
for (i = 1; i <= 5; i++)
{
printf("ATTEMPT %d: Enter the your number: ", i);
scanf("%d", &n);
if (n == x)
{
printf("Congrats! You have guessed the number
correctly\n");
bGuessedCorrectly = 1;
break;
}
diff = (int)(fabs(x - n));
if(x > n)
bMoveHigher = 1;
if(diff >= 50)
{
if (bMoveHigher == 0)
printf("Your guess is VERY HIGH\n");
else
printf("Your guess is VERY LOW\n");
}
else if (diff >= 30)
{
if (bMoveHigher == 0)
printf("Your guess is HIGH\n");
else
printf("Your guess is LOW\n");
}
else if (diff >= 15)
{
if (bMoveHigher == 0)
printf("Your guess is MODERATELY HIGH\n");
else
printf("Your guess is MODERATELY LOW\n");
}
else
{
if (bMoveHigher == 0)
printf("Your guess is SOMEWHAT HIGH\n");
else
printf("Your guess is SOMEWHAT LOW\n");
}
}
if (bGuessedCorrectly == 0)
{
printf("Unforunatly you did not guess it correctly. The
correct number is: %d\n", x);
}
Output
Welcome to Guess a Word Program (0-100) with in 5 attempts.
ATTEMPT 1: Enter the your number: 50
Your guess is MODERATELY HIGH
ATTEMPT 2: Enter the your number: 28
Your guess is SOMEWHAT HIGH
ATTEMPT 3: Enter the your number: 24
Your guess is SOMEWHAT HIGH
ATTEMPT 4: Enter the your number: 22
Your guess is SOMEWHAT LOW
ATTEMPT 5: Enter the your number: 23
Congrats! You have guessed the number correctly
Press any key to continue . . .
15.
Source Code
void main()
{
int evencount = 0, i = 0;
for(i = 0; ; i++)
{
if(i >= 60)
break; // Terminate the for loop
if((i % 2) != 0)
continue; // Will skip the reminder of the for loop
evencount++;
}
printf(Total Even Numbers Between 0 60 is: %d, evencount);
}
Output
Total Even Numbers Between 0 60 is: 31
16.
Source Code
#include <stdio.h>
void main()
{
int opcode;
int a, b;
int result;
printf("Program for Addition, Subtraction, Multiplication and
Division\n");
printf("Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: ");
scanf("%d", &opcode);
printf("Enter First Number:");
scanf("%d", &a);
printf("Enter Second Number:");
scanf("%d", &b);
b);
switch(opcode)
{
case 1:
result = a
printf("%d
break;
case 2:
result = a
printf("%d
break;
case 3:
result = a
printf("%d
break;
case 4:
result = a
printf("%d
}
+ b;
+ %d = %d", a, b, result);
- b;
- %d = %d", a, b, result);
* b;
* %d = %d", a, b, result);
/ b;
/ %d = %d\n%d %% %d = %d", a, b, result, a, b, a %
break;
Output
Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 1
Enter First Number: 5
Enter Second Number: 3
5 + 3 = 8
Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 2
5 - 3 = 2
Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 3
Enter First Number: 5
Enter Second Number: 3
5 * 3 = 15
Program for Addition, Subtraction, Multiplication and Division
Enter Your Choice: 1 - Add, 2 - Sub, 3 - Mul, 4 - Div: 4
Enter First Number: 5
Enter Second Number: 3
5 / 3 = 1
5 % 3 = 2
17.
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <conio.h>
pos = 1;
}
else
{
printf(" ");
}
pos++;
}
}
int main(int argc, char *argv[])
{
char message[64];
if(argc < 2)
{
printf("Usage: scroll.exe <text>");
exit(0);
}
strcpy(message, (const char*) argv[1]);
scroll(message);
return 0;
}
Output
C:\TC\Samples> Scroll.exe "welcome to
www.google.com"
{
int index = 0;
while(1)
{
char ch;
if( feof(fp))
break;
if( fread(&ch, 1, 1, fp) < 1)
break;
if(ch == '\n')
{
data[index++] = '\0';
break;
}
data[index++] = ch;
}
return index;
}
int i, j;
int bGuessedCorrectly = 0;
printf("\nWelcome to Guess a Word\n");
count = ReadWordsFromFile(words);
if (count <= 0)
{
printf("\nNo words found in the file");
return;
}
guessX = rand() % count;
secretWord = words[guessX];
numChars = strlen(secretWord);
printf("Your secret word is: ");
for(i = 0; i < numChars; i++)
printf("*");
printf("\n");
printf("\nGuess now
while (1)
{
char choice[128];
scanf("%s", choice);
if (choice[0] == '#')
break;
if (strcmp(secretWord, choice) == 0)
{
bGuessedCorrectly = 1;
break;
}
for (i = 0; i < numChars; i++)
{
if (i < strlen(secretWord) &&
i < strlen(choice))
{
if (secretWord[i] == choice[i])
printf("%c", choice[i]);
else
printf("*");
}
else
printf("*");
}
printf("\n");
}
if (bGuessedCorrectly == 0)
printf("\nUnforunatly you did not guess it correctly. The
secret word is: %s", secretWord);
else
printf("\nCongrats! You have guessed it correctly");
for(i = 0; i < count; i++)
free(words[i]);
}
Output
// contents of words_input.txt
computer
science
school
schooling
information
technology
char name[64];
int marks[5];
int total;
} CStudent;
static int m_nMaxStudents;
static CStudent m_studList[100];
void AddRecord(const char *name, int *marks);
void AddRecord(const char *name, int *marks)
{
int i, pos = m_nMaxStudents;
strcpy(m_studList[pos].name,name);
m_studList[pos].total = 0;
for(i = 0; i < 5; i++)
{
m_studList[pos].marks[i] = marks[i];
m_studList[pos].total += marks[i];;
}
m_nMaxStudents++;
}
void ViewRecords()
{
int i = 0;
printf("_______________________________________________________________
\n");
printf("SNo Student Name
Sub1
Sub2
Sub3
Sub4
Sub5
Total\n");
printf("_______________________________________________________________
\n");
for(i = 0; i < m_nMaxStudents; i++)
{
printf("%3d %-19s %-6d %-6d %-6d %-6d %-6d %-6d\n",
i + 1,
m_studList[i].name,
m_studList[i].marks[0],
m_studList[i].marks[1],
m_studList[i].marks[2],
m_studList[i].marks[3],
m_studList[i].marks[4],
m_studList[i].total);
}
printf("_______________________________________________________________
\n");
}
void InputRecords()
{
char name[64];
int i, marks[5];
5; i++)
%d Mark:", i);
&marks[i-1]);
marks);
}
int main()
{
int i, numStudents = -1;
printf("Welcome to Student MarkList Application\n");
printf("Enter the number of Students: ");
scanf("%d", &numStudents);
Output
Welcome to Student MarkList Application
Enter the number of Students: 3
Sub1
Sub2
Sub3
Sub4
Sub5
Total
_______________________________________________________________
1 AAA
40
50
60
70
80
300
2 BBB
80
80
70
670
90
990
3 CCC
100
90
99
88
89
466
_______________________________________________________________
2.
Source Code
#include <stdio.h>
#include <string.h>
{
d1 = digit2;
if(digit1 > digit3)
{
d2 = digit1;
d3 = digit3;
}
else
{
d3 = digit1;
d2 = digit3;
}
}
else
{
d1 = digit3;
d2 = digit2;
d3 = digit1;
}
}
{
a1 = digit3;
a2 = digit1;
a3 = digit2;
}
}
else
{
if(digit2 < digit3)
{
a1 = digit2;
if(digit1 < digit3)
{
a2 = digit1;
a3 = digit3;
}
else
{
a3 = digit1;
a2 = digit3;
}
}
else
{
a1 = digit3;
a2 = digit2;
a3 = digit1;
}
}
digit1 == digit3)
{
printf("All digits should not be equal\n");
exit(0);
}
while(1)
{
nextnum = ComputeAndPrint(num);
if(num == nextnum)
break;
num = nextnum;
}
return 0;
}
Output
3digit.exe 812
821 - 128 = 693
963 - 369 = 594
954 - 459 = 495
954 - 459 = 495
3digit.exe 576
765 - 567 = 198
981 - 189 = 792
972 - 279 = 693
963 - 369 = 594
954 - 459 = 495
954 - 459 = 495
3digit.exe 417
741 - 147 = 594
954 - 459 = 495
954 - 459 = 495
3.
Pascal Triangle
Source Code
#include <stdio.h>
int main()
{
int n, x, y, c, q;
printf("Pascal Triangle Program\n");
printf("Enter the number of rows: ");
scanf("%d",&n);
for (y = 0; y < n; y++)
{
c = 1;
for(q = 0; q < n - y; q++)
{
printf("%3s", " ");
}
}
printf("\n");
return 0;
Output
1
1
7
8
3
4
10
21
15
28
10
20
35
56
1
5
15
35
70
6
21
56
1
7
28
1
8
1
1
9
10
36
45
84
120
126
210
126
252
84
210
36
120
9
45
1
10
if(islower(ch) != 0)
putch(toupper(ch));
else
putch(ch);
Output
ESC to break
WELCOME TO GOOGLE . COM
5.
Source Code
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
printf("Program for printing characters. Enter Esc Key to exit\n");
while(1)
{
ch = getch();
//printf("%d", ch);
if(ch == 27)
break;
else if(ch == 7)
printf("\n");
else if(ch == 9)
printf("\t");
else
putch(ch);
}
}
Output
Program for printing characters. Enter Esc Key to exit
This is for testing from
www.google.com
Source Code
#include <stdio.h>
void TestFunc()
{
static int IsFirstTime = 0;
if(IsFirstTime == 0)
{
printf("This function is called as a first time\n");
IsFirstTime++;
}
else
{
IsFirstTime++;
printf("This function has been called %d times so far\n",
IsFirstTime);
}
}
int main()
{
int i;
for(i = 0; i < 10; i++)
TestFunc();
return 0;
}
Output
This function is called as a first time
This function has been called 2 times so far
Source Code
// Program for Palindrome of a number
//
#include <stdio.h>
int main()
{
long n, number;
int digits[10], i, index;
int palindrome;
printf("\nProgram to check whether given number is palindrome or not.
Enter -1 to exit");
while(1)
{
printf("\nEnter a number:");
scanf("%ld", &n);
if(n < 0)
break;
number = n;
index = 0;
palindrome = 1;
do
{
digits[index++] = n % 10;
n = n / 10;
} while(n > 0);
for(i = 0; i < index / 2 + 1; i++)
{
if(digits[i] != digits[index - 1 - i])
{
palindrome = 0;
break;
}
}
if(palindrome == 1)
printf("The number %d is a palindrome\n", number);
else
printf("The number %d is NOT a palindrome\n", number);
}
}
return 0;
Output
Program to check whether given number is palindrome or not. Enter -1 to exit
Enter a number:121
The number 121 is a palindrome
Enter a number:565
The number 565 is a palindrome
Enter a number:98
The number 98 is NOT a palindrome
Enter a number:978
The number 978 is NOT a palindrome
Enter a number:-1
8.
Reverse Number
Source Code
#include <stdio.h>
#include <conio.h>
#include <math.h>
int CountNumberOfDigits_usingLog(int n)
{
return (int) (log10((double)n) + 1);
}
int CountNumberOfDigits(int n)
{
int numdgits = 0;
do
{
n = n / 10;
numdgits++;
} while(n > 0);
return numdgits;
}
void main()
{
int i, n, number, numdigits, result;
printf("\nProgram to find the reverse of a number. Enter -1 to
exit");
while(1)
{
Output
9.
Source Code
#include <stdio.h>
#include <conio.h>
#include <math.h>
int CountNumberOfDigits_usingLog(int n)
{
return (int) (log10((double)n) + 1);
}
int CountNumberOfDigits(int n)
{
int numdgits = 0;
do
{
n = n / 10;
numdgits++;
} while(n > 0);
return numdgits;
}
void main()
{
int i, n, number, numdigits, result;
printf("\nProgram to find the reverse of a number. Enter -1 to
exit");
while(1)
{
printf("\nEnter a number: ");
scanf("%d", &number);
if(number < 0)
break;
n = number;
numdigits = CountNumberOfDigits(number);
printf("\nNumber of digits in %d is: %d\n", number, numdigits);
result = 0;
for(i = 0; i < numdigits; i++)
{
result *= 10;
result += n % 10;
n = n / 10;
}
printf("The reverse of number %d is : %d\n", number, result);
}
Output
10.
Source Code
// WordCount.c : Defines the entry point for the console application.
#include <stdio.h>
bool GetWordCount(const char *srcFileName, int &numChars, int &numWords,
int &numLines)
{
const int len = 4096;
int nc, wc, lc;
int nBytesRead ;
char buf[4096];
FILE *istream = fopen(srcFileName, "rb");
if (istream == 0)
return false;
numChars = 0;
numWords = 0;
numLines = 0;
do
{
if( feof(istream))
break;
nBytesRead = fread(buf, 1, len, istream);
if(nBytesRead <= 0)
break;
GetWordCount(buf, nBytesRead, nc, wc, lc);
numChars += nc;
numWords += wc;
numLines += lc;
} while(1);
fclose(istream);
return true;
int main()
{
int nc, wc, lc;
GetWordCount("c:\\kathir.txt", nc, wc, lc);
printf("Chars: %d\n", nc);
printf("Words: %d\n", wc);
printf("Lines: %d\n", lc);
return 0;
}
Output
The file kathir.txt contains the following lines:
Welcome to softwareandfinance.com website.
Thank you very much for using our web site.
The output is given below:
Chars: 87
Words: 13
Lines: 2
Press any key to continue . . .
248);
Output
Enter a number to use in Fahrenheit and Celsius Converter: 100
100.0000 F = 37.7778 C
100.0000 C = 212.0000 F
Press any key to continue . . .
-2.2222 C
82.4000 F
<conio.h>
<process.h>
<dos.h>
<stdio.h>
int main()
248);
unsigned char ch ;
int x, y;
clrscr();
for(unsigned char ch = 0; ch < 255; ch++)
{
printf("%3d = %c\n", ch, ch);
}
printf("\n\n");
return 0;
Output
0 =
1 =
2 =
3 =
4 =
5 =
6 =
7 =
8 =
9 =
10 =
11 =
12 =
13 =
14 =
15 =
16 =
17 =
18 =
19 =
20 =
21 =
22 =
23 =
24 =
25 =
26 =
27 =
28 =
29 =
30 =
31 =
32 =
33 = !
34 = "
35 = #
36 = $
37 = %
38 = &
39 = '
40 = (
41 = )
42 = *
43 = +
44 = ,
45 = 46 = .
47 = /
48 = 0
49 = 1
50 = 2
51 = 3
52 = 4
53 = 5
54 = 6
55 = 7
56 = 8
57 = 9
58 = :
59 = ;
60 = <
61 = =
62 = >
63 = ?
64 = @
65 = A
66 = B
67 = C
68 = D
69 = E
70 = F
71 = G
72 = H
73 = I
74 = J
75 = K
76 = L
77 = M
78 = N
79 = O
80 = P
81 = Q
82 = R
83 = S
84 = T
85 = U
86 = V
87 = W
88 = X
89 = Y
90 = Z
91 = [
92 = \
93 = ]
94 = ^
95 = _
96 = `
97 = a
98 = b
99 = c
100 = d
101 = e
102 = f
103 = g
104 = h
105 = i
106 = j
107 = k
108 = l
109 = m
110 = n
111 = o
112 = p
113 = q
114 = r
115 = s
116 = t
117 = u
118 = v
119 = w
120 = x
121 = y
122 = z
123 = {
124 = |
125 = }
126 = ~
127 =
128 =
129 =
130 =
131 =
132 =
133 =
134 =
135 =
136 =
137 =
138 =
139 =
140 =
141 =
142 =
143 =
144 =
145 =
146 =
147 =
148 =
149 =
150 =
151 =
152 =
153 =
154 =
155 =
156 =
157 =
158 =
159 =
160 =
161 =
162 =
163 =
164 =
165 =
166 =
167 =
168 =
169 =
170 =
171 =
172 =
173 =
174 =
175 =
176 =
177 =
178 =
179 =
180 =
181 =
182 =
183 =
184 =
185 =
186 =
187 =
188 =
189 =
190 =
191 =
192 =
193 =
194 =
195 =
196 =
197 =
198 =
199 =
200 =
201 =
202 =
203 =
204 =
205 =
206 =
207 =
208 =
209 =
210 =
211 =
212 =
213 =
214 =
215 =
216 =
217 =
218 =
219 =
220 =
221 =
222 =
223 =
224 =
225 =
226 =
227 =
228 =
229 =
230 =
231 =
232 =
233 =
234 =
235 =
236 =
237 =
238 =
239 =
240 =
241 =
242 =
243 =
244 =
245 =
246 =
247 =
248 =
249 =
250 =
251 =
252 =
253 =
254 =
Press any key to continue . . .
Source Code
#include <stdio.h>
int main()
{
int sum = 0;
int i, a, b, c;
for(i = 100; i <= 200; i++)
{
a = i % 100;
b = a / 10;
c = a % 10;
if(b == 5 || c == 5)
printf("%d\n", i);
sum += i;
}
printf("\n\nSum = %d\n\n", sum);
return 0;
}
Output
105
115
125
135
145
150
151
152
153
154
155
156
157
158
159
165
175
185
195
Sum = 15150
Press any key to continue . . .
Output
Program for sum of all numbers from 0 - N
Enter N: 100
The sum of all numbers between 0 and 100 is: 5050
scanf("%d", &endno);
index = begno;
for(; index <= endno; index ++)
sum = sum + index;
printf("The sum of even numbers between %d and %d is: %d", begno,
endno, sum);
}
Output
Program for sum of all numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of even numbers between 1 and 100 is: 5050
Source Code
#include <stdio.h>
void main()
{
int index, begno, endno, sum = 0;
printf("Program for sum of even numbers in the given range\n");
printf("Enter Beg. No.: ");
scanf("%d", &begno);
printf("Enter End. No.: ");
scanf("%d", &endno);
index = begno;
if( (begno % 2) == 1) // If it ODD, then make it EVEN
index = begno + 1;
for(; index <= endno; index += 2)
{
sum = sum + index;
}
Output
Program for sum of even numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of even numbers between 1 and 100 is: 2550
Output
Program for sum of odd numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of odd numbers between 1 and 100 is: 2500
Output
Program for distance between the two points
Enter X1: 10
Enter Y1: 10
Enter X2: 30
Enter Y2: 30
Distance between (10, 10) and (30, 30) = SQRT(800) = 28.2843
intercept);
Output
Program to find the equation of a line given two end points
Enter X1: 2
Enter Y1: 3
Enter X2: 5
Enter Y2: 7
Equation of the line with end points (2, 3 and (5, 7) : Y = 1.33333X
+0.333333
Press any key to continue . . .
Output
Program to find the slope of a line given two end points
Enter X1: 2.5
Enter Y1: 7.5
Enter X2: 12.5
Enter Y2: 18
Slope of the line with end points (2.5, 7.5 and (12.5, 18) = 1.05
Press any key to continue . . .
Output
Program to find whether the given point lies on a line:
Enter Line1 - Slope: 1.25
Enter Line1 - Intercept: 0.75
Enter Point X: 2.33
Enter Point Y: 3.67
Equation of the line: 1.25X +0.75
Given point lies on the line
Press any key to continue . . .
right = x2;
}
else
{
left = x2;
right = x1;
}
if(y1 < y2)
{
top = y1;
bottom = y2;
}
else
{
top = y1;
bottom = y2;
}
Output
Program to find whether the given point lies with in line segment:
Enter X1: 1
Enter Y1: 2
Enter X2: 5
Enter Y2: 7
Enter Point X: 2.33
Enter Point Y: 3.67
Equation of the line: 1.25X +0.75
Given point lies in the line segment
Press any key to continue . . .
Program to find whether the given point lies with in line segment:
Enter X1: 1
Enter Y1: 2
Enter X2: 5
Enter Y2: 7
Enter Point X: 2.3
Enter Point Y: 3.6
Equation of the line: 1.25X +0.75
The point is outside the line segment
Press any key to continue . . .
c1);
c2);
Output
Program to find the intersecting point of two lines:
Enter Line1 - X1: 1
Enter Line1 - Y1: 2
Enter Line1 - X2: 5
Enter Line1 - Y2: 7
Enter Line2 - X1: 3
Enter Line2 - Y1: 3
Enter Line2 - X2: 4
Enter Line2 - Y2: 5
Equation of line1: Y = 1.25X + 0.75
Equation of line2: Y = 2.00X
-3.00
int IsPointInBoundingBox(float x1, float y1, float x2, float y2, float
px, float py)
{
float left, top, right, bottom; // Bounding Box For Line Segment
// For Bounding Box
if(x1 < x2)
{
left = x1;
right = x2;
}
else
{
left = x2;
right = x1;
}
if(y1 < y2)
{
top = y1;
bottom = y2;
}
else
{
top = y1;
bottom = y2;
}
if( (px+0.01) >= left && (px-0.01) <= right &&
(py+0.01) >= top && (py-0.01) <= bottom )
{
return 1;
}
else
return 0;
}
int LineIntersection(float l1x1, float l1y1, float l1x2, float l1y2,
float l2x1, float l2y1, float l2x2, float l
2y2,
float *m1, float *c1, float *m2, float *c2,
float* intersection_X, float*
intersection_Y)
{
float dx, dy;
dx = l1x2 - l1x1;
dy = l1y2 - l1y1;
*m1 = dy / dx;
// y = mx + c
// intercept c = y - mx
*c1 = l1y1 - *m1 * l1x1; // which is same as y2 - slope * x2
dx = l2x2 - l2x1;
dy = l2y2 - l2y1;
*m2 = dy / dx;
// y = mx + c
// intercept c = y - mx
*c2 = l2y1 - *m2 * l2x1; // which is same as y2 - slope * x2
scanf("%f", &l2y1);
printf("Enter Line2 - X2: ");
scanf("%f", &l2x2);
printf("Enter Line2 - Y2: ");
scanf("%f", &l2y2);
c1);
c2);
if(nRet == 0)
printf("The two line segments do not intersect each other");
else
printf("The two line segments intersect each other at %.2f,
%.2f", intersection_X, intersection_Y);
}
Output
Program to find the intersection point of two line segments:
Enter Line1 - X1: 1
Enter Line1 - Y1: 2
Enter Line1 - X2: 5
Enter Line1 - Y2: 7
Enter Line2 - X1: 3
Enter Line2 - Y1: 3
Enter Line2 - X2: 2.33
Enter Line2 - Y2: 3.66
Equation of line1: Y = 1.25X + 0.75
Equation of line2: Y = -0.99X + 5.96
void main()
{
int nCountIntersections = 0;
float x, y, cx, cy, radius;
float distance;
printf("Program to find the given point inside or outside the
circle:\n");
printf("Enter Center Point - X: ");
scanf("%f", &cx);
printf("Enter Center Point - Y: ");
scanf("%f", &cy);
printf("Enter Radius: ");
scanf("%f", &radius);
printf("Enter Point - X: ");
scanf("%f", &x);
printf("Enter Point - Y: ");
scanf("%f", &y);
distance = sqrt( (double)(cx-x)*(cx-x) + (cy-y)*(cy-y));
printf("\nDistance between the point and center of the circle:
%.4f", distance);
if(distance <= radius)
Output
Program to find the given point inside or outside the circle:
Enter Center Point - X: 10
Enter Center Point - Y: 10
Enter Radius: 7
Enter Point - X: 12
Enter Point - Y: 4
Distance between the point and center of the circle: 6.32456
Given point is inside the circle
int IsPointInBoundingBox(float x1, float y1, float x2, float y2, float
px, float py)
{
float left, top, right, bottom; // Bounding Box For Line Segment
// For Bounding Box
if(x1 < x2)
{
left = x1;
right = x2;
}
else
{
left = x2;
right = x1;
}
if(y1 < y2)
{
top = y1;
bottom = y2;
}
else
{
top = y2;
bottom = y1;
}
if( (px+0.01) >= left && (px-0.01) <= right &&
(py+0.01) >= top && (py-0.01) <= bottom )
{
return 1;
}
else
return 0;
}
*m1 = dy / dx;
// y = mx + c
// intercept c = y - mx
*c1 = l1y1 - *m1 * l1x1; // which is same as y2 - slope * x2
dx = l2x2 - l2x1;
dy = l2y2 - l2y1;
*m2 = dy / dx;
// y = mx + c
// intercept c = y - mx
*c2 = l2y1 - *m2 * l2x1; // which is same as y2 - slope * x2
if( (*m1 - *m2) == 0)
return 0;
else
{
*intersection_X = (*c2 - *c1) / (*m1 - *m2);
*intersection_Y = *m1 * *intersection_X + *c1;
}
if(IsPointInBoundingBox(l1x1, l1y1, l1x2, l1y2, *intersection_X,
*intersection_Y) == 1 && IsPointInBoundingBox(l2x1, l2y1, l2x2, l2y2,
*intersection_X, *intersection_Y) == 1)
{
return 1;
}
else
return 0;
}
int Calculate_Area_Perimeter_Triangle(float ax, float ay, float bx, flo
at by, float cx, floatcy, float *perimeter, float *area)
{
float A = sqrt((double)(bx-ax) * (bx-ax) + (by-ay) * (by-ay));
float B = sqrt((double)(bx-cx) * (bx-cx) + (by-cy) * (by-cy));
float C = sqrt((double)(ax-cx) * (ax-cx) + (ay-cy) * (ay-cy));
float height = 0;
void main()
{
float m1, c1, m2, c2;
float intersection_X, intersection_Y;
float ax, ay, bx, by, cx, cy;
float perimeter, area;
float angleA, angleB, angleC;
float x, y, px, py;
int type = 0;
float total = 0;
int nCountIntersections = 0;
printf("Program to find the given point inside or outside the
triangle:\n");
printf("Enter Triangle Point A - X: ");
scanf("%f", &ax);
printf("Enter Triangle Point A - Y: ");
scanf("%f", &ay);
printf("Enter Triangle Point B - X: ");
scanf("%f", &bx);
printf("Enter Triangle Point B - Y: ");
scanf("%f", &by);
printf("Enter Triangle Point C - X: ");
scanf("%f", &cx);
printf("Enter Triangle Point C - Y: ");
scanf("%f", &cy);
printf("Enter Point - X: ");
scanf("%f", &x);
printf("Enter Point - Y: ");
scanf("%f", &y);
px = x + 1000;
py = y;
%.4f", area);
Output
Program to find the given point inside or outside the triangle:
Enter Triangle Point A - X: 10
Enter Triangle Point A - Y: 10
Enter Triangle Point B - X: 20
Enter Triangle Point B - Y: 10
Enter Triangle Point C - X: 15
Enter Triangle Point C - Y: 15
Enter Point - X: 12
Enter Point - Y: 12
The Given Point is outside the triangle
Perimeter: 24.1421
Area: 25
Acute Triangle
Press any key to continue . . .
25.0000
Acute Triangle
Press any key to continue . . .
%.4f", area);
printf("\n");
}
Output
Program to find the given type of the triangle:
Enter Triangle Point A - X: 10
Enter Triangle Point A - Y: 10
Enter Triangle Point B - X: 20
Enter Triangle Point B - Y: 10
Enter Triangle Point C - X: 15
Enter Triangle Point C - Y: 15
Perimeter: 24.1421
Area:
25.0000
int IsPointInBoundingBox(float x1, float y1, float x2, float y2, float
px, float py)
{
float left, top, right, bottom; // Bounding Box For Line Segment
// For Bounding Box
if(x1 < x2)
{
left = x1;
right = x2;
}
else
{
left = x2;
right = x1;
}
if(y1 < y2)
{
top = y1;
bottom = y2;
}
else
{
top = y2;
bottom = y1;
}
if( (px+0.01) >= left && (px-0.01) <= right &&
(py+0.01) >= top && (py-0.01) <= bottom )
{
return 1;
}
else
return 0;
}
// intercept c = y - mx
*c2 = l2y1 - *m2 * l2x1; // which is same as y2 - slope * x2
if( (*m1 - *m2) == 0)
return 0;
else
{
*intersection_X = (*c2 - *c1) / (*m1 - *m2);
*intersection_Y = *m1 * *intersection_X + *c1;
}
if(IsPointInBoundingBox(l1x1, l1y1, l1x2, l1y2, *intersection_X,
*intersection_Y) == 1 && IsPointInBoundingBox(l2x1, l2y1, l2x2, l2y2,
*intersection_X, *intersection_Y) == 1)
{
return 1;
}
else
return 0;
}
int Calculate_Triangle_Type(float ax, float ay, float bx, float by, flo
at cx, float cy, float*angleA, float *angleB, float *angleC)
{
float m1, c1, m2, c2;
float intersection_X, intersection_Y;
// Find the angle between Line AB and BC
LineSegmentIntersection(ax, ay, bx, by, bx, by, cx, cy,
&m1, &c1, &m2, &c2, &intersection_X,
&intersection_Y);
*angleB = atan( (m1 - m2) / (1 + m1 * m2)) * 180 / 3.1417;
void main()
{
float m1, c1, m2, c2;
float ax, ay, bx, by, cx, cy;
float angleA, angleB, angleC;
int type = 0;
float total = 0;
printf("Program to find the type of a triangle:\n");
printf("Enter Triangle Point A - X: ");
scanf("%f", &ax);
printf("Enter Triangle Point A - Y: ");
scanf("%f", &ay);
printf("Enter Triangle Point B - X: ");
scanf("%f", &bx);
printf("Enter Triangle Point B - Y: ");
scanf("%f", &by);
printf("Enter Triangle Point C - X: ");
scanf("%f", &cx);
printf("Enter Triangle Point C - Y: ");
scanf("%f", &cy);
type = Calculate_Triangle_Type(ax, ay, bx, by, cx, cy, &angleA,
&angleB, &angleC);
total = angleA + angleB + angleC;
printf("Angle between the lines is %.4f, %.4f, %.4f\n", angleA,
angleB, angleC);
if(type == 1)
printf("\nAcute Triangle");
else if(type == 2)
printf("\nObtuse Triangle");
else if(type == 3)
printf("\nRight
Triangle");
printf("\n");
}
Output
Program to find the type of a triangle:
Enter Triangle Point A - X: 1
Enter Triangle Point A - Y: 1
Enter Triangle Point B - X: 20
Enter Triangle Point B - Y: 10
Enter Triangle Point C - X: 10
Enter Triangle Point C - Y: 20
Angle between the lines is 39.3063, 70.3438, 70.3438
Acute Triangle
Press any key to continue . . .
#include <string.h>
#include <math.h>
return 0;
Output
25 is a perfect square of 5
36 is a perfect square of 6
49 is a perfect square of 7
64 is a perfect square of 8
68 is NOT a perfect square
if(dispctr >= 6)
{
printf("\n");
dispctr = 0;
}
}
}
printf("\n");
}
int main()
{
GeneratePrimeNumbers(1000);
return 0;
}
Output
All the prime numbers under 1000 are given below:
2
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
long i = 0;
long idx = 0;
for(i = 2; i <= num; i++)
{
if(IsPrimeNumber(i) == true)
arr[count++] = i;
}
while(1)
{
if(IsPrimeNumber(num) == true)
{
arrResult[idx++] = num;
break;
}
for(i = count - 1; i >= 0; i--)
{
if( (num % arr[i]) == 0)
{
arrResult[idx++] = arr[i];
num = num / arr[i];
break;
}
}
}
return idx;
}
long main()
{
long num1;
long result[MAX_SIZE];
long count =
int i = 0;
0;
Output
Enter a Number: 9360
Prime Factors For 9360 = 13 *
5 *
3 *
3 *
2 *
2 *
2 *
7 *
5 *
5 *
5 *
19.
5 *
3 *
3 *
Calculate Mean
Source Code
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
2 *
2 *
2 *
int i;
float sum = 0;
for( i = 0; i < max; i++)
sum = sum + value[i];
return (sum / max);
int main()
{
float arrNumbers[100];
int i, max;
float mean;
char buf[1024];
printf("Total Number of Elements: ");
scanf("%d", &max);
for(i = 0; i < max; i++)
{
printf("Enter [%d] Number: ", i + 1);
scanf("%f", &arrNumbers[i]);
}
printf("Total Numbers: %d\n", max);
mean = CalculateMean(arrNumbers, max);
printf("Mean: %f", mean);
}
return 0;
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter [7] Number: 2
Enter [8] Number: 2
Enter [9] Number: 9
Enter [10] Number: 3
Total Numbers: 10
Mean: 5.500000
int main()
{
float arrNumbers[100];
int i, max;
float median;
char buf[1024];
printf("Total Number of Elements: ");
scanf("%d", &max);
for(i = 0; i < max; i++)
{
return 0;
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter [7] Number: 2
Enter [8] Number: 2
Enter [9] Number: 9
Enter [10] Number: 3
Total Numbers: 10
Median: 6.000000
}
sprintf(range, "%d - %d", small, big);
return range;
}
int main()
{
float arrNumbers[100];
int i, max;
const char *range;
char buf[1024];
printf("Total Number of Elements: ");
scanf("%d", &max);
for(i = 0; i < max; i++)
{
printf("Enter [%d] Number: ", i + 1);
scanf("%f", &arrNumbers[i]);
}
printf("Total Numbers: %d\n", max);
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter [7] Number: 2
Enter [8] Number: 2
Enter [9] Number: 9
Enter [10] Number: 3
Total Numbers: 10
Range: 2 - 9
int key;
int value;
} Pair;
typedef struct _Collection
{
Pair m_pair[100];
int m_maxElements;
} Collection;
const char* CalculateMode(float *arrValue, int max)
{
static char mode[256] = "";
char buf[32];
float value[100];
int i, j;
int highcount = 0;
float temp;
int nIndex = -1;
Collection map;
Pair ptemp;
memset(map.m_pair, 0, sizeof(Pair) * 100);
map.m_maxElements = 0;
if(max > 100)
return 0;
for(i = 0; i < max; i++)
value[i] = arrValue[i];
for(i = 0; i < max; i++)
{
for(j = 0; j < max - i - 1; j++)
{
if(value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
for(i = 0; i < max; i++)
{
// Insert arrValue[i] to the map
nIndex = -1;
for(j = 0; j < map.m_maxElements; j++)
{
if(map.m_pair[j].key == value[i])
{
nIndex = j;
break;
}
}
if(nIndex == -1)
{
map.m_pair[map.m_maxElements].key = value[i];
map.m_pair[map.m_maxElements].value = 1;
map.m_maxElements++;
}
else
{
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter [7] Number: 2
Enter [8] Number: 2
Enter [9] Number: 9
Enter [10] Number: 3
Total Numbers: 10
Mode: 2 6 7
Collection map;
Pair ptemp;
memset(map.m_pair, 0, sizeof(Pair) * 100);
map.m_maxElements = 0;
if(max > 100)
return 0;
for(i = 0; i < max; i++)
value[i] = arrValue[i];
for(i = 0; i < max; i++)
{
for(j = 0; j < max - i - 1; j++)
{
if(value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
for(i = 0; i < max; i++)
{
// Insert arrValue[i] to the map
nIndex = -1;
for(j = 0; j < map.m_maxElements; j++)
{
if(map.m_pair[j].key == value[i])
{
nIndex = j;
break;
}
}
if(nIndex == -1)
{
map.m_pair[map.m_maxElements].key = value[i];
map.m_pair[map.m_maxElements].value = 1;
map.m_maxElements++;
}
else
{
// map.m_pair[nIndex].key = value[i]; // alreday written
map.m_pair[nIndex].value++;
}
}
for(i = 0; i < map.m_maxElements; i++)
{
for(j = 0; j < map.m_maxElements - i - 1; j++)
{
if(map.m_pair[j].value < map.m_pair[j + 1].value)
{
ptemp = map.m_pair[j];
map.m_pair[j] = map.m_pair[j + 1];
}
}
map.m_pair[j + 1] = ptemp;
highcount = map.m_pair[0].value;
for(i = 0; i < map.m_maxElements; i++)
{
if(highcount == map.m_pair[i].value)
{
sprintf(buf, "%d ", map.m_pair[i].key);
strcat(mode, buf);
}
//std::cout << map.m_pair[i].key << " " << map.m_pair[i].value
<< "\n";
}
}
return mode;
int main()
{
float arrNumbers[100];
int i, max;
float mean;
const char *mode;
char buf[1024];
printf("Total Number of Elements: ");
scanf("%d", &max);
for(i = 0; i < max; i++)
{
printf("Enter [%d] Number: ", i + 1);
scanf("%f", &arrNumbers[i]);
}
printf("Total Numbers: %d\n", max);
mode = CalculateMode(arrNumbers, max);
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter
Enter
Enter
Enter
[7] Number: 2
[8] Number: 2
[9] Number: 9
[10] Number: 3
Total Numbers: 10
Mode: 2 6 7
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
for(i = 0; i < max; i++)
{
// Insert arrValue[i] to the map
nIndex = -1;
for(j = 0; j < map.m_maxElements; j++)
{
if(map.m_pair[j].key == value[i])
{
nIndex = j;
break;
}
}
if(nIndex == -1)
{
map.m_pair[map.m_maxElements].key = value[i];
map.m_pair[map.m_maxElements].value = 1;
map.m_maxElements++;
}
else
{
// map.m_pair[nIndex].key = value[i]; // alreday written
map.m_pair[nIndex].value++;
}
}
for(i = 0; i < map.m_maxElements; i++)
{
for(j = 0; j < map.m_maxElements - i - 1; j++)
{
if(map.m_pair[j].value < map.m_pair[j + 1].value)
{
ptemp = map.m_pair[j];
map.m_pair[j] = map.m_pair[j + 1];
map.m_pair[j + 1] = ptemp;
}
}
}
highcount = map.m_pair[0].value;
for(i = 0; i < map.m_maxElements; i++)
{
if(highcount == map.m_pair[i].value)
{
sprintf(buf, "%d ", map.m_pair[i].key);
strcat(mode, buf);
}
//std::cout << map.m_pair[i].key << " " << map.m_pair[i].value
<< "\n";
}
}
return mode;
return 0;
Output
Total Number of Elements: 10
Enter [1] Number: 6
Enter [2] Number: 7
Enter [3] Number: 8
Enter [4] Number: 7
Enter [5] Number: 6
Enter [6] Number: 5
Enter [7] Number: 2
Enter [8] Number: 2
Enter [9] Number: 9
Enter [10] Number: 3
Total Numbers: 10
Mean: 5.500000
Median: 6.000000
Mode: 2 6 7
Range: 2 - 9
Sample Variance: 6.055555
Population Variance: 5.450000
Sample Stdandrd Deviation: 2.460804
Population Standard Deviation: 2.334523
Output
Enter First Number: 10
Enter Second Number: 135
GCD(10,135) = 5
LCM(10,135) = 270
return sineresult;
20; term++)
/ fact;
sineresult - termfactor;
sineresult + termfactor;
int main()
{
double i = 0;
for(i = 0; i <= 360; i += 4.75)
{
printf("%.4lf = %.4lf\t%.4lf\n", i,
sin(i * PI / 180), MySin(i * PI / 180));
//printf("%.4lf\n", sin(i * PI / 180) - MySin(i * PI / 180));
}
return 0;
}
Output
0.0000 = 0.0000
4.7500 = 0.0828
9.5000 = 0.1650
14.2500 = 0.2462
19.0000 = 0.3256
23.7500 = 0.4027
28.5000 = 0.4772
33.2500 = 0.5483
38.0000 = 0.6157
42.7500 = 0.6788
47.5000 = 0.7373
52.2500 = 0.7907
57.0000 = 0.8387
61.7500 = 0.8809
66.5000 = 0.9171
71.2500 = 0.9469
76.0000 = 0.9703
80.7500 = 0.9870
85.5000 = 0.9969
90.2500 = 1.0000
95.0000 = 0.9962
99.7500 = 0.9856
104.5000 = 0.9681
109.2500 = 0.9441
114.0000 = 0.9135
118.7500 = 0.8767
123.5000 = 0.8339
128.2500 = 0.7853
133.0000 = 0.7314
137.7500 = 0.6724
142.5000 = 0.6088
147.2500 = 0.5410
152.0000 = 0.4695
156.7500 = 0.3947
161.5000 = 0.3173
166.2500 = 0.2377
171.0000 = 0.1564
175.7500 = 0.0741
180.5000 = -0.0087
185.2500 = -0.0915
190.0000 = -0.1736
194.7500 = -0.2546
199.5000 = -0.3338
204.2500 = -0.4107
0.0000
0.0828
0.1650
0.2462
0.3256
0.4027
0.4772
0.5483
0.6157
0.6788
0.7373
0.7907
0.8387
0.8809
0.9171
0.9469
0.9703
0.9870
0.9969
1.0000
0.9962
0.9856
0.9681
0.9441
0.9135
0.8767
0.8339
0.7853
0.7314
0.6724
0.6088
0.5410
0.4695
0.3947
0.3173
0.2377
0.1564
0.0741
-0.0087
-0.0915
-0.1736
-0.2546
-0.3338
-0.4107
209.0000 = -0.4848
-0.4848
213.7500 = -0.5556
-0.5556
218.5000 = -0.6225
-0.6225
223.2500 = -0.6852
-0.6852
228.0000 = -0.7431
-0.7431
232.7500 = -0.7960
-0.7960
237.5000 = -0.8434
-0.8434
242.2500 = -0.8850
-0.8850
247.0000 = -0.9205
-0.9205
251.7500 = -0.9497
-0.9497
256.5000 = -0.9724
-0.9724
261.2500 = -0.9884
-0.9884
266.0000 = -0.9976
-0.9976
270.7500 = -0.9999
-0.9999
275.5000 = -0.9954
-0.9954
280.2500 = -0.9840
-0.9840
285.0000 = -0.9659
-0.9659
289.7500 = -0.9412
-0.9412
294.5000 = -0.9100
-0.9100
299.2500 = -0.8725
-0.8725
304.0000 = -0.8290
-0.8290
308.7500 = -0.7799
-0.7799
313.5000 = -0.7254
-0.7254
318.2500 = -0.6659
-0.6659
323.0000 = -0.6018
-0.6018
327.7500 = -0.5336
-0.5336
332.5000 = -0.4617
-0.4617
337.2500 = -0.3867
-0.3867
342.0000 = -0.3090
-0.3090
346.7500 = -0.2292
-0.2292
351.5000 = -0.1478
-0.1478
356.2500 = -0.0654
-0.0654
Press any key to continue . . .
Source Code
#include <math.h>
#include <stdio.h>
const double PI = 3.14159265;
// cos series = 1 - x^2/2! + x^4/4! - x^6 / 6!
double MyCos(double x)
{
double sqx = x * x;
double cosineresult = 1;
double fact = 2;
int index = 2;
int term = 0;
double termfactor = 0;
for(term = 1; term < 20; term++)
{
termfactor = 0;
Output
0.0000
= 1.0000
1.0000
4.7500
= 0.9966
0.9966
9.5000
= 0.9863
0.9863
14.2500 = 0.9692
0.9692
19.0000 = 0.9455
0.9455
23.7500 = 0.9153
0.9153
28.5000 = 0.8788
0.8788
33.2500 = 0.8363
0.8363
38.0000 = 0.7880
0.7880
42.7500 = 0.7343
0.7343
47.5000 = 0.6756
0.6756
52.2500 = 0.6122
0.6122
57.0000 = 0.5446
0.5446
61.7500 = 0.4733
0.4733
66.5000 = 0.3987
0.3987
71.2500 = 0.3214
0.3214
76.0000 = 0.2419
0.2419
80.7500 = 0.1607
0.1607
85.5000 = 0.0785
0.0785
90.2500 = -0.0044
-0.0044
95.0000 = -0.0872
-0.0872
99.7500 = -0.1693
-0.1693
104.5000 = -0.2504
-0.2504
109.2500 = -0.3297
-0.3297
114.0000 = -0.4067
-0.4067
118.7500 = -0.4810
-0.4810
123.5000 = -0.5519
-0.5519
128.2500 = -0.6191
-0.6191
133.0000 = -0.6820
-0.6820
137.7500 = -0.7402
-0.7402
142.5000 = -0.7934
-0.7934
147.2500 = -0.8410
-0.8410
152.0000 = -0.8829
-0.8829
156.7500 = -0.9188
-0.9188
161.5000 = -0.9483
-0.9483
166.2500 = -0.9713
-0.9713
171.0000 = -0.9877
-0.9877
175.7500 = -0.9973
-0.9973
180.5000 = -1.0000
-1.0000
185.2500 = -0.9958
-0.9958
190.0000 = -0.9848
-0.9848
194.7500 = -0.9670
-0.9670
199.5000 = -0.9426
-0.9426
204.2500 = -0.9118
-0.9118
209.0000 = -0.8746
-0.8746
213.7500 = -0.8315
-0.8315
218.5000 = -0.7826
-0.7826
223.2500 = -0.7284
-0.7284
228.0000 = -0.6691
-0.6691
232.7500 = -0.6053
-0.6053
237.5000 = -0.5373
-0.5373
242.2500 = -0.4656
-0.4656
247.0000 = -0.3907
-0.3907
251.7500 = -0.3132
-0.3132
256.5000 = -0.2334
-0.2334
261.2500 = -0.1521
-0.1521
266.0000 = -0.0698
-0.0698
270.7500 = 0.0131
0.0131
275.5000 = 0.0958
0.0958
280.2500 = 0.1779
0.1779
285.0000 = 0.2588
0.2588
289.7500 = 0.3379
0.3379
294.5000 = 0.4147
0.4147
299.2500 = 0.4886
0.4886
304.0000 = 0.5592
0.5592
308.7500 = 0.6259
0.6259
313.5000 = 0.6884
0.6884
318.2500 = 0.7461
0.7461
323.0000 = 0.7986
0.7986
327.7500 = 0.8457
0.8457
332.5000 = 0.8870
0.8870
337.2500 = 0.9222
0.9222
342.0000 = 0.9511
0.9511
346.7500 = 0.9734
0.9734
351.5000 = 0.9890
0.9890
356.2500 = 0.9979
0.9979
Source Code
#include <math.h>
#include <stdio.h>
const double PI = 3.14159265;
// tan series = sine series / cosine series
double MyTan(double x)
{
double sineresult = x;
double cosineresult = 1;
double sqx = 0;
double fact = 0;
int index = 0;
int term = 0;
double termfactor = 0;
}
{
sqx = x * x * x;
fact = 2 * 3;
index = 3;
for(term = 1; term <
{
termfactor = 0;
termfactor = sqx
if(term % 2)
{
sineresult =
}
else
{
sineresult =
}
index++;
fact *= index;
index++;
fact *= index;
sqx *= (x*x);
}
/ fact;
sineresult - termfactor;
sineresult + termfactor;
sqx = x * x;
fact = 2;
index = 2;
for(term = 1; term <
{
termfactor = 0;
termfactor = sqx
if(term %2)
{
cosineresult
}
else
{
cosineresult
}
index++;
fact *= index;
index++;
fact *= index;
sqx *= (x*x);
}
20; term++)
20; term++)
/ fact;
= cosineresult - termfactor;
= cosineresult + termfactor;
}
return sineresult / cosineresult;
int main()
{
double i = 0;
for(i = 0; i <= 360; i += 4.75)
{
printf("%.4lf = %.4lf\t%.4lf\n", i,
Output
0.0000 = 0.0000 0.0000
4.7500 = 0.0831 0.0831
9.5000 = 0.1673 0.1673
14.2500 = 0.2540
19.0000 = 0.3443
23.7500 = 0.4400
28.5000 = 0.5430
33.2500 = 0.6556
38.0000 = 0.7813
42.7500 = 0.9244
47.5000 = 1.0913
52.2500 = 1.2915
57.0000 = 1.5399
61.7500 = 1.8611
66.5000 = 2.2998
71.2500 = 2.9459
76.0000 = 4.0108
80.7500 = 6.1402
85.5000 = 12.7062
90.2500 = -229.1818
95.0000 = -11.4301
99.7500 = -5.8197
104.5000 = -3.8667
109.2500 = -2.8636
114.0000 = -2.2460
118.7500 = -1.8228
123.5000 = -1.5108
128.2500 = -1.2685
133.0000 = -1.0724
137.7500 = -0.9083
142.5000 = -0.7673
147.2500 = -0.6432
152.0000 = -0.5317
156.7500 = -0.4296
161.5000 = -0.3346
166.2500 = -0.2447
171.0000 = -0.1584
175.7500 = -0.0743
180.5000 = 0.0087
185.2500 = 0.0919
0.2540
0.3443
0.4400
0.5430
0.6556
0.7813
0.9244
1.0913
1.2915
1.5399
1.8611
2.2998
2.9459
4.0108
6.1402
12.7062
-229.1818
-11.4301
-5.8197
-3.8667
-2.8636
-2.2460
-1.8228
-1.5108
-1.2685
-1.0724
-0.9083
-0.7673
-0.6432
-0.5317
-0.4296
-0.3346
-0.2447
-0.1584
-0.0743
0.0087
0.0919
190.0000 = 0.1763
0.1763
194.7500 = 0.2633
0.2633
199.5000 = 0.3541
0.3541
204.2500 = 0.4505
0.4505
209.0000 = 0.5543
0.5543
213.7500 = 0.6682
0.6682
218.5000 = 0.7954
0.7954
223.2500 = 0.9407
0.9407
228.0000 = 1.1106
1.1106
232.7500 = 1.3151
1.3151
237.5000 = 1.5697
1.5697
242.2500 = 1.9007
1.9007
247.0000 = 2.3559
2.3559
251.7500 = 3.0326
3.0326
256.5000 = 4.1653
4.1653
261.2500 = 6.4971
6.4971
266.0000 = 14.3007
14.3007
270.7500 = -76.3900
-76.3900
275.5000 = -10.3854
-10.3854
280.2500 = -5.5301
-5.5301
285.0000 = -3.7321
-3.7321
289.7500 = -2.7852
-2.7852
294.5000 = -2.1943
-2.1943
299.2500 = -1.7856
-1.7856
304.0000 = -1.4826
-1.4826
308.7500 = -1.2460
-1.2460
313.5000 = -1.0538
-1.0538
318.2500 = -0.8925
-0.8925
323.0000 = -0.7536
-0.7536
327.7500 = -0.6310
-0.6310
332.5000 = -0.5206
-0.5206
337.2500 = -0.4193
-0.4193
342.0000 = -0.3249
-0.3249
346.7500 = -0.2355
-0.2355
351.5000 = -0.1495
-0.1495
356.2500 = -0.0655
-0.0655
Press any key to continue . . .
Source Code
#include <math.h>
#include <stdio.h>
// quadratic equation is a second order of polynomial equation in a
single variable
// x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
void SolveQuadratic(double a, double b, double c)
{
double sqrtpart = b*b - 4*a*c;
double x, x1, x2, img;
if(sqrtpart > 0)
{
x1 = (-b + sqrt(sqrtpart)) / (2 * a);
x2 = (-b - sqrt(sqrtpart)) / (2 * a);
printf("Two Real Solutions: %.4lf or %.4lf\n\n", x1, x2);
}
else if(sqrtpart < 0)
{
sqrtpart = -sqrtpart;
x = -b / (2 * a);
img = sqrt(sqrtpart) / (2 * a);
printf("Two Imaginary Solutions: %.4lf + %.4lf i or %.4lf +
%.4lf i\n\n", x, img, x, img);
}
else
{
x = (-b + sqrt(sqrtpart)) / (2 * a);
printf("One Real Solution: %.4lf\n\n", x);
}
}
int main()
{
// 6x^2 + 11x - 35 = 0
SolveQuadratic(6, 11, -35);
// 5x^2 + 6x + 1 = 0
SolveQuadratic(5, 6, 1);
// 2x^2 + 4x + 2 = 0
SolveQuadratic(2, 4, 2);
// 5x^2 + 2x + 1 = 0
SolveQuadratic(5, 2, 1);
return 0;
}
Output
Two Real Solutions: 1.6667 or -3.5000
Source Code
#include <math.h>
#include <stdio.h>
const double PI = 3.14159265;
const double EulersNumber = 2.71828;
// exp(x) series = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4!
double MyExp1(double x)
{
double f = x;
double result = 1 + x;
double fact = 1;
int i = 0;
for(i = 2; i < 20; i++)
{
fact *= i;
f *= x;
result += f / fact;
}
return result;
}
// exp(x) series = power(2.71828, x)
double MyExp2(double x)
{
return pow(EulersNumber, x);
}
int main()
{
double i;
for( i = -2; i <= 3; i += 0.1)
}
return 0;
Output
-2.0000
-1.9000
-1.8000
-1.7000
-1.6000
-1.5000
-1.4000
-1.3000
-1.2000
-1.1000
-1.0000
-0.9000
-0.8000
-0.7000
-0.6000
-0.5000
-0.4000
-0.3000
-0.2000
-0.1000
0.0000
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
1.1000
1.2000
1.3000
1.4000
1.5000
1.6000
1.7000
1.8000
1.9000
2.0000
2.1000
2.2000
2.3000
2.4000
2.5000
2.6000
2.7000
2.8000
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
0.1353
0.1496
0.1653
0.1827
0.2019
0.2231
0.2466
0.2725
0.3012
0.3329
0.3679
0.4066
0.4493
0.4966
0.5488
0.6065
0.6703
0.7408
0.8187
0.9048
1.0000
1.1052
1.2214
1.3499
1.4918
1.6487
1.8221
2.0138
2.2255
2.4596
2.7183
3.0042
3.3201
3.6693
4.0552
4.4817
4.9530
5.4739
6.0496
6.6859
7.3891
8.1662
9.0250
9.9742
11.0232
12.1825
13.4637
14.8797
16.4446
0.1353 0.1353
0.1496 0.1496
0.1653 0.1653
0.1827 0.1827
0.2019 0.2019
0.2231 0.2231
0.2466 0.2466
0.2725 0.2725
0.3012 0.3012
0.3329 0.3329
0.3679 0.3679
0.4066 0.4066
0.4493 0.4493
0.4966 0.4966
0.5488 0.5488
0.6065 0.6065
0.6703 0.6703
0.7408 0.7408
0.8187 0.8187
0.9048 0.9048
1.0000 1.0000
1.1052 1.1052
1.2214 1.2214
1.3499 1.3499
1.4918 1.4918
1.6487 1.6487
1.8221 1.8221
2.0138 2.0138
2.2255 2.2255
2.4596 2.4596
2.7183 2.7183
3.0042 3.0042
3.3201 3.3201
3.6693 3.6693
4.0552 4.0552
4.4817 4.4817
4.9530 4.9530
5.4739 5.4739
6.0496 6.0496
6.6859 6.6859
7.3891 7.3890
8.1662 8.1662
9.0250 9.0250
9.9742 9.9742
11.0232 11.0232
12.1825 12.1825
13.4637 13.4637
14.8797 14.8797
16.4446 16.4446
30. Finding the Intersection of two Lines Given Slope and Intercept of Two Lines
Source Code
#include <stdio.h>
#include <math.h>
void main()
{
float m1, c1, m2, c2;
float dx, dy;
float intersection_X, intersection_Y;
printf("Program to find the intersecting point of two lines:\n");
printf("Enter Line1 - Slope: ");
scanf("%f", &m1);
printf("Enter Line1 - Intercept: ");
scanf("%f", &c1);
printf("Enter Line1 - Slope: ");
scanf("%f", &m2);
printf("Enter Line1 - Intercept: ");
scanf("%f", &c2);
printf("Equation of line1: Y = %.2fX %c %.2f\n", m1, (c1 < 0) ? '
' : '+',
c1);
c2);
Output
Program to find the intersecting point of two lines:
Enter Line1 - Slope: 1.25
Enter Line1 - Intercept: 0.75
Enter Line2 - Slope: 2
Enter Line2 - Intercept: -3
Equation of line1: 1.25X +0.75
Equation of line2: 2X
-3
1. Permutation Algorithm
Source Code
#include <stdio.h>
#include <string.h>
void sortchar(char *buffer, int len)
{
int i, j;
for(i = 1; i < len; i++)
{
for(j = 0; j < len - i; j++)
{
if(buffer[j] > buffer[j + 1])
{
char temp = buffer[j];
buffer[j] = buffer[j + 1];
buffer[j + 1] = temp;
}
}
}
}
int NextPermuation(char *p, int len)
{
int i,j,k;
int anchor, count;
char *tempptr;
ch = tempptr[i];
tempptr[i] = tempptr[i-1];
tempptr[i-1] = ch;
}
return 1;
}
}
return 0;
}
return 0;
Output
Enter a string to find permutation: 123
123
132
213
231
312
321
Count: 6
Press any key to continue . . .
Enter a string to find permutation: 4123
4123
4132
4213
4231
4312
4321
Count: 6
Press any key to continue . . .
NOTE: If you use the sortchar function after the user input, then
output would be changed to give always all possible combinations:
Enter a string to find permutation: 4123
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
Count: 24
Press any key to continue . . .
<stdio.h>
<alloc.h>
<stdlib.h>
<string.h>
int m_numElements;
} MyStack;
static
static
static
static
int
int
int
int
movecount = 0;
countA = 0;
countB = 0;
countC = 0;
static MyStack *A = 0;
static MyStack *B = 0;
static MyStack *C = 0;
int push(MyStack *s, int data)
{
if(s->m_data == NULL) // root node
{
s->m_numElements = 1;
s->m_data = (int*) malloc(sizeof(int));
}
else
{
s->m_numElements++;
s->m_data = realloc(s->m_data, s->m_numElements
* sizeof(int));
memmove(&s->m_data[1], s->m_data, (s->m_numElements - 1)
* sizeof(int));
}
s->m_data[0] = data;
return 1;
A", top(A));
A", top(A));
B", top(B));
B", top(B));
C", top(C));
else
PrintStack(A);
printf(" , ");
PrintStack(B);
printf(" , ");
PrintStack(C);
printf(" , ");
if (nDiscs == 1)
return 0;
Solve2DiscsTOH(source, dest, temp);
nDiscs = nDiscs - 1;
push(dest, pop(source));
movecount++;
PrintStacks();
Solve2DiscsTOH(temp, source, dest);
}
return 1;
}
else if (nDiscs >= 5)
{
SolveTOH(nDiscs - 2, source, temp, dest);
push(temp, pop(source));
movecount++;
PrintStacks();
SolveTOH(nDiscs - 2, dest, source, temp);
push(dest, pop(source));
movecount++;
PrintStacks();
SolveTOH(nDiscs - 1, temp, source, dest);
}
return 1;
}
int main()
{
int sz, i, maxdisc;
A = malloc(sizeof(MyStack));
B = malloc(sizeof(MyStack));
C = malloc(sizeof(MyStack));
while(1)
{
printf("\nEnter the number of discs (-1 to exit): ");
scanf("%d", &maxdisc);
if(maxdisc == -1)
break;
if(maxdisc < 2 || maxdisc > 9)
{
printf("Enter between 2 - 9");
continue;
}
movecount = 0;
memset((void*)A, 0, sizeof(MyStack));
memset((void*)B, 0, sizeof(MyStack));
memset((void*)C, 0, sizeof(MyStack));
for (i = maxdisc; i >= 1; i--)
push(A, i);
countA = A->m_numElements;
countB = B->m_numElements;
countC = C->m_numElements;
PrintStacks();
SolveTOH(maxdisc, A, B, C);
printf("Total Moves = %d", movecount);
free(C->m_data);
return 0;
}
Output
Enter the number of discs (-1 to exit): 0
Enter between 2 - 9
Enter the number of discs (-1 to exit): 1
Enter between 2 - 9
Enter the number
[21] , [] , [] ,
[2] , [1] , [] ,
[] , [1] , [2] ,
[] , [] , [21] ,
to exit):
From A To
From A To
From C To
From A To
From B To
From B To
From A To
= 7
3
C
B
B
C
A
C
C
(-1 to
Disc 1
Disc 2
Disc 1
Disc 3
Disc 1
Disc 2
Disc 1
Disc 4
Disc 1
Disc 2
Disc 1
Disc 3
Disc 1
Disc 2
Disc 1
Disc 5
Disc 1
Disc 2
Disc 1
Disc 3
exit):
From A
From A
From B
From A
From C
From C
From A
From A
From B
From B
From C
From B
From A
From A
From B
From A
From C
From C
From A
From C
6
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
C
B
B
C
A
C
C
B
B
A
A
B
C
B
B
C
A
C
C
A
B
A
A
C
C
B
B
C
A
C
C
B
C
C
B
A
B
B
C
C
A
A
C
B
C
C
B
A
B
B
A
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 4
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 3
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 6
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 3
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 4
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 3
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 5
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 3
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 4
Move Disc 1
Move Disc 2
Move Disc 1
Move Disc 3
Move Disc 1
Move Disc 2
Move Disc 1
Total Moves
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
From
= 63
B
B
C
C
A
A
B
A
C
C
A
A
B
B
C
B
A
A
B
B
C
C
A
C
B
B
C
B
A
A
B
A
C
C
A
A
B
B
C
B
A
A
B
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
To
C
A
A
B
B
C
C
B
A
B
B
C
C
A
A
C
B
C
C
A
A
B
B
A
C
A
A
C
B
C
C
B
A
B
B
C
C
A
A
C
B
C
C
<alloc.h>
<stdlib.h>
<string.h>
<stdio.h>
}
}
return 1;
}
int Sort(MyQueue *q)
{
int i, j, temp;
for(i = 1; i < q->m_numElements; i++)
{
for(j = 0; j < q->m_numElements - i; j++)
{
if(q->m_data[j] > q->m_data[j + 1])
{
temp = q->m_data[j];
q->m_data[j] = q->m_data[j + 1];
q->m_data[j + 1] = temp;
}
}
}
return 1;
}
void Clear(MyQueue *q)
{
free(q->m_data);
q->m_data = NULL;
q->m_numElements = 0;
}
int main()
{
MyQueue *q1 = malloc(sizeof(MyQueue));
int i, sz1;
char inpstring[32];
printf("Enter Queue : ");
scanf("%s", inpstring);
for( i = 0; i < strlen(inpstring); i++)
push(q1,inpstring[i] - '0');
sz1 = size(q1);
Sort(q1);
printf("Displaying Queue : %d elements\n", sz1);
for(i = 0; i < sz1; i++)
{
int m = front(q1);
printf("%d", m);
pop(q1);
}
printf("\n");
Clear(q1);
free(q1);
return 0;
Output
Enter Queue: 543126
Displaying Queue: 5 elements
123456
Press any key to continue . . .
<stdio.h>
<alloc.h>
<stdlib.h>
<string.h>
s->m_data = NULL;
}
else
{
* sizeof(int));
s->m_numElements--;
memmove(s->m_data, &s->m_data[1], s->m_numElements
10);
20);
30);
40);
40);
50);
sz = size(s1);
for(i = 0; i < sz; i++)
{
printf("%d\n", top(s1));
pop(s1);
}
return 0;
}
Output
50
40
30
20
10
Press any key to continue . . .
s->m_data = NULL;
return 0;
}
else
{
s->m_numElements--;
memmove(s->m_data, &s->m_data[1], s->m_numElements * sizeof(char));
s->m_data = (char*) realloc(s->m_data, s->m_numElements * sizeof(char));
}
}
return 1;
}
char top(MyStack *s)
{
if(s->m_numElements > 0)
return s->m_data[0];
return 0;
}
int size(MyStack *s)
{
return s->m_numElements;
}
int main(int argc, char *argv[])
{
MyStack *s1 = (MyStack *)malloc(sizeof(MyStack));
int sz, i, j, len, priority;
char infix[512], postfix[512];
char *p = infix;
int postfixlen = 0;
memset(postfix, 0, sizeof(postfix));
memset((void*)s1, 0, sizeof(MyStack));
if(argc != 2)
{
printf("Usage: InFixCnv.exe <infix expression>\n");
exit(0);
}
strcpy(infix, argv[1]);
while(1)
{
if(p == 0 || *p == '\0')
{
len = size(s1);
if(len <= 0)
break;
else for(j = 0; j < len; j++)
{
postfix[postfixlen++] = top(s1);
pop(s1);
}
}
if(*p == '+' || *p == '-' || *p == '*' || *p == '/')
{
// check the precedence
if(size(s1) <= 0)
push(s1, *p);
else
{
if( top(s1) == '*' || top(s1) == '/')
priority = 1;
else
priority = 0;
if(priority == 1)
{
if(*p == '+' || *p == '-')
{
postfix[postfixlen++] = top(s1);
pop(s1);
p--;
}
else
{
postfix[postfixlen++] = top(s1);
pop(s1);
p--;
}
}
else
{
if(*p == '+' || *p == '-')
{
postfix[postfixlen++] = top(s1);
pop(s1);
push(s1, *p);
}
else
push(s1, *p);
}
}
}
else
{
postfix[postfixlen++] = *p;
}
p++;
}
printf("InFix :\t%s\n", infix);
printf("PostFix:\t%s\n", postfix);
return 0;
}
Click here to download Turbo C Source Code and Executable
Output
InFix :
a+b*c
PostFix:
abc*+
InFix :
PostFix:
a+b*c/d-e
abc*d/+e-
InFix :
PostFix:
a+b*c/d-e+f*h/i+j-k
abc*d/+e-fh*i/+j+k-
char state[48];
char zipcode[12];
};
int ReadRecords(const char *fileName, TEmployee *e, int Sz)
{
int i = 0;
int nTotalRecordsRead = 0;
char buf[4096];
FILE *istream = fopen(fileName, "rb");
if (istream == 0)
return false;
for(i = 0; i < Sz; i++)
{
if(feof(istream))
break;
int nBytesRead = fread(buf, 1, sizeof(TEmployee), istream);
if(nBytesRead < sizeof(TEmployee))
break;
char *p = reinterpret_cast<char*>(&e[i]);
memcpy(p, buf, sizeof(TEmployee));
nTotalRecordsRead++;
}
fclose(istream);
return nTotalRecordsRead;
}
int WriteRecords(const char *fileName, TEmployee *e, int Sz)
{
int i = 0;
int nTotalRecordsWritten = 0;
char buf[4096];
FILE *ostream = fopen(fileName, "wb");
if (ostream == 0)
return false;
for(i = 0; i < Sz; i++)
{
fwrite((char*)&e[i], 1, sizeof(TEmployee), ostream);
nTotalRecordsWritten++;
}
fclose(ostream);
return true;
}
TEmployee wremplList[100];
TEmployee rdemplList[100];
void main()
{
int i, Sz;
for(i = 0; i < 10; i++)
{
strcpy(wremplList[i].name, "victor");
strcpy(wremplList[i].dept, "CS");
wremplList[i].age = 23;
}
WriteRecords("c:\\kathir\\2.bin", wremplList, 10);
Sz = ReadRecords("c:\\kathir\\2.bin", rdemplList, 100);
for(i = 0; i < Sz; i++)
{
std::cout << rdemplList[i].name << "\t";
std::cout << rdemplList[i].age << "\t";
std::cout << rdemplList[i].dept << "\n";
}
}
Output
victor
victor
victor
victor
victor
victor
victor
victor
victor
CS
CS
CS
CS
CS
CS
CS
CS
CS
23
23
23
23
23
23
23
23
23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int IsLeapYear(int year)
{
if((year % 4) == 0)
{
if((year % 100) == 0)
{
if( (year % 400) == 0)
return 1;
else
return 0;
}
else
return 1;
}
return 0;
}
int main()
{
static int arrnumdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int arrleapnumdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static char *strMonthName[] = {
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec",
};
int numdays;
int *pdaysinmonth = 0;
int year, firstdayofyear, firstdayofmonth, month, i, j;
::system("cls");
printf("Enter First Day of Year(1 - Sunday, 7 - Saturday): ");
scanf("%d", &firstdayofyear);
printf("Enter Year: ");
scanf("%d", &year);
if(firstdayofyear < 1 || firstdayofyear > 7)
{
printf("Invalid First Day Of Year. Enter 1 - Sunday, ..., 7 - Saturday.\n");
return 0;
}
pdaysinmonth = ( IsLeapYear(year)) ? arrleapnumdays : arrnumdays;
::system("cls");
printf("\n");
firstdayofmonth = 0;
for(month = 0; month < 12; month++)
{
printf("\n\n\n\t%s %d\n\n", strMonthName[month], year);
printf(" S M T W T F S\n");
numdays = pdaysinmonth[month];
if( month == 0)
firstdayofmonth = firstdayofyear;
else
{
firstdayofmonth = (firstdayofmonth + pdaysinmonth[month-1]) % 7;
if(firstdayofmonth == 0)
firstdayofmonth = 7; // 7 for saturday
}
for(j = 0; j < firstdayofmonth - 1; j++)
printf(" ");
for(i = 0; i < numdays; i++)
{
char buf[32];
sprintf(buf, " %d ", i + 1);
if(i < 9)
strcat(buf, " ");
printf(buf);
if( (i + firstdayofmonth) % 7 == 0)
printf("\n");
}
}
printf("\n\n");
return 0;
}
Click here to get the Turbo C Source code and executable
Output
Enter First Day of Year(1 - Sunday, 7 - Saturday): 3
Jan 2008
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Feb 2008
S M T W
1
3 4 5 6 7
10 11 12 13
17 18 19 20
24 25 26 27
T
2
8
14
21
28
F S
9
15 16
22 23
29
Mar 2008
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Apr 2008
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
May 2008
S M T W T
1 2 3
4 5 6 7 8 9
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29
F S
10
16 17
23 24
30 31
Jun 2008
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Jul 2008
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Aug 2008
S M T W
1
3 4 5 6 7
10 11 12 13
17 18 19 20
24 25 26 27
31
T
2
8
14
21
28
F S
9
15 16
22 23
29 30
Sep 2008
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Oct 2008
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Nov 2008
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Dec 2008
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Enter First Day of Year(1 - Sunday, 7 - Saturday): 6
Enter Year: 2010
Jan 2010
S M T W
1
3 4 5 6 7
10 11 12 13
T F S
2
8 9
14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Feb 2010
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28
Mar 2010
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Apr 2010
S M T W T
1 2 3
4 5 6 7 8 9
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29
F S
10
16 17
23 24
30
May 2010
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Jun 2010
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Jul 2010
S M T W T
1 2 3
4 5 6 7 8 9
11 12 13 14 15
18 19 20 21 22
25 26 27 28 29
F S
10
16 17
23 24
30 31
Aug 2010
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Sep 2010
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Oct 2010
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Nov 2010
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Dec 2010
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
{
if( (year % 400) == 0)
return true;
else
return false;
}
else
return true;
}
return false;
}
bool CheckDate(long date, long month, long year)
{
if(year < 0 || year > 10000)
return false;
if(month < 1 || month > 12)
return false;
if(date < 1 || date > 31)
return false;
if(IsLeapYear(year) == true)
{
if( date > arrleapnumdays[month-1])
return false;
}
else
if( date > arrnumdays[month-1])
return false;
return true;
}
long GetNumDays(long begdate, long begmonth, long begyear, long enddate, long
endmonth, long endyear)
{
long diffyears = endyear - begyear;
long numdays = 0;
long days = -1;
long m, diffmonth, d1, d2, y;
bool bLeap = false;
if(diffyears < 0)
return -1; // The start date is greater than end date
if( CheckDate(begdate, begmonth, begyear) == false)
return -2; // Not a valid start date
if(single > 0)
{
strcat(result, strones[single-1]);
strcat(result, " ");
}
if(len > strlen(result))
strcpy(buf, result);
return true;
}
bool ConvertNumberToText(int num, char *buf, int len)
{
char tres[1024];
char result[1024];
int thousands;
int temp;
if(num < 0 || num > 100000)
{
printf( " %d \t Not Supported\n", num);
return false;
}
if( num == 0)
{
printf(" %d \t Zero\n", num);
return false;
}
memset(result, 0, 1024);
if(num < 1000)
{
HelperConvertNumberToText(num, (char*) &tres, 1024);
strcat(result, tres);
}
else
{
thousands = num / 1000;
temp = num - thousands * 1000;
HelperConvertNumberToText(thousands, (char*) &tres, 1024);
strcat(result, tres);
strcat(result, "Thousand ");
HelperConvertNumberToText(temp, (char*) &tres, 1024);
strcat(result, tres);
}
if(len > strlen(result))
strcpy(buf, result);
return true;
}
int main()
{
int len = 1024;
char result[1024];
int i, num;
static int arrNum[] =
{
-1, 0, 5, 10, 15, 19, 20, 21, 25, 33, 49, 50, 72,
99, 100, 101, 117, 199, 200, 214, 517, 589, 999,
1000, 1010, 1018, 1200, 9890, 10119, 13535, 57019,
99999, 100000, 100001
};
for(i = 0; i < sizeof(arrNum) / sizeof(int); i++)
{
num = arrNum[i];
if( ConvertNumberToText(num, result, len) == true)
printf("%d \t %d\n", num, result);
}
return 0;
}
Output
-1
Not Supported
0
Zero
5
Five
10
Ten
15
Fifteen
19
Nineteen
20
Twenty
21
Twenty One
25
Twenty Five
33
Thirty Three
49
Fourty Nine
50
Fifty
72
Seventy Two
99
Ninety Nine
100
One Hundred
101
One Hundred One
117
One Hundred Seventeen
199
One Hundred Ninety Nine
200
Two Hundred
214
Two Hundred Fourteen
517
Five Hundred Seventeen
589
Five Hundred Eighty Nine
999
Nine Hundred Ninety Nine
1000 One Thousand
1010 One Thousand Ten
1018 One Thousand Eighteen
1200 One Thousand Two Hundred
9890 Nine Thousand Eight Hundred Ninety
10119 Ten Thousand One Hundred Nineteen
13535 Thirteen Thousand Five Hundred Thirty Five
57019 Fifty Seven Thousand Nineteen
99999 Ninety Nine Thousand Nine Hundred Ninety Nine
100000 One Hundred Thousand
100001 Not Supported
}
printf("Before Sorting : ");
for(k = 0; k < max; k++)
printf("%d ", numarray[k]);
printf("\n");
for(i = 1; i < max; i++)
{
for(j = 0; j < max - i; j++)
{
if(numarray[j] > numarray[j + 1])
{
temp = numarray[j];
numarray[j] = numarray[j + 1];
numarray[j + 1] = temp;
}
}
printf("After iteration %d": ", i);
for(k = 0; k < max; k++)
printf("%d ", numarray[k]);
printf("/*** %d biggest number(s) is(are) pushed to the end of the array ***/\n", i + 1);
}
printf("\n\nThe numbers in ascending orders are given below:\n\n");
for(int i = 0; i < max; i++)
{
printf("Sorted [%d] element: ",i + 1);
printf("%d\n", numarray[i]);
}
free(numarray);
return 0;
}
int main()
{
BubbleSort();
return 0;
}
Output
Program for Ascending order of Numeric Values using BUBBLE SORT
{
InsertionSort();
return 0;
}
Output
Program for Ascending order of Numeric Values using INSERTION SORT
Enter the total number of elements: 8
Enter [1] element: 80
Enter [2] element: 60
Enter [3] element: 40
Enter [4] element: 20
Enter [5] element: 10
Enter [6] element: 30
Enter [7] element: 50
Enter [8] element: 70
Before Sorting : 80 60 40 20 10 30 50 70
After iteration 1: 60 80 40 20 10 30 50 70
After iteration 2: 40 60 80 20 10 30 50 70
After iteration 3: 20 40 60 80 10 30 50 70
After iteration 4: 10 20 40 60 80 30 50 70
After iteration 5: 10 20 30 40 60 80 50 70
After iteration 6: 10 20 30 40 50 60 80 70
After iteration 7: 10 20 30 40 50 60 70 80
The numbers in ascending orders are given below:
Sorted [1] element: 10
Sorted [2] element: 20
Sorted [3] element: 30
Sorted [4] element: 40
Sorted [5] element: 50
Sorted [6] element: 60
Sorted [7] element: 70
Sorted [8] element: 80
int main()
{
for(long i = 0; i < 128; i++)
{
if(i > 16)
i += 7;
printf("\n%3ld = %08ld", i, ConvertDecimal2Binary(i));
}
printf("\n\n");
return 0;
}
Output
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
4 = 00000100
5 = 00000101
6 = 00000110
7 = 00000111
8 = 00001000
9 = 00001001
10 = 00001010
11 = 00001011
12 = 00001100
13 = 00001101
14 = 00001110
15 = 00001111
16 = 00010000
24 = 00011000
32 = 00100000
40 = 00101000
48 = 00110000
56 = 00111000
64 = 01000000
72 = 01001000
80 = 01010000
88 = 01011000
96 = 01100000
104 = 01101000
112 = 01110000
120 = 01111000
128 = 10000000
13. Binary to Decimal Conversion
Source Code
#include <stdio.h>
#include <conio.h>
#include <dos.h>
long ConvertDecimal2Binary(long dec)
{
long bin = 0, pos = 1;
while(dec > 0)
{
bin = bin + (dec % 2) * pos;
dec = dec / 2;
pos *= 10;
}
return bin;
}
long ConvertBinary2Decimal(long bin)
{
long dec = 0, pos = 0;
long factor = 1;
while(bin > 0)
{
if( (bin % 10) == 1)
{
dec += factor;
}
bin /= 10;
pos++;
factor = factor * 2;
}
return dec;
}
int main()
{
for(long i = 0; i < 128; i++)
{
if(i > 16)
i += 7;
long bin = ConvertDecimal2Binary(i);
long dec = ConvertBinary2Decimal(bin);
printf("\n%3ld = %08ld = %3ld", i, bin, dec);
}
printf("\n\n");
return 0;
}
Output
0 = 00000000 = 0
1 = 00000001 = 1
2 = 00000010 = 2
3 = 00000011 = 3
4 = 00000100 = 4
5 = 00000101 = 5
6 = 00000110 = 6
7 = 00000111 = 7
8 = 00001000 = 8
9 = 00001001 = 9
10 = 00001010 = 10
11 = 00001011 = 11
12 = 00001100 = 12
13 = 00001101 = 13
14 = 00001110 = 14
15 = 00001111 = 15
16 = 00010000 = 16
24 = 00011000 = 24
32 = 00100000 = 32
40 = 00101000 = 40
48 = 00110000 = 48
56 = 00111000 = 56
64 = 01000000 = 64
72 = 01001000 = 72
80 = 01010000 = 80
88 = 01011000 = 88
96 = 01100000 = 96
104 = 01101000 = 104
112 = 01110000 = 112
120 = 01111000 = 120
128 = 10000000 = 128
Press any key to continue . . .
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
tempresult *= tens;
}
break;
}
if(bOdd == 1)
{
numdigits -= 1;
bOdd = 0;
}
else
numdigits -= 2;
if( numdigits <= 0)
{
if(numeric > 0 || fraction > 0)
{
if(fractioncount >= 5)
break;
// Handle the fraction part for integer numbers
fractioncount++;
numeric *= 100;
if(fraction > 0)
{
// Handle the fraction part for real numbers
fnumdigits -= 2;
tens = 1;
for(k = 0; k < fnumdigits; k++)
tens *= 10;
numeric += fraction / tens;
fraction = fraction % tens;
}
numdigits += 2;
}
else
break;
}
}
if(fractioncount == 0)
result = tempresult;
else
{
tens = 1;
for(k = 0; k < fractioncount; k++)
tens *= 10;
#include <conio.h>
#include <bios.h>
#include <ctype.h>
#define F1_Key 0x3b00
#define F2_Key 0x3c00
#define F3_Key 0x3d00
#define F4_Key 0x3e00
#define F5_Key 0x3f00
#define F6_Key 0x4000
#define F7_Key 0x4100
#define F8_Key 0x4200
#define F9_Key 0x4300
#define F10_Key 0x4400
int handle_keyevents()
{
int key = bioskey(0);
if (isalnum(key & 0xFF))
{
printf("'%c' key pressed\n", key);
return 0;
}
switch(key)
{
case F1_Key:
printf("F1 Key Pressed");
break;
case F2_Key:
printf("F2 Key Pressed");
break;
case F3_Key:
printf("F3 Key Pressed");
break;
case F4_Key:
printf("F4 Key Pressed");
break;
case F5_Key:
printf("F5 Key Pressed");
break;
case F6_Key:
printf("F6 Key Pressed");
break;
case F7_Key:
printf("F7 Key Pressed");
break;
case F8_Key:
printf("F8 Key Pressed");
break;
case F9_Key:
printf("F9 Key Pressed");
break;
case F10_Key:
printf("F10 Key Pressed");
return -1;
default:
printf("%#02x\n", key);
break;
}
printf("\n");
return 0;
}
void main()
{
int key;
printf("Press F10 key to Quit\n");
while(1)
{
key = bioskey(1);
if(key > 0)
{
if(handle_keyevents() < 0)
break;
}
}
}
Click here to get the Turbo C Source Code and EXE
Output
Press F10 key to Quit
'a' key pressed
's' key pressed
'd' key pressed
'f' key pressed
'g' key pressed
'f' key pressed
'L' key pressed
int main()
{
int begyear, endyear;
int i;
printf("Enter Begining Year: ");
scanf("%d", &begyear);
printf("Enter Ending year: ");
scanf("%d", &endyear);
printf("List of Leap Years Between: %d and %d\n", begyear, endyear);
for(i = begyear; i < endyear; i++)
{
if( IsLeapYear(i) == true)
{
printf("%d\t", i);
}
}
return 0;
}
Output
Enter Beginning Year: 1000
Enter Ending year: 2100
List of Leap Years Between: 1000 and 2100
1004
1028
1052
1076
1104
1128
1152
1176
1200
1224
1248
1272
1296
1324
1348
1372
1396
1424
1448
1008
1032
1056
1080
1108
1132
1156
1180
1204
1228
1252
1276
1304
1328
1352
1376
1404
1428
1452
1012
1036
1060
1084
1112
1136
1160
1184
1208
1232
1256
1280
1308
1332
1356
1380
1408
1432
1456
1016
1040
1064
1088
1116
1140
1164
1188
1212
1236
1260
1284
1312
1336
1360
1384
1412
1436
1460
1020
1044
1068
1092
1120
1144
1168
1192
1216
1240
1264
1288
1316
1340
1364
1388
1416
1440
1464
1024
1048
1072
1096
1124
1148
1172
1196
1220
1244
1268
1292
1320
1344
1368
1392
1420
1444
1468
1472
1496
1524
1548
1572
1596
1620
1644
1668
1692
1720
1744
1768
1792
1820
1844
1868
1892
1920
1944
1968
1992
2016
2040
2064
2088
1476
1504
1528
1552
1576
1600
1624
1648
1672
1696
1724
1748
1772
1796
1824
1848
1872
1896
1924
1948
1972
1996
2020
2044
2068
2092
1480
1508
1532
1556
1580
1604
1628
1652
1676
1704
1728
1752
1776
1804
1828
1852
1876
1904
1928
1952
1976
2000
2024
2048
2072
2096
1484
1512
1536
1560
1584
1608
1632
1656
1680
1708
1732
1756
1780
1808
1832
1856
1880
1908
1932
1956
1980
2004
2028
2052
2076
1488
1516
1540
1564
1588
1612
1636
1660
1684
1712
1736
1760
1784
1812
1836
1860
1884
1912
1936
1960
1984
2008
2032
2056
2080
1492
1520
1544
1568
1592
1616
1640
1664
1688
1716
1740
1764
1788
1816
1840
1864
1888
1916
1940
1964
1988
2012
2036
2060
2084
{
if((year % 4) == 0)
{
if((year % 100) == 0)
{
if( (year % 400) == 0)
return true;
else
return false;
}
else
return true;
}
return false;
}
bool CheckDate(int date, int month, int year)
{
if(year < 0 || year > 10000)
return false;
if(month < 1 || month > 12)
return false;
if(date < 1 || date > 31)
return false;
if(IsLeapYear(year) == true)
{
if( date > arrleapnumdays[month-1])
return false;
}
else
if( date > arrnumdays[month-1])
return false;
return true;
}
int GetNumDays(int begdate, int begmonth, int begyear, int enddate, int endmonth, int
endyear)
{
int diffyears = endyear - begyear;
int numdays = 0;
int days = -1;
int m, y, d1, d2;
bool bLeap = false;
if(diffyears < 0)
{
// Beg Date to end of beg year (Dec 31, YYYY)
bLeap = IsLeapYear(begyear);
if(bLeap == true)
days = arrleapnumdays[begmonth - 1];
else
days = arrnumdays[begmonth - 1];
numdays += days - begdate;
for(d1 = begmonth + 1; d1 <= 12; d1++)
{
if(bLeap == true)
numdays += arrleapnumdays[d1 - 1];
else
numdays += arrnumdays[d1 - 1];
}
if(diffyears > 1)
{
for(y = begyear + 1; y <= endyear - 1; y++)
{
if(IsLeapYear(y) == true)
numdays += 366;
else
numdays += 365;
}
}
// Beg of End Year (Jan 01, YYYY) to End Date
bLeap = IsLeapYear(endyear);
for(d2 = 1; d2 <= endmonth - 1; d2++)
{
if(bLeap == true)
numdays += arrleapnumdays[d2 - 1];
else
numdays += arrnumdays[d2 - 1];
}
numdays += enddate;
}
return numdays;
}
int main()
{
int begdate, begmonth, begyear;
int enddate, endmonth, endyear;
char bd[128], ed[128];
int numdays;
printf("Enter Begin Date: ");
scanf("%d", &begdate);
printf("Enter Begin Month: ");
scanf("%d", &begmonth);
printf("Enter Begin Year: ");
scanf("%d", &begyear);
printf("Enter End Date: ");
scanf("%d", &enddate);
printf("Enter End Month: ");
scanf("%d", &endmonth);
printf("Enter End Year: ");
scanf("%d", &endyear);
sprintf(bd, "(DD/MM/YYYY) %02d/%02d/%04d", begdate, begmonth, begyear);
sprintf(ed, "%02d/%02d/%04d", enddate, endmonth, endyear);
numdays = GetNumDays(begdate, begmonth, begyear, enddate, endmonth, endyear);
if(numdays== -1)
printf("The start date is greater than end date\n");
else if(numdays == -2)
printf("Not a valid start date\n");
else if(numdays == -3)
printf("Not a valid end date\n");
else
printf("Number of days Between %s and %s is : %d\n", bd, ed, numdays);
return 0;
}
Output
Enter Begin Date: 6
Enter Begin Month: 2
Enter Begin Year: 1978
Enter End Date: 1
Enter End Month: 11
Enter End Year: 2010
Number of days Between (DD/MM/YYYY) 06/02/1978 and 01/11/2010 is : 11956
Press any key to continue . . .
Enter Begin Date: 31
grade = "B+";
else if(arrMark[i] > 50)
grade = "C+";
else if(arrMark[i] > 30)
grade = "C";
else
grade = "F";
printf("%d\t%d\t%s\n", i + 1, arrMark[i], grade);
}
return 0;
}
Output
Enter 1 Student Mark: 65
Enter 2 Student Mark: 76
Enter 3 Student Mark: 89
Enter 4 Student Mark: 95
Enter 5 Student Mark: 20
Enter 6 Student Mark: 45
Enter 7 Student Mark: 55
Enter 8 Student Mark: 67
Enter 9 Student Mark: 89
Enter 10 Student Mark: 29
No
Mark Grade
1
65
C+
2
76
B+
3
89
B+
4
95
A+
5
20
F
6
45
C
7
55
C+
8
67
C+
9
89
B+
10
29
F
Press any key to continue . . .
21. randomize, rand and srand functions for generating random numbers
22. Using kbhit, getch and putch functions
23. Using Circle and setcolor, setbkcolor functions
Source Code
#include <graphics.h>
#include <math.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
void main()
{
int i, grd, grm;
detectgraph(&grd,&grm);
initgraph(&grd, &grm, "");
while(!kbhit())
{
randomize();
setbkcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(1,1,638,478);
setcolor(WHITE);
rectangle(0,0,639,479);
for(i = 0; i < 300; i++)
{
setcolor(rand() % 10);
circle(rand() % 640, rand() % 480, rand() % 25);
delay(1);
}
}
getch();
closegraph();
}
Output
Source Code
#include <graphics.h>
#include <math.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
void main()
{
int i, grd, grm;
detectgraph(&grd,&grm);
initgraph(&grd, &grm, "");
while(!kbhit())
{
randomize();
setbkcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(1,1,638,478);
setcolor(WHITE);
rectangle(0,0,639,479);
for(i = 0; i < 300; i++)
{
setcolor(rand() % 10);
circle(rand() % 640, rand() % 480, rand() % 25);
delay(1);
}
}
getch();
closegraph();
}
Output
NOTE: The above programs are tested and given output programs. It is an internal
programs.
This are the Very very important programs in the interview point of view.