Showing posts with label C programming. Show all posts

Write a program to read a line of text then count number of vowels, number of digits and number of spaces.

#include<stdio.h>
int main(){
    char line[150];
    int i,v,c,ch,d,s,o;
    o=v=c=ch=d=s=0;
    printf("Enter a line of string:\n");
    gets(line);
    for(i=0;line[i]!='\0';++i)
    {
        if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
            ++v;
        else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
            ++c;
        else if(line[i]>='0'&&c<='9')
            ++d;
        else if (line[i]==' ')
            ++s;
    }
    printf("Vowels: %d",v);
    printf("\nConsonants: %d",c);
    printf("\nDigits: %d",d);
    printf("\nWhite spaces: %d",s);
    return 0;
}
Learn more »

Wrtie C program to find the trace and normal of a matrix.

Here is source code of the C program to find the trace & normal of a given matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to find the trace and normal of a matrix
  3.  *
  4.  * Trace is defined as the sum of main diagonal elements and
  5.  * Normal is defined as square root of the sum of all the elements
  6.  */
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. void main ()
  11. {
  12.     static int array[10][10];
  13.     int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;
  14.  
  15.     printf("Enter the order of the matrix\n");
  16.     scanf("%d %d", &m, &n);
  17.     printf("Enter the n coefficients of the matrix \n");
  18.     for (i = 0; i < m; ++i)
  19.     {
  20.         for (j = 0; j < n; ++j)
  21.         {
  22.             scanf("%d", &array[i][j]);
  23.             a = array[i][j] * array[i][j];
  24.             sum1 = sum1 + a;
  25.         }
  26.     }
  27.     normal = sqrt(sum1);
  28.     printf("The normal of the given matrix is = %d\n", normal);
  29.     for (i = 0; i < m; ++i)
  30.     {
  31.         sum = sum + array[i][i];
  32.     }
  33.     printf("Trace of the matrix is = %d\n", sum);
  34. }
Learn more »

WRITING OF ENTIRE ARRAY TO A FILE USING C PROGRAM

#include<stdio.h>
int main(){
  FILE *p;
  int i,a[10];
  if((p=fopen("myfile.dat","wb"))==NULL){
      printf("\nUnable to open file myfile.dat");
      exit(1);
  }
  printf("\nEnter ten values, one value on each line\n");
  for(i=0;i<10;i++)
      scanf("%d",&a[i]);
  fwrite(a,sizeof(a),1,p);
  fclose(p);
  return 0;
}
Learn more »

Write a c program which reads string from file.

#include<stdio.h>
int main(){
char str[70];
FILE *p;
if((p=fopen("string.txt","r"))==NULL){
printf("\nUnable t open file string.txt");
exit(1);
}
while(fgets(str,70,p)!=NULL)
puts(str);
fclose(p);
return 0;
}

Learn more »

Implement Tower of Hanoi Problem using C programmg with algorithm.


Tower of Hanoi problem:
Initial state:
  • There are three poles named as origin, intermediate and destination.
  • n number of different-sized disks having hole at the center is stacked around the
    origin pole in decreasing order.
  • The disks are numbered as 1, 2, 3, 4, ...................,n.
    Objective:
Transfer all disks from origin pole to destination pole using intermediate pole for temporary storage.
Conditions:
  • Move only one disk at a time.
  • Each disk must always be placed around one of the pole.
  • Never place larger disk on top of smaller disk.
    Algorithm: - To move a tower of n disks from source to dest (where n is positive integer):
    1. If n ===1:
    1.1. Move a single disk from
    source to dest.
    2. If n > 1:
    2.1. Let
    temp be the remaining pole other than source and dest.
    2.2. Move a tower of (n – 1) disks form source to temp. 2.3. Move a single disk from source to dest.
    2.4. Move a tower of (
    n – 1) disks form temp to dest.
    3. Terminate.
    Example: Recursive solution of tower of Hanoi:
    #include <stdio.h>
    #include <conio.h>
    void TOH(int, char, char, char); //Function prototype void main()
    {

    int n;
    printf(“Enter number of disks”); scanf(“%d”,&n); TOH(n,’O’,’D’,’I’);
    getch();

    }
    void TOH(int n, char A, char B, char C) {
    if(n>0) { 

    TOH(n-1, A, C, B);
    Printf(“Move disk %d from %c to%c\n”, n, A, B); TOH(n-1, C, B, A);


    } }
Learn more »