Showing posts with label C Language. Show all posts

Write a program in c language to reverse a string in reverse order without using strrev function or any other function

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
char str[100], ch, str2[100];
int count = 1, i, count2 = 0;

printf("Enter String: ");
while(ch != 13)
{
ch = getche();
str[count] = ch;
count++;
}
 
    for(i=0; i<=99; i++)
    {
str2[i] = str[i];
}

    for(i=count; i>=0; i--)
    {
str[count2] = str2[i];
count2++;
}

printf("\nRevered string is\n");

for(i = 0; i < count; i++)
{
printf("%c", str[i]);
}

   getch();
}

Write a program in c language to reverse a string by using strrev function or ptrint a string in reverse order

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
   char str[100];

   printf("Enter a string to reverse: ");
   gets(sttr);

   strrev(str);  //function to reverse a string

   printf("Reverse of entered string is: %s\n", str);

   getch();
}

Write a program in c language to compare two strings by using pointers.

#include <stdio.h>
#include <conio.h>

int compare_strings(char*, char*);

int main()
{
    char str1[100], str2[100], result;

    printf("Enter first string: ");
    gets(str1);

    printf("Enter second string: ");
    gets(str2);

    result = compare_strings(str1, str2);

    if ( result == 0 )
       printf("Strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    getch();
}

int compare_strings(char *str1, char *str2)
{
   while(*str1==*str2)
   {
      if ( *str1 == '\0' || *str2 == '\0' )
         break;

      str1++;
      str2++;
   }
   if( *str1 == '\0' && *str2 == '\0' )
      return 0;
   else
      return 1;
}

Write a program in c language to compare two strings without strcmp function or any other function

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
   char str1[100], str2[100], ch;
   int i, check = 0, count = 0;

for(i=0; i<=100; i++)
{
str1[i] = str2[i] = 0;
}

   printf("Enter the first string: ");
   while(ch != 13)
   {
ch = getche();
str1[count] = ch;
count++;
   }
 
 
count = 0;
ch = NULL;
   printf("\nEnter the second string: ");
   while(ch != 13)
   {
ch = getche();
str2[count] = ch;
count++;
   }

for(i=0; i<=99; i++)
{
if(str1[i] != str2[i])
check = 1;
}


   if( check == 0 )
   {
      printf("\nEntered strings are equal.\n");
   }
   else
   {
      printf("\nEntered strings are not equal.\n");
   }
 
getch();
}

Write a program to Compare two strings by using strcmp function in c language

#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
   char str1[100], str2[100];

   printf("Enter the first string: ");
   gets(str1);

   printf("Enter the second string: ");
   gets(str2);

   if( strcmp(str1, str2) == 0 )
   {
      printf("Entered strings are equal.\n");
   }
   else
   {
      printf("Entered strings are not equal.\n");
   }
 
getch();
}

Write a program in C language to add two numbers using pointers




#include <stdio.h>
#include <conio.h>

int main()
{
   int num1, num2, *p, *q, sum;

   printf("Enter two integers to add: ");
   scanf("%d %d", &num1, &num2);

   p = &num1;
   q = &num2;

   sum = *p + *q;

   printf("Sum of given numbers is: %d",sum);

   getch();
}

Write a program in c language to print Floyd's triangle


#include <stdio.h>
#include <conio.h>

int main()
{
  int n, i,  c, a = 1;

  printf("Enter the number of rows of Floyd's triangle to print: ");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ", a);
      a++;
    }
    printf("\n");
  }

  getch();
}


Write a program in c language to multiply matrices of NxN order by recursion

#include <stdio.h>
#include <conio.h>
#define MAX 10

int m,n,o,p;
int c[MAX][MAX];

void multiplyMatrix(int a[MAX][MAX],int b[MAX][MAX]){

    static int sum,i=0,j=0,k=0;

    if(i<m){ //rows of first matrix
    if(j<p){  //columns of second matrix
         if(k<n){
             sum=sum+a[i][k]*b[k][j];
             k++;
             multiplyMatrix(a,b);
         }
         c[i][j]=sum;
             sum=0;
             k=0;
             j++;
             multiplyMatrix(a,b);
    }
    j=0;
    i++;
    multiplyMatrix(a,b);
    }
}

int main(){
 
    int a[MAX][MAX],b[MAX][MAX],i,j,k;

    printf("Enter the row and column of first matrix: ");
    scanf("%d %d",&m,&n);
    printf("Enter the row and column of second matrix: ");
    scanf("%d %d",&o,&p);

    if(n!=o){

         printf("Matrix multiplication is not possible");
         printf("\nColumn of first matrix must be same as row of second matrix");
    }
  else{

      printf("Enter the First matrix: ");
      for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);

      printf("Enter the Second matrix: ");
      for(i=0;i<o;i++)
      for(j=0;j<p;j++)
           scanf("%d",&b[i][j]);

      printf("\nThe First matrix is: \n");
      for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<n;j++){
           printf("%d\t",a[i][j]);
      }
      }

      printf("\nThe Second matrix is: \n");
      for(i=0;i<o;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",b[i][j]);
      }
      }

      multiplyMatrix(a,b);

  }

  printf("\nThe multiplication of two matrix is: \n");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",c[i][j]);
      }
  }
 
  getch();
}

Write a program to check given number is prime or not using recursion

#include <stdio.h>
#include <conio.h>

int isPrime(int number, int i)
{

    if(i==1)
    {
        return 1;
    }
    else
    {
       if(number%i==0)
         return 0;
       else
         isPrime(number,i-1);
    }
}

