Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it
Showing posts with label C programs. Show all posts
Showing posts with label C programs. Show all posts

21 Jan 2024

C program that uses a loop to calculate the factorial of a given number

// Factorial program using function


#include <stdio.h>

// Function prototype

long long calculateFactorial(int n);

int main() {

    int number;
    // Get input from the user

    printf("Enter a non-negative integer: ");
    scanf("%d", &number);

    // Check if the input is non-negative

    if (number < 0) {
        printf("Please enter a non-negative integer.\n");
        return 1; // Return with an error code
   }

    // Calculate and display the factorial using a loop

    long long factorial = calculateFactorial(number);
    printf("Factorial of %d = %lld\n", number, factorial);
    return 0;

}

// Function to calculate factorial using a loop

long long calculateFactorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
         long long result = 1;
        for (int i = 2; i <= n; ++i) {
           result *= i;
        }
        return result;
    }
}


14 Jan 2024

C program example that uses functions - This program calculates the sum of two numbers using separate functions for input, addition, and output.


#include <stdio.h>

// Function prototypes
    int getInput();
    int addNumbers(int num1, int num2);
    void displayResult(int result);

int main() {
    // Get input from the user
    int number1 = getInput();
    int number2 = getInput();

    // Perform addition
    int sum = addNumbers(number1, number2);

    // Display the result
    displayResult(sum);
    return 0;
}


// Function to get input from the user
    int getInput() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    return num;
}

// Function to add two numbers
    int addNumbers(int num1, int num2) {
    return num1 + num2;
}

// Function to display the result
    void displayResult(int result) {
    printf("The sum is: %d\n", result);
}


30 Jul 2014

CALL BY VALUE PROGRAM IN C

Program of Call by value for swapping two values

#include<stdio.h>
void swap(int a, int b)
{
  int temp;
  temp=a;
  a=b;
  b=temp;
 }
void main()
{
   int a=5;
   int b=10;
   /*printf("c-programcodes.blogspot.in");*/
   swap(a,b);  /*the values goes out of scope  after the function is executed*/
   printf("values are not swapped");
   printf("a=%d\n",a);
   printf("b=%d",b)
}
==OUTPUT==

values are not swapped
a=5
b=10

17 Oct 2013

C Program to remove all characters except string

Q) Write a C program to remove the characters in String except alphabets.

Sol)
 
#include<stdio.h>

int main()
{
  char line[150];
  int i,j;  

  printf("Enter a string:") ;
  gets(line);

  for(i=0; line[i]!='\0' ; ++i)
   {
      while(!((line[i]>='a' && line[i]<='z') || (line[i]>='A' && line[i]<='Z' || line[i] == '\0')))
           {
                for(j=i; line[j]!='\0';++j)
                        {
                                  line[j] = line[j+1];
                         }
                line[j]='\0' ;
            }
   }
  printf("Output String is :  ")
  puts(line);
  return 0;
}


                                  :: OUTPUT ::  

Enter a string: G-="o";][d 
Output String is: God

30 Jul 2013

PROGRAM TO PRINT SPIRAL MATRIX

Q) Write a program to print spiral matrix .


Sol)

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

void main()
{
      int m[20][20],i,j;
      int lc,hc,lr,hr,r,c,cnt ;
      clrscr();

     printf("www.c-programcodes.co.in");
     printf("\n Enter r & c ::") ;
     scanf("%d%d" ,&r, &c) ;;
    
     cnt=1;
     lr=0, lc =0 ;
     hr= r-1;
     hc = c-1;
    while(lr<=hr && lc <= hc )
        {
            i = lr;
            for(j=lc;j<= hc ; j++)
                 {
                         m[i][j] = cnt++ ;
                         j= hc;
         
                         for(i=lr+1;i<=hr)
                             {
                                 m[i][j]= cnt++;
                                 if( lr != hr)
                                      {
                                         i =hr;
              
                                         for(j=hc-1;j>=lc;j--)
                                               m[i][j] = cnt++ ;
                                     }
                                 if(lc != hc)
                                     {
                                         j =hc;
                                         for(i=hr-1;i>lr;i--)
                                                m[i][j] = cnt++ ;
                                     }
                          }
                }
        lr++;
        lc++;
        hr--;
        hc--;
       }

     printf("\n Spirally filled matrix is \n");
     for(i=0;i<r ;  i++)
     {
         for(j=0;j<c;j++)
           {
                   printf("%4d", m[i][j]) ;
                   printf("\n") ;
            }
      }
}

