Posts

Showing posts with the label C Programming

C Program To Show Lenght Of A Word Using Function And Array

♣ Try it Out :: #include<stdio.h> #include<conio.h> int strLen(char *str) { int len=0; for(int i=0; str[i]!='\0'; i++) len++; return len; } int main() { char a[100]; printf("Please enter a word: "); scanf("%s", a); int L = strLen(a); printf("\nLenght = %d", L); getch(); return 0; } ♣ Output :: Please enter a word: Muhit Lenght = 5 ♣ Downloads :: Download source code Download executable file

Bresenham's Line Algorithm

Image
♣ The Algorithm :: Compute the initial values: dx = x 2 - x 1 Inc 2 = 2(dy - dx) dy = y 2 - y 1 d = Inc 1 - dx Inc 1 = 2dy Set (x,y) equal to the lower left-hand endpoint and x end equal to the largest value of x. If dx < 0, then x = x 2 , y = y 2 , x end = x 1 . If dx > 0, then x = x 1 , y = y 1 , x end = x 2 . Plot a point at the current (x,y) coordinates. Test to see whether the entire line has been drawn. If x = x end , stop. Compute the location of the next pixel. If d < 0, then d = d + Inc 1 . If d &gte; 0, then d = d + Inc 2 , and then y = y + 1. Increment x: x = x + 1. Plot a point at the current (x,y) coordinates. Go to step 4. ♣ C / C++ Code :: void BresenhamsLine(int x1, int y1, int x2, int y2) { int dx, dy, Inc1, Inc2, d, x, y, xend; dx = x2-x1; dy = y2 - y1; Inc1 = 2*dy; Inc2 = 2*(dy - dx); d = Inc1 - dx; if(dx < 0) { x=x2, y=y2, xend=x1; } else ...

Normal line algorithm

Image
♣ The Algorithm :: ♣ C / C++ Code :: void NormalLine(int x1, int y1, int x2, int y2) { int dx, dy, b, x, y, xend; float m; dx = x2 - x1; dy = y2 - y1; m = (float) dy / dx; b = y1 - m*x1; if(dx < 0) { x=x2, y=y2, xend=x1; } else if(dx > 0) { x=x1, y=y1, xend=x2; } while(x <= xend) { putpixel(x,y, LIGHTMAGENTA); x++; y = m*x + b; } } ♣ JAVA Code :: ♣ Source Codes :: Normal line algorithm.cpp Normal line algorithm.java আবদুল্লাহ বিন ওমার (রাঃ) হতে বর্ণিত। 'রসূলুল্লাহ (সঃ) বলেছেনঃ তোমাদের কেউ নামায পড়লে সে যেন তার দু'কাপড়ে নামায পড়ে। সৌন্দর্য প্রকাশের অগ্রাধিকার আল্লাহর জন্যই।' (তাহাওয়ী, বায়হাকী, তাবরানী)

Bresenham's circle algorithm

Image
♣ The Algorithm :: ♣ C / C++ Code :: void BresenhamsCircle(int h, int k, int r) { int x = 0, y = r; int d = 3 - 2*r; while(x <= y) { putpixel(x+h, y+k, MAGENTA); putpixel(y+h, x+k, MAGENTA); putpixel(-y+h, x+k, MAGENTA); putpixel(-x+h, y+k, MAGENTA); putpixel(-x+h, -y+k, MAGENTA); putpixel(-y+h, -x+k, MAGENTA); putpixel(y+h, -x+k, MAGENTA); putpixel(x+h, -y+k, MAGENTA); if(d < 0){ d += 4*x+6; x++; } else if(d >= 0) { d += 4*(x-y)+10; x++, y--; } } } ♣ JAVA Code :: ♣ Source Codes :: Bresenham's circle algorithm.cpp Bresenham's circle algorithm.java আবদুল্লাহ বিন ওমার (রাঃ) হতে বর্ণিত। 'রসূলুল্লাহ (সঃ) বলেছেনঃ তোমাদের কেউ নামায পড়লে সে যেন তার দু'কাপড়ে নামায পড়ে। সৌন্দর্য প্রকাশের অগ্রাধিকার আল্লাহর জন্যই।' (তাহাওয়ী, বায়হাকী, তাবরানী)

Insertion Sort Algorithm

♣ PSEUDOCODE :: INSERTION-SORT(A) for j = 1 to A.length - 1 key = A[j] // Insert A[j] into the sorted sequence A[0 ... j-1]. i = j - 1 while i >= 0 and A[i] > key A[i + 1] = A[i] i = i - 1 A[i + 1] = key ♣ C / C++ Code :: void InsertionSort(int A[], int len) { int i, j, key; for(j = 1; j <= len-1; j++) { key = A[j]; i = j - 1; while(i >= 0 && A[i] > key) { A[i+1] = A[i]; i--; } A[i+1] = key; } } ♣ JAVA Code :: static void InsertionSort(int A[]) { int i, j, key; for (j = 1; j <= A.length - 1; j++) { key = A[j]; i = j - 1; while (i >= 0 && A[i] > key) { A[i + 1] = A[i]; i--; } A[i + 1] = key; } } ♣ PHP Code :: function InsertionSort($A, $len) { for($j=1; $j<=$len-1; $j++): $key = $A[$j]; ...

C Program To Turn Off Or Shutdown Your Computer

এই C প্রোগ্রামটি চালু করলে প্রথমে, এটি আপনাকে জিজ্ঞেস করবে আপনি কি আপনার কম্পিউটার বন্ধ করতে চান? । যদি 'Y' ইনপুট করেন তাহলে, আপনার কম্পিউটারটি ৩০ সেকেন্ডের মধ্যে বন্ধ হয়ে যাবে। এখানে stdlib.h সিস্টেম ফাইলটি ব্যাবহার করা হয়েছে executable ফাইল shutdown.exe চালু করার জন্য। যা Windows-XP অপারেটিং সিস্টেমে C:\WINDOWS\system32 এই লকেশনে থাকে। আপনি যদি চান, তাহলে shutdown.exe ফাইলটি execute করার সময় বিভিন্ন অপশন ব্যবহার করতে পারেন যেমন, -s অপশনটি ৩০ সেকেন্ড পর কম্পিউটারটি বন্ধ করে। আপনি ইচ্ছে করলে সিস্টেম ফাংশনের shutdown -s আর্গুমেন্টটি shutdown -s -t 0 দ্বারা পরিবর্তীত করে ০ সেকেন্ডের মধ্যে কম্পিউটারটি বন্ধ করতে পারবেন। আর যদি কম্পিউটারটি Restart করতে চান, তাহলে আর্গুমেন্ট হিসেবে টাইপ করুন shutdown -r । ♣ Try it Out :: #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { char ch; printf("Do you want to shutdown your computer now (y/n) "); scanf("%c",&ch); if( ch == 'y' || ch == 'Y' )...

C Program To Implement Bubble Sort Algorithm To Print N Numbers In Ascending Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> void BubbleSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); BubbleSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void BubbleSort(int Arr[], int Size) { int Swap, i, j; for(i=0; i<Size-1; i++) for(j=Size-1; j>=i+1; j--) if(Arr[j-1] > Arr[j]) { Swap = Arr[j-1]; Arr[j-1] = Arr[j]; Arr[j] = Swap; } } ♣ Output :: How many numbers you wish to enter: 5 Enter 5 integers: 8 0 1 9 7 Sorted l...

