Showing posts with label c programs. Show all posts
Showing posts with label c programs. Show all posts

Write a C program to accept the reverse of a string using pointers.

#include<stdio.h>
#include<conio.h>
main()
{
    char str[20],rev[20];
    char *st,*rv,*start;
    clrscr();
    printf("\nEnter a string");
    gets(str);
    st=str;
    rv=rev;
    start=st;
    while(*st!='\0')
    st ++;
    rv --;
    while(st>=start)
    {
      *rv=*st;
      st--;
      rv++;
    }
    *rv='\0';
    printf("\nThe original string is %s",str);
    printf("\nThe reverse string is %s",rev);
    getch();
    return;
}

Write a C Program to find the length of a string without using the built – in function.

#include<stdio.h>
#include<conio.h>
void main()
{
   char a[12];
   int i,length=0;
   clrscr();
   printf("\nEnter a string");
   scanf("%s",&a);
   for(i=0;a[i]!='\0';i++)
   length++;
   printf("\nNumber of character in the string is %d",length);
   getch();
}

Write a C Program to accept a sentence and convert all lowercase characters to uppercase and vice –versa.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
   char str[15];
   int i,len;
   clrscr();
   printf("\nEnter a string\n");
   gets(str);
   len=strlen(str);
   for(i=0;i<len;i++)
   {
      if(str[i]>='a' && str[i]<='z')
      str[i]=str[i]-32;
      else
      if(str[i]>='A' && str[i]<='Z')
      str[i]=str[i]+32;
   }
   printf("\nConverted string is %s",str);
   getch();
   return 0;
}

Write a C Program to find trace and normal of a square matrix using functions.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void calfun(int a[10][10],int n);
main()
{
    int a[10][10],i,j,n;
    clrscr();
    printf("\nEnter order of the matrix:\n");
    scanf("%d",&n);
    printf("\nEnter the elements of matrix:\n");
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    scanf("%d",&a[i][j]);
    calfun(a,n);
    return;
}
void calfun(int a[10][10],int n)
{
   int i,j,trace=0,sum;
   float norm;
   for(i=0;i<n;i++)
   trace=trace+a[i][i];
   sum=0;
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   sum=sum+a[i][j]*a[i][j];
   norm=sqrt(sum);
   printf("\nTrace of the matrix is %d",trace);
   printf("\nNorm of the matrix is %6.2f",norm);
   getch();
   return;
}

Write a C Program to compute the sum of even numbers and the sum of odd numbers using a function.

#include<stdio.h>
#include<conio.h>
void sumfun(int a[],int n);
void main()
{
    int a[15],n,i;
    clrscr();
    printf("\nHow many number of terms?");
    scanf("%d",&n);
    printf("Enter the elements in the list");
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    sumfun(a,n);
    getch();
}
void sumfun(int a[],int n)
{
   int i,osum=0,esum=0;
   for(i=0;i<n;i++)
   if(a[i]%2==0)
     esum=esum+a[i];
     else
       osum=osum+a[i];
       printf("esum is %d",esum);
       printf("\nosum is %d",osum);

}

Write a C Program to find if a character is alphabetic or numeric or special character.

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch;
    clrscr();
    printf("\nEnter a character:");
    scanf("%c",&ch);
    if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
      printf("is an Alphabet");
      else if (ch>='0' && ch<='9')
     printf("Its numeric");
       else
       printf("Its a special character");
    getch();
}

Write a Program to find the factorial of a number using function

#include<stdio.h>
#include<conio.h>
void main()
{
     long factorial(int);
     int n;
     long fact;
     clrscr();
     printf("\nEnter an integer\n");
     scanf("%d",&n);
     fact=factorial(n);
     printf("\nFactorial of %d=%ld",n,fact);
     getch();
     return 0;
}
long factorial(int n)
{
   long f=1;
   int i;
   if(n==0)
     return 1;
     for(i=1;i<=n;i++)
     f=f*i;
     return f;
}

Write a C Program to read a string and check whether it is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char str[80];
   int l,i;
   clrscr();
   printf("\nEnter the string:");
   gets(str);
   printf("\nThe entered string is %s",str);
   l=strlen(str);
   for(i=0;i<l/2;i++)
   {
       if (str[i]!=str[l-1-i])
       {
     printf("\n%s is not a palindrome\n",str);
     getch();
     exit(0);
       }
    }
printf("\n%s is a palindrome",str);
getch();
}