19 Jun 2013

OPEN a FILE

Q) Write a C program to open a file


Sol)

#include<stdio.h>
#include<stdlib.h>

int main()
{

   ch, file_name[24];
   FILE *fp;

    printf("c-programcodes.blogspot.in");
    printf("Enter the name of file you wish to see\n");
    get(file_name,"r");      //read mode

    if(fp==NULL)
       {
              perror("Error while opening the file \n") ;
              exit(EXIT_FAILURE) ;
       } 
    
    printf("The contents of %s file are: \n", file_name);

    while((ch=fgetc(fp)) != EOF)
             printf("%c",ch) ;
  
    fclose(fp);
    return 0;

}

16 May 2013

PROGRAM TO MAKE A FISH

Q) Write a program to make a FISH.


Sol)

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

Void main ()
 {

       int gd=DETECT, gm ; 
       initgraph (&gd,&gm, "     "); 

       ellipse (200, 200, 0, 360, 50, 30);  
       line(250, 200, 280, 170);
       line(280, 170, 280, 170);
       line(280, 230, 250, 200);
       circle(160, 190, 3); 

       getch (); 
       closegraph();

 }

2 Apr 2013

Print Prime Number Pyramid

Q) Write a program to print the PRIME NUMBER PYRAMID.


Sol)

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

int prime(int num) ;
void main()
{
     clrscr();
     int i,j ;
     int number=2;
     printf("c-programcodes.blogspot.in")
     for(i=0;i<5;i++)
        {
              printf("\n");
              for(j=0;j<=i;j++)
                 {
                      while(!prime(number)) ;
                         {
                             number++;
                          }
                       printf("%d\t", number++)}
          }
      getch();
}

int prime(int num)
{
        int i, flag ;
        for(i=2;i<num;i++)
           {
                if(num%i!=0)
                      flag=1;
                else
                  {
                       flag=0;
                       break ;
                   }
              }
         if(flag==1 || num==2)
                return(1) ;
         else
                return(0);
 }

                             ===OUTPUT===
c-programcodes.blogspot.in
2

3     5

7     11    13

17   19    23    29

31    37    41    47

1 Mar 2013

Simple Linked List

Q) A C program of simple Linked List without any user defined function.


SOl)

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

void main()
{
   struct node
     {
          int data;
          struct node*next;
     };

   typedef struct node node ;
   int d ;
   node *start=NULL, temp, *save ;
   int option ;
     
   printf("Enter the data for the node\n ");
   scanf("%d",&d) ;
   
   start=(node*)malloc(sizeof (node)) ;
   start->data ;
   start->next= NULL ;
   save = start ;
   
   printf("Do you want to enter more data \n" ) ;
   printf("Press 1 for yes otherwise 0 \n ");
   scanf("%d",&option) ;

   while(option==1)
     {
         printf("Enter the data for the node\n ") ;
         scanf("%d", &d) ;

         temp = (node*)malloc(sizeof (node)) ;
         temp->data=d;
         temp->next= NULL ;
         save->next= temp ;
         save=temp;

         printf("Do you want to enter more data \n") ;
         printf("Press 1 for yes otherwise 0 \n ");
         scanf("%d", &option) ;
 
    }

  printf("The Linked List is \n") ;
  
  while(start!= NULL)
      {
           printf("%d->", start->data);
           start = start->next ;
      }
   getch();
}

28 Feb 2013

Convert Binary to Octal Number

Q)Write a C program to convert the BINARY NUMBER to OCTAL NUMBER


Sol)

#include<stdio.h>