C Program To Implement Bubble Sort Algorithm To Print N Numbers In Descending Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> void BubbleSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); BubbleSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void BubbleSort(int Arr[], int Size) { int Swap, i, j; for(i=0; i<Size-1; i++) for(j=Size-1; j>=i+1; j--) if(Arr[j-1] < Arr[j]) { Swap = Arr[j-1]; Arr[j-1] = Arr[j]; Arr[j] = Swap; } } ♣ Output :: How many numbers you wish to enter: 5 Enter 5 integers: 4 9 7 0 1 Sorted l...

C Program To Implement Insertion Sort Algorithm To Print N Numbers In Ascending Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> void InsertionSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); InsertionSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void InsertionSort(int Arr[], int Size) { int Key, i, j; for(i=0; i<Size; i++) { Key = Arr[i]; j = i - 1; while(j>=0 && Arr[j]>Key) { Arr[j+1] = Arr[j]; j--; } j++; Arr[j] = Key; } } ♣ Output :: How many numbers you wish to enter: 5 Enter 5 integers: 4 3 0 8 7 So...

C Program To Implement Insertion Sort Algorithm To Print N Numbers In Descending Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> void InsertionSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); InsertionSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void InsertionSort(int Arr[], int Size) { int Key, i, j; for(i=0; i<Size; i++) { Key = Arr[i]; j = i - 1; while(j>=0 && Arr[j]<Key) { Arr[j+1] = Arr[j]; j--; } j++; Arr[j] = Key; } } ♣ Output :: How many numbers you wish to enter: 5 Enter 5 integers: 5 0 4 9 1 So...