int main(){

    int number, prime;

    printf("Enter a positive number: ");
    scanf("%d", &number);

    prime = isPrime(number, number/2);

   if(prime==1)
        printf("%d is a prime number.", number);
   else
      printf("%d is not a prime number.", number);

   getch();
}

Write a Program To Find The Highest Common Factor in C Language


#include<stdio.h>
#include<conio.h>
int higest_common_factor(int number1 ,int number2);
 main()
{
int number1,number2;
//; clrscr();
printf("enter the two number :");
scanf("\n%d\n%d",&number1 ,&number2);


printf("\n higest_common_factor of two numbers is =%d",higest_common_factor(number1,number2));

getch();
}


int higest_common_factor(int number1 ,int number2)
{
int counter,minimum,m;
if (number1>number2)
{
minimum=number2;
}
else
{
minimum=number1;
}

for(counter=minimum;counter>=1;counter--)
{
if(number1%counter==0 && number2%counter==0)
{
  m=counter;
return m;
  }

}
  }

Advantage And Disadvantage Of An Array

Array index start from zero.
Advantages of arrays:
 It has a ability of storing a many elements at a time
 It allows random accessing of elements i.e. any element of the array can be random access using indexes.
Disadvantages:
 Determine the size of the array is an essential.
 There is a chance of memory wastage or shortage.
 If we delete one element in an array then we traverse throughout the array..
Creation of an array consists of three steps.
1. First we can declare the array.
2. Then we create a memory
3. And then put a value into memory.

Common type of arrays is: single dimensional array, multidimensional array and etc.

Write A Program To Count Characters/Numbers Which You Enter

#include<stdio.h>
#include<conio.h>
int main(){
  int n,count=0;

  printf("Enter a number: ");
  scanf("%d",&n);

  for(;n!=0;n=n/10)
      count++;

  printf("Total digits is:  %d",count);

 getch();
}

Write A Program To Print A Multiple Table in One Program

#include<stdio.h>
#include<conio.h>
int main(){
  int num,counter,counter2;
  printf("Enter the number range: ");
  scanf("%d",&num);
  for(counter=1;counter<=num;counter++){
      for(counter2=1;counter2<=10;counter2++)
           printf("%d*%d=%d \n",counter,counter2,counter*counter2);
      printf("\n");
  }
  getch();
}

Write A Program Without Using Subtraction Operator


#include<stdio.h>
#include<conio.h>

int main(){

    int l,m;
    int sum;

    printf("Enter any two integers: ");
    scanf("%d%d",&l,&m);

    sum = l+ ~m + 1;

    printf("Difference of two integers: %d",sum);

    getch();
}

Write a c language to tell if a year is leap year or not.

Leap Year: A year which is divided on four with 0 remainder is called leap year.

#include <stdio.h>
#include <conio.h>
int main()
{
               int year;
               printf("Enter a year to check if it is a leap year: ");
               scanf("%d", &year);

                if (year%400 == 0)
                      printf ("%d is a leap year.\n", year);
               else if ( year%100 == 0)
                      printf ("%d is not a leap year.\n", year);
               else if ( year%4 == 0 )
                      printf ("%d is a leap year.\n", year);
               else
                      printf ("%d is not a leap year.\n", year);
                getch();
}

Write a program to print on screen without semicolon.

Program 1


#include <stdio.h>
#include <conio.h>

int main()
{
    if(printf("http://www.compprog.com"))
    {
           
    }
    getch();
}

Program 2


#include <stdio.h>
#include <conio.h>

int main()
{
    while(!printf("http://www.compprog.com"))
    {
            
    }
    getch();
}

Program 3


#include <stdio.h>
#include <conio.h>

int main()
{
    switch(printf("http://www.compprog.com"))
    {
            
    }
    getch();
}


Write a program to calculate factorial of given number in c language.

#include <stdio.h>
#include <conio.h>

int main()
{
   int num, i, sum=1;
   printf("Enter a number: ");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
         sum=sum*i;
   }
   printf("Factorial of %d is %d.", num, sum);
   getch();
}

Write a program in c language to tell that given number is palindromic number or numeral palindrome or not?

palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, …

#include <stdio.h>
#include <conio.h>

int main()
{
   int num, reverse = 0;
   int temp;

   printf("Enter a number to check if it is a palindrome or not: ");
   scanf("%d",&num);

   temp = num;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
   if ( num == reverse )
      printf("Number %d is a palindrome.\n", num);
   else
      printf("Number %d is not a palindrome.\n", num);
   getch();
}

Write A code Of Fibonacci series without Recursion.

#include<stdio.h>
#include<conio.h>


int main(void)
{
    int n,l=0,m=1,c=0,counter=1;
    printf("How many number you want to Generate: ");
    scanf("%d",&n);
    printf("%d  %d",l,m);
    while(counter<=n-2)
    {
        c=l+m;
        printf("  %d",c);
        l=m;
        m=c;
      counter++;
    }
getch();

}

Write a program in c language to concatenate two strings.

#include<conio.h>
#include<stdio.h>

void stringconcatenation(char[],char[]);

 main()
{

    char mrr1[100],mrr2[100];
    printf("Enter the first string: ");
   gets(mrr1);
    printf("Enter the second string: ");
    gets(mrr2);
    stringconcatenation(mrr1,mrr2);
   printf("The String after concatenation: %s",mrr1);
    getch();
}
void stringconcatenation(char mrr1[],char mrr2[])
{
    int l=0,k=0;
    while(mrr1[l]!='\0')
    {
         l++;
    }
  while(mrr2[k]!='\0')
    {
         mrr1[l] = mrr2[k];
         l++;
         k++;
    }
 mrr1[l] ='\0';
}