int main( )
{

    long int binary_no, octal_no=0, j=1, rem;
   
    printf("Enter any Binary number: " ) ;
    scanf("%ld", &binary_no) ;

    while(binary_no!=0)
       {
              rem=binary_no%10 ;
              octal_no=octal_no+rem*j ;
              j=j*2;
              binary_no=binary_no/10 ;
       }

     printf("Equivalent octal value: %ld", octal_no) ;
     return 0;
}

22 Feb 2013

Sum of diagonal elements in Matrix

Q)Write a C program to find the sum of diagonal elements in  matrix.
Hint: 
        1    2    3
        4    5    6
        7    8    9
First diagonal elements are 1,5,9
Second diagonal elements are 3,5,7.

Sol)
 

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

int main()
{
    int mat[MAX][MAX],row,col ;
    int i,j,d1=0,d2=0 ;

    printf("Enter no. of rows and columns : ") ;
    scanf("%d%d",&row,&col) ;

    printf("Enter the elements of matrix: \n") ;
    if(row==col)
       {
             for(i=0; i<row; i++)
                   {
                         for(j=0; j<col; j++)
                                 scanf("%d",&mat[i][j]) ;
                     }
              for(i=0, j=col-1; i<row || j>=0; i++,j--)
                     {
                             d1=d1+mat[i][i] ;
                             d2=d2+mat[i][j] ;
                      }
            
               printf("\nThe sum of first diagonal elements : %d", d1) ;
               printf("\n The sum of second diagonal elements : %d", d2) ;
         }
     else
          {
                printf("Rows and Columns are not equal !!") ;
                printf("\n Try again !!") ;
           }

      getch();
      return 0;
}

                                    :::::OUTPUT::::

Enter no. of rows and columns : 3  2
Enter the elements of matrix:
Rows and Columns are not equal !!
Try again !!
  
Enter no of rows and columns : 3  3
Enter the elements of matrix:
1   2   3
4   5   6
7   8   9
The sum of first diagonal elements : 15
The sum of second diagonal elements : 15

20 Feb 2013

Count the number of digits using while loop

Q) C program to count the number of digits in a number using while loop.

Sol)

#include<stdio.h>

int main()
{
    int num,count=0;

   printf("Enter a number : ") ;
   scanf("%d",&num);
   
   while(num)
      {
          num=num/10;
          count++ ;
       }
   
   printf("\nTotal digits is :  %d",count) ;
   return 0;
}

15 Feb 2013

Subtraction of two matrices

Q)Write a C program for SUBTRACTION of two matrices.


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

int main()
{
   int a[3][3],b[3][3],c[3][3],i,j ;
   printf("Enter the first matrix->") ;
   for(i=0;i<3;i++)
      {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&a[i][j]) ;
                }
        }
     printf("\nEnter the Second matrix->");
     for(i=0;i<3;i++)
       {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&b[i][j]) ;
                }
        }
     for(i=0;i<3;i++)
         {
              for(j=0;j<3;j++) 
                  {
                       c[i][j]=a[i][j]-b[i][j] ;
                   }
          }
     printf("\nResultant matrix after Subtraction->\n ") ;
     for(i=0;i<3;i++)
      {    printf("\n");
            for(j=0;j<3;j++)
               {
                   printf("%d\t",c[i][j]) ;
                }
       }
    return 0; 
}

Addition of two matrices

Q) Write a C program for addition of two matrices using arrays.


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

int main()
{
    int a[3][3],b[3][3],c[3][3],i,j ;
    printf("Enter the first matrix->") ;
    for(i=0;i<3;i++)
      {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&a[i][j]) ;
                }
       }
    printf("\nEnter the second matrix->") ;
    for(i=0;i<3;i++)
      {
            for(j=0;j<3;j++)
               {
                    scanf("%d",&b[i][j]) ;
                }
       }
    
    for(i=0;i<3;i++)
       {
            for(j=0;j<3;j++)
               {
                  c[i][j]=a[i][j]+b[i][j] ;
                }
        }
     printf("\nThe addition of two matrix is -> \n ") ;
     for(i=0;i<3;i++)
       {    printf("\n") ;
             for(j=0;j<3j++)
                {
                     printf("%d\t",c[i][j]) ;
                 }
        }
      return 0;
}  