C Program To Implement Selection Sort Algorithm To Print N Numbers In Ascending Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> void SelectionSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); SelectionSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void SelectionSort(int Arr[], int Size) { int Swap, i, j; for(i=0; i<Size-1; i++) for(j=i+1; j<=Size-1; j++) if(Arr[i] > Arr[j]) { Swap = Arr[i]; Arr[i] = Arr[j]; Arr[j] = Swap; } } ♣ Output :: How many numbers you wish to enter: 5 Enter 5 integers: 4 0 2 7 1 Sorted li...

C Program To Implement Selection Sort Algorithm To Print N Numbers In Descending Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> void SelectionSort(int*, int); int main() { int ArraySize; printf("How many numbers you wish to enter: "); scanf("%d", &ArraySize); int Array[ArraySize], i; printf("Enter %d integers: \n", ArraySize); for(i=0; i<ArraySize; i++) scanf("%d", &Array[i]); SelectionSort(Array, ArraySize); printf("\nSorted list in ascending order:\n"); for(i=0; i<ArraySize; i++) printf("%d ", Array[i]); getch(); return(0); } void SelectionSort(int Arr[], int Size) { int Swap, i, j; for(i=0; i<Size-1; i++) for(j=i+1; j<=Size-1; j++) if(Arr[i] < Arr[j]) { Swap = Arr[i]; Arr[i] = Arr[j]; Arr[j] = Swap; } } ♣ Output :: How many numbers you wish to enter: 5 Enter 5 integers: 7 0 3 1 8 Sorted li...

C Program To Read A File

♣ Try it Out :: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 50 main() { FILE *fp; char ch, fn[MAX]; printf("Type a file name which you wish to see: "); gets(fn); fp = fopen(fn, "r"); if(fp == NULL) { printf("\nError! File does not exist."); getch(); exit(1); } printf("The file: %s contains: \n\n", fn); while((ch=fgetc(fp)) != EOF) putc(ch, stdout); fclose(fp); getch(); return(0); } ♣ Output :: Type a file name which you wish to see: file1.txt The file: file1.txt contains: Nurul Amin Muhit Student of Leading University B.Sc [engg.] in CSE ♣ Downloads :: Download source code Download executable file

C Program To Write In A File

♣ Try it Out :: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 50 main() { FILE *fp; char ch, fn[MAX]; printf("Type a file name where you wish to write: "); gets(fn); fp = fopen(fn, "r"); if(fp != NULL) { printf("The file exists. Overwrite it? (y/n) "); char con = getch(); if(con=='n') { printf("\nType a different file name: "); gets(fn); } } fp = fopen(fn, "w"); printf("Type your text & Press x to save: \n\n"); while((ch=getchar()) != EOF) { if(ch == 'x') break; fputc(ch, fp); fflush(fp); } fclose(fp); printf("\nData has been added successfully."); getch(); return(0); } ♣ Output :: Type a file name where you wish to write: muhit.txt Type your text & Press x to save: Nurul Amin Muhit Stu...

C Program To Merge Two Files

