├── .gitignore ├── fibonacci.c ├── reverse.c ├── sum.c ├── callbyvalue.c ├── callbyreference.c ├── pointer.c ├── gcd.c ├── temperature.c ├── palindrome.c ├── greatestof3.c ├── cos.c ├── armstrong.c ├── pascal.c ├── concatenate.c ├── bubble sort.c ├── file.c ├── variance.c ├── transpose.c └── hex.c /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | -------------------------------------------------------------------------------- /fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n , i , t1 = 1 , t2 = 1, nt ; 6 | printf("Enter the length of the series\n"); 7 | scanf("%d",&n); 8 | for ( i = 0 ; i < n ; i++) 9 | { 10 | printf("%d ", t1 ); 11 | nt = t1 + t2; 12 | t1 = t2; 13 | t2 = nt; 14 | } 15 | return 0; 16 | } -------------------------------------------------------------------------------- /reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n , rem , rev = 0; 6 | printf("Enter the number to be reversed\n"); 7 | scanf("%d",&n); 8 | while (n>0) 9 | { 10 | rem = n % 10; 11 | rev = rev * 10 + rem; 12 | n = n / 10; 13 | } 14 | printf("The reversed number is %d \n", rev); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[5]; 5 | int i , sum = 0; 6 | printf("Enter the 5 elements\n"); 7 | for( i = 0 ; i < 5 ; i++) 8 | { 9 | scanf("%d",&a[i]); 10 | } 11 | for(i = 0 ; i <5 ; i++) 12 | { 13 | sum = sum + a[i]; 14 | i++; 15 | } 16 | printf("%d is the sum \n",sum ); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /callbyvalue.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int, int); 3 | int main() 4 | { 5 | int a, b; 6 | printf("Enter 2 numbers to be swapped\n"); 7 | scanf("%d%d",&a,&b); 8 | swap(a,b); 9 | } 10 | void swap(int a, int b) 11 | { 12 | int temp; 13 | temp = a; 14 | a = b; 15 | b = temp; 16 | printf("after swapping a = %d , b = %d \n",a,b); 17 | } -------------------------------------------------------------------------------- /callbyreference.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int* , int*); 3 | int main() 4 | { 5 | int a, b; 6 | printf("Enter 2 numbers to be swapped\n"); 7 | scanf("%d%d",&a,&b); 8 | swap(&a,&b); 9 | } 10 | void swap(int *a, int *b) 11 | { 12 | int temp; 13 | temp = *a; 14 | *a = *b; 15 | *b = temp; 16 | printf("after swapping a = %d , b = %d \n",*a,*b); 17 | } -------------------------------------------------------------------------------- /pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | void show(int *); 3 | int main() 4 | { 5 | int a[10] , i ; 6 | printf("Enter the 10 elements in array\n"); 7 | for (int i = 0; i < 10; ++i) 8 | { 9 | scanf("%d",&a[i]); 10 | } 11 | show(a); 12 | return 0; 13 | } 14 | void show(int *a) 15 | { 16 | for (int i = 0; i < 10; ++i) 17 | { 18 | printf("%d ", a[i]); 19 | } 20 | } -------------------------------------------------------------------------------- /gcd.c: -------------------------------------------------------------------------------- 1 | #include 2 | int gcd(int , int); 3 | int main() 4 | { 5 | int answer, n1, n2; 6 | printf("Enter the 2 numbers\n"); 7 | scanf("%d%d",&n1,&n2); 8 | answer=gcd(n1,n2); 9 | printf("%d is the gcd\n",answer ); 10 | return 0; 11 | } 12 | int gcd(int n1 , int n2) 13 | { 14 | if ( n2 != 0 ) 15 | return (gcd(n2, n1%n2)); 16 | else 17 | return n1; 18 | } -------------------------------------------------------------------------------- /temperature.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float f , c; 6 | printf("Enter the temperature in celsius\n"); 7 | scanf("%f",&c); 8 | f = (9.0/5*c)+32; 9 | printf("The temperature in fahrenheit is %f \n",f); 10 | printf("Enter the temperature in fahrenheit\n"); 11 | scanf("%f",&f); 12 | c = (f - 32 ) * 5/9 ; 13 | printf("The temperature in fahrenheit is %f \n",f); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i = 0 , n = 0 , j; 6 | char a[100]; 7 | printf("Enter a string\n"); 8 | scanf("%s",a); 9 | while(a[i] != '\0') 10 | { 11 | i++; 12 | n++; 13 | } 14 | for(i = 0 , j = n - 1 ; i <= (n-1)/2 ;i++, j-- ) 15 | { 16 | if(a[i] != a[j]) 17 | { 18 | printf("Not a palindrome\n"); 19 | return 0; 20 | } 21 | } 22 | printf("It is a palindrome\n"); 23 | } -------------------------------------------------------------------------------- /greatestof3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a , b , c; 6 | printf("Enter the three values a, b, c \n"); 7 | scanf("%d%d%d",&a,&b,&c); 8 | if(a>b) 9 | { 10 | if(a>c) 11 | { 12 | printf(" Max is %d \n",a); 13 | } 14 | else 15 | { 16 | printf("Max is %d\n",c ); 17 | } 18 | } 19 | else if (b>c) 20 | { 21 | printf("Max is %d ",b); 22 | } 23 | else 24 | { 25 | printf("Max is %d ",c); 26 | } 27 | return 0 ; 28 | 29 | } -------------------------------------------------------------------------------- /cos.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int n ,i, sign = -1, fact = 1; 6 | float current , x , power, sum ; 7 | printf("Enter the degree and number of terms\n"); 8 | scanf("%f%d",&x,&n); 9 | x = x * 3.14 / 180; 10 | sum = 1; 11 | for(i= 2 ; i <=n ; i = i + 2 ) 12 | { 13 | fact = fact * i * (i -1); 14 | power = pow(x, i); 15 | current = power/fact; 16 | sum = sum + current*sign; 17 | sign = sign*-1; 18 | } 19 | printf("sum is %f \n",sum ); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /armstrong.c: -------------------------------------------------------------------------------- 1 | #include 2 | void armstrong(int); 3 | int main() 4 | { 5 | int n ; 6 | printf("Enter the number to be checked\n"); 7 | scanf("%d",&n); 8 | armstrong(n); 9 | return 0; 10 | } 11 | void armstrong(int n) 12 | { 13 | int temp , sum = 0, rem ; 14 | temp = n; 15 | while(n>0) 16 | { 17 | rem = n % 10 ; 18 | sum = sum + rem*rem*rem; 19 | n = n/10; 20 | } 21 | if(temp==sum) 22 | { 23 | printf("armstrong number\n"); 24 | } 25 | else 26 | printf("Not an armstrong number\n"); 27 | } -------------------------------------------------------------------------------- /pascal.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i , j , space , row , c =1 ; 5 | printf("Enter the number of rows\n"); 6 | scanf("%d",&row); 7 | for(i = 0 ; i < row ; i++) 8 | { 9 | for(space = 1; space <= row - i; space++) 10 | { 11 | printf(" "); 12 | } 13 | for(j = 0;j <= i ; j++) 14 | { 15 | if(j==0|| i==0) 16 | { 17 | c = 1; 18 | } 19 | else 20 | { 21 | c = c* ( i - j + 1)/j; 22 | } 23 | printf("%4d",c ); 24 | } 25 | printf("\n"); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /concatenate.c: -------------------------------------------------------------------------------- 1 | #include 2 | void concat(char[] , char[]); 3 | int main() 4 | { 5 | char p[100], q[100]; 6 | printf("Enter the string\n"); 7 | scanf("%s", p); 8 | printf("Enter the second string\n"); 9 | scanf("%s", q); 10 | concat(p,q); 11 | return 0; 12 | } 13 | void concat(char p[100],char q[100]) 14 | { 15 | int c = 0 , d =0; 16 | while(p[c] != '\0') 17 | { 18 | c++; 19 | } 20 | while(q[d] != '\0') 21 | { 22 | p[c] = q[d]; 23 | c++; 24 | d++; 25 | } 26 | p[c] = '\0'; 27 | printf("The concated string is %s \n", p); 28 | } -------------------------------------------------------------------------------- /bubble sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[100], i , j , temp, n; 6 | printf("How many elements\n"); 7 | scanf("%d",&n); 8 | printf("Enter the elements\n"); 9 | for(i = 0; i a[j+1]) 18 | { 19 | temp = a[j]; 20 | a[j] = a [j+1]; 21 | a [j + 1] = temp; 22 | } 23 | } 24 | } 25 | printf("The sorted array is \n"); 26 | for( i = 0 ; i < n ; i++) 27 | { 28 | printf("%d ",a[i] ); 29 | } 30 | return 0; 31 | } -------------------------------------------------------------------------------- /file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | FILE *fp; 6 | char fname[20], name [20], addr[20]; 7 | int age ; 8 | float salary; 9 | printf("Enter the filename \n"); 10 | gets(fname); 11 | fp = fopen(fname, "w"); 12 | printf("Enter name\n"); 13 | gets(name); 14 | fflush(stdin); 15 | fprintf(fp, "%s\n", name ); 16 | printf("Enter the address\n"); 17 | gets(addr); 18 | fflush(stdin); 19 | fprintf(fp, "%s\n", addr ); 20 | printf("Enter the age\n"); 21 | scanf("%d",&age); 22 | fflush(stdin); 23 | fprintf(fp, "%d\n", age); 24 | printf("Enter salary\n"); 25 | fflush(stdin); 26 | scanf("%f",&salary); 27 | fprintf(fp, "%f\n",salary ); 28 | fclose(fp); 29 | } -------------------------------------------------------------------------------- /variance.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int n , i , sum=0; 6 | int a[100]; 7 | float variance, mean, deviation; 8 | printf("how many elements\n"); 9 | scanf("%d",&n); 10 | printf("Enter the elements\n"); 11 | for (int i = 0; i < n; ++i) 12 | { 13 | scanf("%d",&a[i]); 14 | } 15 | for (int i = 0; i < n; ++i) 16 | { 17 | sum = sum + a[i]; 18 | } 19 | mean = (float)sum / n; 20 | sum = 0; 21 | for (int i = 0; i < n; ++i) 22 | { 23 | sum = sum + (float)(pow(a[i]- mean , 2)); 24 | } 25 | variance = sum / (n - 1); 26 | deviation = sqrt(variance); 27 | printf("%f is the variance\n",variance ); 28 | printf("%f is the standard deviation\n",deviation ); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /transpose.c: -------------------------------------------------------------------------------- 1 | #include 2 | void transpose(int a[10][10], int r, int c ); 3 | int main() 4 | { 5 | int r , c , i ; 6 | int a[10][10]; 7 | printf("Enter the number of rows and columns\n"); 8 | scanf("%d%d", &r, &c); 9 | printf("Enter the elements\n"); 10 | for (int i = 0; i < r; ++i) 11 | { 12 | for (int j = 0; j < c; j++) 13 | { 14 | scanf("%d",&a[i][j]); 15 | } 16 | } 17 | transpose(a, r, c); 18 | return 0; 19 | } 20 | void transpose(int a[10][10], int r , int c) 21 | { 22 | int b[10][10]; 23 | int i , j; 24 | for (int i = 0; i < r; ++i) 25 | { 26 | for (int j = 0; j < c; j++) 27 | { 28 | b[j][i] = a[i][j]; 29 | } 30 | } 31 | printf("The transpose is \n"); 32 | 33 | for (int i = 0; i < r; ++i) 34 | { 35 | for (int j = 0; j < c; j++) 36 | { 37 | printf("%d ",b[i][j]); 38 | } 39 | printf("\n"); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /hex.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int opt, i , j , n ; 6 | int a[100]; 7 | char h[100]; 8 | printf("Choose from the options\n"); 9 | printf("1. Binary\n"); 10 | printf("2. Octal\n"); 11 | printf("3. Hexadecimal\n"); 12 | scanf("%d",&opt); 13 | switch(opt) 14 | { 15 | case 1: printf("Enter the number\n"); 16 | scanf("%d",&n); 17 | for( i = 0 ; n > 0 ; i++) 18 | { 19 | a[i] = n % 2; 20 | n = n/2; 21 | } 22 | for ( j = i -1 ; j>= 0 ; j-- ) 23 | { 24 | printf("%d", a[j]); 25 | } 26 | printf("\n"); 27 | break; 28 | case 2: printf("Enter the number\n"); 29 | scanf("%d",&n); 30 | for( i = 0 ; n > 0 ; i++) 31 | { 32 | a[i] = n % 8; 33 | n = n/8; 34 | } 35 | for ( j = i -1 ; j>= 0 ; j-- ) 36 | { 37 | printf("%d", a[j]); 38 | } 39 | printf("\n"); 40 | break; 41 | 42 | case 3: printf("Enter the number\n"); 43 | scanf("%d",&n); 44 | for( i = 0 ; n > 0 ; i++) 45 | { 46 | a[i] = n % 16; 47 | n = n/16; 48 | if(a[i] > 9) 49 | { 50 | 51 | h[i]= a[i]+55; 52 | } 53 | else 54 | h[i]= a[i]+ 48; 55 | } 56 | for ( j = i -1 ; j>= 0 ; j-- ) 57 | { 58 | printf("%c", h[j]); 59 | } 60 | printf("\n"); 61 | break; 62 | 63 | default : printf("Invalid input\n"); 64 | break; 65 | 66 | } 67 | return 0; 68 | } 69 | --------------------------------------------------------------------------------