2 Feb 2013

Print ASCII table

Q.) Write a C- program to print the ASCII table.

Sol)

#include<stdio.h>

int main()
{
    int i ; 

    for(i=0;i<=255;i++)
           {
                   printf("ASCII values of character %c: %d \n,i,i") ;
            }
    return 0 ;
 }

1 Feb 2013

String concatenation without using strcat

Q.) Write a C-Program to concatenate the strings.


Sol)

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

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

     printf("Enter first String : ") ;
     scanf("%s",str1) ;

     printf("Enter second String : ") ;
     scanf("%s",str2) ;

     strconcat(str1,str2) ;

     printf("String after concatenation : %s",str1) ;

     return 0 ;
}

void strconcat(char str1[], char str2[])
{
    int i=0,j=0;

    while(str[i]!='\0')
                i++ ;


    while(str2[j]!='\0')
          {
                str1[i]=str2[j] ;
                i++; j++;
          }
     str1[i]='\0' ;
}     

PASCAL TRIANGLE USING C PROGRAM

Q.) Write a C program to print PASCAL Triangle without using array
                                                or
      Write a C program to print PASCAL Triangle using for loop.

Sol )

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

long fact(int) ;
int main()
{
      int line,i,j ;
      printf("Enter the number of lines : ") ;
      scanf("%d",&line) ;
   
      for(i=0;i<line;i++)
          {
               for(j=0;j<line-i-1;j++)
                         printf("  ");

               for(j=0;j<=i;j++)
                         printf("%ld ",fact(i)/(fact(j))*fact(i-j))) ;
               printf("\n") ;
           }
       return 0;
}

long fact(int num)
{
          long f=1;
          int i=1;
          while(i<=num)
             {
                   f=f*i ;
                   i++ ;
              }
          return f;
}

                                :: SAMPLE OUTPUT ::
Enter the number of lines : 6
                           1
                       1       1
                     1    2    1
                  1    3    3   1
                1   4     6    4   1
              1  5  10    10   5  1

To print the FLOYD's TRIANGLE

Q.) Write a C-program to print Floyd's Triangle .
                               or
        How to print Floyd's Triangle in C.

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

int main()
{
   int i,j,r,k=1 ;
   
   printf("Enter the range : ") ;
   scanf("%d",&r) ;
   
   printf("FLOYD's TRIANGLE \n\n") ;
   for(i=1;i<=r;i++)
        {
            for(j=1;j<=i;j++,k++)
                       printf("%d",k) ;
             printf("\n") ;
         }
    return 0 ;
}  

Armstrong number or not

Q.) Write a C program to check whether the number is ARMSTRONG NUMBER or NOT .

Sol)

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

int main()
{
    int num,r,sum=0,temp ;
    
    printf("Enter a number: ") ;
    scanf("%d",&num) ;
    
    temp=num ;
    while(num!=0)
         {
               r=num%10;
               num=num/10;
               sum=sum+(r*r*r) ;
          }

     if(sum==temp)
                 print("%d is an ARMSTRONG NUMBER",temp) ;
      else
                 printf("%d is not an ARMSTRONG NUMBER", temp) ;
      return 0 ;
  }

31 Jan 2013

C-program to find PERFECT NUMBER

Q.) Find out the perfect number using C- program .


Sol)

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

int main( )
{
    int n, i=1, sum=0;
    printf("Enter a Number : ") ;
    scanf("%d",&n) ;
    
    while(i<n)
      {
           if(n%i==0)
                 {  sum=sum+i ; }
           i++ ;
       }
    if(sum==n)
           printf("\n %d is a perfect number", i) ;
    else
           printf("\n %d is not a perfect number", i)  ;
    return 0;
}

                     :: OUTPUT ::
      
Enter a Number : 6
6 is perfect number