♣ Try it Out :: #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { FILE *fs1, *fs2, *ft; char ch, file1[20], file2[20], file3[20]; printf("Enter name of first file: "); gets(file1); printf("Enter name of second file: "); gets(file2); printf("File name which will store contents of two files: "); gets(file3); fs1 = fopen(file1, "r"); fs2 = fopen(file2, "r"); if(fs1 == NULL || fs2 == NULL) { perror("Error "); getch(); exit(EXIT_FAILURE); } ft = fopen(file3, "w"); if(ft == NULL) { perror("Error "); exit(EXIT_FAILURE); } while((ch=fgetc(fs1)) != EOF) fputc(ch, ft); while((ch=fgetc(fs2)) != EOF) fputc(ch, ft); printf("Two files were merged into %s file successfully. \n", file3); fclose(fs1); fclose(fs2); fclose(f...

C Program To Copy Data From One File To Another

♣ Try it Out :: #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 25 main() { char ch, source[MAX], target[MAX]; FILE *fs, *ft; printf("Type name of file to copy: "); gets(source); fs = fopen(source, "r"); if(fs == NULL) { perror("Error "); getch(); exit(EXIT_FAILURE); } printf("Type name of target file: "); gets(target); ft = fopen(target, "r"); if(ft != NULL) { printf("The file exists. Overwrite it? (y/n) \n"); char con = getch(); if(con != 'y') { printf("\nType a different target file name: "); gets(target); } } ft = fopen(target, "w"); while((ch=fgetc(fs)) != EOF) fputc(ch, ft); printf("\nThe file has been copied successfully."); fclose(fs); fclose(ft); getch(); ...

C Program To Delete A File

♣ Try it Out :: #include<stdio.h> #include<conio.h> main() { int status; char fname[25]; printf("Enter the file name which you wish to delete: "); gets(fname); status = remove(fname); if( status == 0 ) printf("The %s file has been deleted successfully.", fname); else { perror("Error "); } getch(); return(0); } ♣ Output :: Enter the file name which you wish to delete: muhit.txt The muhit.txt file has been deleted successfully. ♣ Downloads :: Download source code Download executable file

C Program To List Files In A Folder Or Directory

♣ Try it Out :: #include<stdio.h> #include<conio.h> #include<dir.h> main() { int done; struct ffblk a; printf("Press any key to view the files \n"); getch(); done = findfirst("*.*", &a, 0); printf("The files are: \n\n"); while( ! done) { printf("%s\n", a.ff_name); done = findnext(&a); } getch(); return(0); } ♣ Output :: Press any key to view the files The files are: CPROGR~1.HTM CPROGR~2.HTM CPROGR~3.HTM CPROGR~1.EXE ♣ Downloads :: Download source code Download executable file

C Program To Take Input A 2D Array Of 2x5 In Column Major Order

♣ Try it Out :: #include<stdio.h> #include<conio.h> #define row 2 #define col 5 main() { int arr[row][col], i, j; printf("Enter a 2x5 matrix in column major order: \n\n"); for(i=0; i<col; i++) for(j=0; j<row; j++) scanf("%d", &arr[j][i]); printf("\nYou have entered: \n\n"); for(i=0; i<row; i++) { for(j=0; j<col; j++) printf("a%d%d=%d\t", i, j, arr[i][j]); printf("\n"); } getch(); return(0); } ♣ Output :: Enter a 2x5 matrix in column major order: 1 2 3 4 5 6 7 8 9 0 You have entered: a00=1 a01=3 a02=5 a03=7 a04=9 a10=2 a11=4 a12=6 a13=8 a14=0 ♣ Downloads :: Download source code Download executable file

C Program To Calculate The Series 3+9+12+...+N

♣ Try it Out :: #include<stdio.h> #include<conio.h> int SeriesSum(int n) { if(n == 0) return(0); else return(n+SeriesSum(n-3)); } int main() { int num, i; printf("Enter the n\'th number (N will be a multiple of 3): "); scanf("%d", &num); printf("\nSeries: "); for(i=3; i ♣ Output :: Enter the n'th number (N will be a multiple of 3): 27 Series: 3 + 6 + 9 + 12 + 15 + 18 + 21 + 24 + 27 = 135 ♣ Downloads :: Download source code Download executable file