C# programming language is a structure oriented programming language. Here some codes of C# programming that helps of you learn c# programming.
- Copy the String
- Counting Frequencies Of Elements Of Array
- Count the Digit in a Number
- Factorial
- Fibonacci Series
- File-Copy one file contents to another
- File Example
- File-How the data stored on the disk is read
- File-File operations
- Find The Roots Of A Quadratic Equation
| Name |
Code |
Copy the String
|
#include<stdio.h> void stringCopy(char[],char[]); int main() { char str1[100],str2[100]; printf("Enter any string: "); scanf("%s",str1); stringCopy(str1,str2); printf("After copying: %s",str2); return 0; } void stringCopy(char str1[],char str2[]) { int i=0; while(str1[i]!=\'\u0000\') { str2[i] = str1[i]; i++; } str2[i]=\'\u0000\'; }
|
Counting Frequencies Of Elements Of Array
|
#include <stdio.h> #include <conio.h> #include <stdlib.h> #define S 6 main() { int a[S], freq[S]; int i, j, k,n = S; clrscr(); for(i = 0; i < S; i++) { printf(" \n Enter a[%d] element: ", i); scanf(" %d ", &a[i]); freq[i] = 1; } printf(" Original Array\n "); for(i = 0; i < S; i++) printf(" %d ", a[i]); /* Main Logic Starts Here */ for(i = 0; i < n; i++) for(j = i + 1; j < n; j++) { if(a[i] == a[j]) { for(k = j; k < n; k++) a[k] = a[k+1]; freq[i]++; n--; } } printf(" \nArray with freq\n "); printf(" \nElement Freq\n "); for(i = 0; i < n; i++) printf("%d %d\n ", a[i], freq[i]); getch(); }
|
Count the Digit in a Number
|
#include<stdio.h> int main() { int num,count=0; printf("Enter a number: "); scanf("%d",&num); while(num) { num=num/10; count++; } printf("Total digits is:%d",count); return 0; }
|
Factorial
|
#include <stdio.h> #include <conio.h> long int factorial(int n); void main() { int n; clrscr(); printf("Enter the number:\n"); scanf("%d",&n); printf("Factorial of %d is %ld",n,factorial(n)); getch(); } long int factorial(int n) { if(n<=1) { return(01); } else { n=n*factorial(n-1); return(n); } }
|
Fibonacci Series
|
#include <stdio.h> #include <conio.h> long int factorial(int n); void main() { int n; clrscr(); printf("Enter the number:\n"); scanf("%d",&n); printf("Factorial of %d is %ld",n,factorial(n)); getch(); } long int factorial(int n) { if(n<=1) { return(01); } else { n=n*factorial(n-1); return(n); } }
|
File-Copy one file contents to another
|
#include <stdio.h> #include <conio.h> #include <process.h> void main(int argc, char *argv[]) { FILE *fs,*ft; char ch; clrscr(); if(argc!=3) { puts("Invalid number of arguments."); exit(0); } fs = fopen(argv[1],"r"); if(fs==NULL) { puts("Source file cannot be opened."); exit(0); } ft = fopen(argv[2],"w"); if (ft==NULL) { puts("Target file cannot be opened."); fclose(fs); exit(0); } while(1) { ch=fgetc(fs); if (ch==EOF) break; else fputc(ch,ft); } fclose(fs); fclose(ft); getch(); }
|
File Example
|
#include<stdio.h> int main() { FILE *fp; char ch; fp=fopen("file.txt","w"); printf("\nEnter data to be stored in to the file:"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); return 0; }
|
File-How the data stored on the disk is read
|
#include <stdio.h> #include <stdlib.h> void main() { FILE *fptr; char filename[15]; char ch; printf("Enter the filename to be opened\n"); gets(filename); fptr = fopen (filename, "r"); /*open for reading*/ if (fptr == NULL) { printf("Cannot open file\n"); exit(0); } ch = fgetc(fptr); while (ch != EOF) { printf ("%c", ch); ch = fgetc(fptr); } fclose(fptr); }
|
File-File operations
|
#include <stdio.h> void main() { FILE *fptr; char name[20]; int age; float salary; fptr = fopen ("emp.txt", "w"); /*open for writing*/ if (fptr == NULL) { printf("File does not exists \n"); return; } printf("Enter the name\n"); scanf("%s", name); fprintf(fptr, "Name = %s\n", name); printf("Enter the age\n"); scanf("%d", &age); fprintf(fptr, "Age = %d\n", age); printf("Enter the salary\n"); scanf("%f", &salary); fprintf(fptr, "Salary = %.2f\n", salary); fclose(fptr); }
|
Find The Roots Of A Quadratic Equation
|
#include <stdio.h> #include <conio.h> #include <math.h> void main() { float a, b, c, d, realp, imgp, r1, r2; clrscr(); printf(" Enter the 3 numbers\n "); scanf(" %f %f %f " ,&a, &b, &c); if ( a == 0 || b == 0 || c == 0 ) { printf(" Error input only non zero numbers\n "); } else { d = b * b - 4 * a * c; if ( d == 0 ) { printf(" Roots are equal\n "); r1 = r2 = - b / ( 2 * a ); printf(" Root1 = %f, Root2 = %f ", r1, r2 ); } else if(d>0) { printf( "Roots are real & distinct\n" ); r1 = ( - b + sqrt ( fabs ( d ) ) ) / ( 2 * a ); r2 = ( - b - sqrt ( fabs ( d ) ) ) / ( 2 * a ); printf(" Root1 = %f, Root2 = %f", r1, r2); } else { printf(" Roots are imaginary\n "); realp = - b / ( 2 * a ); imgp = sqrt ( fabs ( d ) ) / ( 2 * a ); printf(" Root1 = %f + i%f, Root2 = %f - i%f ",realp, imgp, realp, imgp); } } getch(); }
|