Write a C Program to read two matrices and perform addition and subtractions of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10][10],b[10][10],sum[10][10],diff[10][10],i,j,n;
   clrscr();
   printf("Enter the order of matrix:");
   scanf("%d",&n);
   printf("\nEnter the matrix Elements A\n");
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);
   printf("\nEnter the matrix of elements B\n");
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   scanf("%d",&b[i][j]);
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   {
    sum[i][j]=a[i][j]+b[i][j];
    diff[i][j]=a[i][j]-b[i][j];
   }
   printf("\nSumation matrix\n");
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   {
      printf("%d\t",sum[i][j]);
   }
   printf("\n");
   printf("Difference Matrix\n");
   for(i=0;i<n;i++)
   for(j=0;j<n;j++)
   {
      printf("%d\t",diff[i][j]);
   }
   printf("\n");
   getch();
   return;
}

Write a C Program to input numbers and to find mean variance and standard deviation.

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
   int i,n;
   float std_dev,sum=0,sumsqr=0,mean,value,variance=0.0,a[100];
   clrscr();
   printf("\nEnter number of data points");
   scanf("%d",&n);
   printf("\nEnter data points");
   for(i=0;i<n;i++)
   {
      printf("\nPoint %d:",i+1);
      scanf("%f",&a[i]);
      sum=sum+a[i];
   }
   mean=sum/n;
   sumsqr=0;
   for(i=0;i<n;i++)
   {
     value=a[i]-mean;
     sumsqr=sumsqr+value*value;
    }
     variance=sumsqr/n;
     std_dev=sqrt(variance);
     printf("\nMean=%f",mean);
     printf("\nVariance=%f",variance);
     printf("\nstd_dev=%f",std_dev);
     getch();
     return;
}

Write a Program to find whether a given number is prime number or not

#include<stdio.h>
#include<conio.h>
void main()
{
    int n,i;
    clrscr();
    printf("Enter a number \n");
    scanf("%d",&n);
    if (n<=1)
    {
    printf("Not a prime number");
    getch();
    exit();
    }
    for(i=2;i<=n/2;i++)
    {
       if (n%i==0)
       {
      printf("Not a prime number");
      getch();
      exit();
       }
    }
    printf("Its a prime number");
    getch();
}

Write a C Program that reverse a given integer number and check whether the number is palindrome or not.

#include<stdio.h>
#include<conio.h>
main()
{
    int n,temp=0,t,rev=0;
    clrscr();
    printf("Enter the number\n");
    scanf("%d",&n);
    temp=n;
    while(n!=0)
    {
    t=n%10;
    rev=rev*10+t;
    n=n/10;
    }
    if(temp==rev)
    printf("Its a palindrome");
    else
    printf("Its not a palindrome");
    getch();
    return 0;
}

Write a Program to find the GCD and LCM of two integer numbers

#include<stdio.h>
#include<conio.h>
main()
{
    int m,n,temp,lcm,gcd;
    clrscr();
    printf("Enter the two numbers\n");
    scanf("%d%d",&m,&n);
    temp=m*n;
    while(m!=n)
    {
    if(m>n)
      m=m-n;
      else
      n=n-m;
    }
    gcd=n;
    lcm=temp/gcd;
    printf("LCM =%d\t GCD =%d",lcm,gcd);
    getch();
    return;
}

Write a C Program to generate and print first N FIBONACCI numbers.

#include<stdio.h>
#include<conio.h>
main()
{
    int i=3,fib1=0,fib2=1,fib3,n;
    clrscr();
    printf("\nHow many terms?");
    scanf("%d",&n);
    printf("%d\t%d\t",fib1,fib2);
    for(i=3;i<=n;i++)
    {
    fib3=fib1+fib2;
    printf("\t%d",fib3);
    fib1=fib2;
    fib2=fib3;
    }
    getch();
    return;
}

Write a Program to find the root of the given quadratic equation using switch case.

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
    int flag;
    float a,b,c,d;
    float x,x1,x2;
    float rpart,ipart;
    clrscr();
    printf("\Enter 3 NUmber:");
    scanf("%f%f%f",&a,&b,&c);
    if(a==0)
    {
      x=-c/b;
      printf("Only root x=%7.3f",x);
      exit();
    }
    d=((b*b)-(4*a*c));
    if(d>0)
       flag=1;
       else if(d==0)
      flag=2;
      else
          flag=3;
    switch(flag)
    {
    case 1: printf("\nRead and Distinct roots are :");
        x1=(-b+sqrt(d))/(2*a);
        x2=(-b-sqrt(d))/(2*a);
        printf("\nx1=%7.3f\nx2=%f",x1,x2);
        break;
    case 2: printf("\nRepeated Roots are :");
        x1=-b/(2*a);
        x2=x1;
        printf("\nx1 and x2=%f",x1);
        break;
    case 3: d=sqrt(abs(d));
        rpart=-b/(2*a);
        ipart=d/(2*a);
        printf("\nComplex Roots are ");
        printf("\nx1=%7.3f -i%f",rpart,ipart);
        printf("\nx2=%f -i%f",rpart,ipart);
        break;
    }
    getch();
    return;
}