Pages

Wednesday, December 2, 2015

C Program to find sum of minor diagonal

Program

#include<stdio.h>
main()
{
int arr[3][3],i,j,sum=0;
printf("enter the 3*3 matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0,j=2;i<3;i++,j--)
{
sum+=arr[i][j];

}
printf("Sum= %d",sum);
}

Output

enter the 3*3 matrix
5 3 6
2 6 8
2 5 7
Sum= 14

Sunday, November 29, 2015

C Program to sort array using pointer

Program

#include<stdio.h>        //program to sort array using pointer
void swap(int *px,int *py)
{
int temp;
temp=*px;
*px=*py;
*py=temp;
}
main()
{
int i,j;
int arr[]={45,40,78,69,55,12,5,2};
int *ptr=arr;
printf("Before Sorting array=\n");
for(i=0;i<8;i++)
{
printf("%d\t",*(ptr+i));
}
for(i=0;i<7;i++)
{
for(j=0;j<7-i;j++)
{
if(*(ptr+j)>*(ptr+j+1))
swap((ptr+j),(ptr+j+1));
}
}
printf("\nAfter Sorting array=\n");
for(i=0;i<8;i++)
{
printf("%d\t",*(ptr+i));
}
}

Output

Before Sorting array=
45      40      78      69      55      12      5       2
After Sorting array=
2       5       12      40      45      55      69      78

C Program to handle string using pointer

Program 

#include<stdio.h> //Program to handle string using pointer
main()
{
int i;
char name[]="Rajesh";
char *ptrname;
ptrname=name;
printf("Name= %s\n",name);
printf("Name via ptrname= %s\n",ptrname);
for(i=0;i<(sizeof(name)-1);i++)
{
printf("Name[%d] via ptrname= %c\n",i,ptrname[i]);
}
}

Output

Name= Rajesh
Name via ptrname= Rajesh
Name[0] via ptrname= R
Name[1] via ptrname= a
Name[2] via ptrname= j
Name[3] via ptrname= e
Name[4] via ptrname= s
Name[5] via ptrname= h

Saturday, November 28, 2015

C Program to print E alphabate

Program

#include<stdio.h>
main()
{
int i,j;
for(i=0;i<=6;i++)
{
for(j=0;j<3;j++)
{
if(i==0 || i==6)
printf("**");
else if(i==3 && j<2)
printf("**");
 else if(j==2)
break;
else
{
printf("*");
break;
}
}
printf("\n");
}
}

Output

******
*
*
****
*
*
******

Monday, November 16, 2015

C Program for Queue using array

Program:-

/* Queue implementation using array*/
#include<stdio.h>
#include<process.h>
# define MAX 5

int queue[MAX];
int rear = -1;
int front = -1;
void insert();
void del();
void display();
void main()
{
int choice;

while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Enter correct choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

void insert()
{
int ele;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1)  /*If queue is initially empty */
front=0;
printf("Enter an element : ");
scanf("%d", &ele);
rear=rear+1;
queue[rear] = ele ;
}
}/*End of insert()*/

void del()
{
if (front == -1)
printf("Queue Underflow\n");
else
{
printf("Element deleted from queue is : %d\n", queue[front]);
front=front+1;
if(front>rear)
{
printf("\nQueue is empty\n");
front=rear=-1;
}
}
}/*End of del() */

void display()
{
int i;
if (front == -1)
printf("Queue is empty\n");
else
{
printf("\nQueue is :\n");
for(i=front;i<= rear;i++)
printf("%d  ",queue[i]);
printf("\n");
}
}/*End of display() */


Output:-



1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Enter an element : 10
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Enter an element : 20
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Enter an element : 30
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3

Queue is :
10  20  30
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3

Queue is :
20  30
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 4


C Program for Multiplication of polynomial

Program

#include<stdio.h>

struct poly
{
int coeff, exp, temp_flag;
};
struct poly p1[10],p2[10],p3[10],p4[10];

void main()
{
int t1,t2,t3;
void read_poly(struct poly [],int);
void display_poly(struct poly [],int);
int mult_poly(struct poly [],int,struct poly [],int,struct poly []);
int sort_poly(struct poly [],int,struct poly []);
printf("\nEnter no of terms of first polynomial : ");
scanf("%d",&t1);
read_poly(p1,t1);
display_poly(p1,t1);

printf("\nEnter no of terms of second polynomial : ");
scanf("%d",&t2);
read_poly(p2,t2);
display_poly(p2,t2);
printf("\nMultiplication of polynomial :\n");
t3=mult_poly(p1,t1,p2,t2,p3);
t3=sort_poly(p3,t3,p4);
display_poly(p4,t3);
getch();

}

void read_poly(struct poly A[10],int terms)
{
int i;
printf("\nEnter polynomial\n");
for(i=terms-1;i>0;i--)
{
printf("\n%d term",i+1);
printf("\nEnter coefficient:");
scanf("%d",&A[i].coeff);
printf("\nEnter exponent:");
scanf("%d",&A[i].exp);
A[i].temp_flag=0;
}
printf("\nEnter constant term of polynomial:");
scanf("%d",&A[0].coeff);
A[0].exp=0;
A[0].temp_flag=0;
}

void display_poly(struct poly A[10], int terms)
{
int i;
for(i=terms-1;i>0;i--)
printf("%dX^%d+",A[i].coeff,A[i].exp);
printf("%d\n",A[0].coeff);
}

int mult_poly(struct poly A[10], int terms1, struct poly B[10],int terms2,struct poly C[10])
{
int i,j,k;
k=0;

for(i=0;i<terms1;i++)
{
for(j=0;j<terms2;j++)
{
C[k].exp =A[i].exp +B[j].exp;
C[k].coeff =A[i].coeff * B[j].coeff;
C[k].temp_flag=0;
k++;
}
}
return k;
}

int sort_poly(struct poly A[10], int terms, struct poly B[10])
{
int i,j,k=0;
for(i=0;i<terms;i++)
{
for(j=0;j<terms;j++)
{
if(i!=j)
{
if(A[j].temp_flag ==0)
{
if(A[i].exp == A[j].exp)
{
B[k].exp =A[i].exp;
B[k].coeff =A[i].coeff +A[j].coeff;
A[i].temp_flag=1;
A[j].temp_flag =1;
k++;
}
}
}
}
if(A[i].temp_flag ==0)
{
B[k].exp =A[i].exp;
B[k].coeff =A[i].coeff;
A[i].temp_flag =1;
k++;
}
}
return k;
}

C Program for addition of polynomial

Program

//Addition of polynomial using array
#include<stdio.h>
void main()
{

int a[5],b[5],c[10],d1,d2,d;
void read_poly(int [],int);
void display_poly(int [], int);
void add_poly(int [],int,int [],int,int []);


printf("\nEnter degree of first polynomial: ");
scanf("%d",&d1);
read_poly(a,d1);
display_poly(a,d1);

printf("\nEnter degree of second polynomial: ");
scanf("%d",&d2);
read_poly(b,d2);
display_poly(b,d2);

if(d1>d2)
d=d1;
else
d=d2;

add_poly(a,d1,b,d2,c);
display_poly(c,d);
getch();
}

void read_poly(int poly[5],int degree)
{
int i;
printf("\nEnter first polynomial\n");
for(i=degree;i>0;i--)
{
printf("X^%d : ",i);
scanf("%d",&poly[i]);
}
printf("\nEnter constant term of first polynomial : ");
scanf("%d",&poly[0]);
}

void display_poly(int poly[10],int degree)
{
int i;
for(i=degree;i>0;i--)
printf("%dX^%d+",poly[i],i);
printf("%d\n",poly[0]);
}

void add_poly(int poly1[5], int degree1, int poly2[5], int degree2,int poly3[10])
{
int i,j,k;
k=1;
if(degree1>=degree2)
{
for(i=1;i<=degree2;i++)
{
poly3[k++]=poly1[i]+poly2[i];
}
for(j=i;j<=degree1;j++)
{
poly3[k++]=poly1[j];
}
poly3[0]=poly1[0]+poly2[0];
}

if(degree2>degree1)
{
for(i=0;i<=degree1;i++)
{
poly3[k++]=poly1[i]+poly2[i];
}
for(j=i;j<=degree2;j++)
{
poly3[k++]=poly1[j];
}
poly3[0]=poly1[0]+poly2[0];

}
}

C Program to swap two integer using pointer

Program

#include<stdio.h>

void swap(int *num1, int *num2)
 {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}

int main()
{
int num1, num2;

printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);

swap(&num1, &num2);

printf("\nFirst number  : %d", num1);
printf("\nSecond number : %d", num2);

return (0);
}

C Program to convert octal to decimal

Program 

/* C programming source code to convert either octal to decimal or decimal to octal according to data entered by user. */

#include <stdio.h>
#include <math.h>
int decimal_octal(int n);
int octal_deciaml(int n);
int main()
{
   int n;
   char c;
   printf("Instructions:\n"); 
   printf("1. Enter alphabet 'o' to convert decimal to octal.\n");
   printf("2. Enter alphabet 'd' to convert octal to decimal.\n");
   scanf("%c",&c);
   if (c =='d' || c == 'D')
   {
       printf("Enter an octal number: ");
       scanf("%d", &n);
       printf("%d in octal = %d in decimal", n, octal_decimal(n));
   }
   if (c =='o' || c == 'O')
   {
       printf("Enter a decimal number: ");
       scanf("%d", &n);
       printf("%d in decimal = %d in octal", n, decimal_octal(n));
   }
   return 0;
}

int decimal_octal(int n) /* Function to convert decimal to octal */
{
    int rem, i=1, octal=0;
    while (n!=0)
    {
        rem=n%8;
        n/=8;
        octal+=rem*i;
        i*=10;
    }
    return octal;
}

int octal_decimal(int n) /* Function to convert octal to decimal */
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(8,i);
        ++i;
    }
    return decimal;
}

C Program for sum of all elements of matrix

Program

#include<stdio.h>
int sum(int [10][10]);
int i, j, row, col;
void main()
{
int mat[10][10] ;
int s;
printf("Enter the order of the matrix : ") ;
scanf("%d %d", &row, &col) ;
printf("\nEnter the elements of the matrix : \n\n") ;
for(i = 0 ; i < row ; i++)
  for(j = 0 ; j < col ; j++)
scanf("%d", &mat[i][j]) ;
 s=sum(mat);
printf("\nThe sum of the elements in the matrix is : %d",s) ;

}
int sum(int matrix[10][10])
{
int sum=0;
for(i = 0 ; i < row ; i++)
 for(j = 0 ; j < col ; j++)
sum = sum + matrix[i][j] ;

return sum;
}

C Program to reverse no. using recursion

Program

#include<stdio.h>
int reverse_function(int);

int main()
{
int num,reverse_number;

//User would input the number
printf("\nEnter any number:");
scanf("%d",&num);

//Calling user defined function to perform reverse
reverse_number=reverse_function(num);
printf("\nAfter reverse the no is :%d",reverse_number);
return 0;
}
int sum=0,rem;
int reverse_function(int num)
{
if(num){
rem=num%10;
sum=sum*10+rem;
reverse_function(num/10);
}
else
return sum;

}

C Program for sum of digits using Recursion

Program

#include<stdio.h>
int findsum(int);
void main()
{
  int num,x;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);

}

int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
 else
return s;
}

Output

Enter a number: 235
Sum of the digits of 235 is: 10

C Program for Factorial using Recurtion Function

Program

#include<stdio.h>
int fact(int);
void main()
{
  int num,f;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  f=fact(num);
  printf("\nFactorial of %d is: %d",num,f);
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}

Output

Enter a number: 5

Factorial of 5 is: 120

Sunday, November 15, 2015

C Program to reverse the string without using library function

Program

#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
int i=0,j=0;
printf("Enter the String ");
gets(str);
while(str[i]!='\0')
i++;
i--;
printf("Reverse of string =\n");
while(i>=0)
{
printf("%c",str[i]);
i--;
}
}


Output

Enter the String Rajesh mishra
Reverse of string =
arhsim hsejaR

C Program for Multiplication(Product) of matrices(Matrix)

Program

#include <stdio.h>

void main()
{
  int row,col,r1,c1,r2,c2,k;
  int first[10][10],second[10][10],multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d",&r1,&c1);
  printf("Enter the elements of first matrix\n");

  for(row=0;row<r1;row++)
for(col=0;col<c1;col++)
scanf("%d", &first[row][col]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d",&r2,&c2);

if(c1!=r2)
printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
printf("Enter the elements of second matrix\n");

for(row=0;row<r2;row++)
for(col=0;col<c2;col++)
scanf("%d", &second[row][col]);

for(row=0;row<r1;row++)
{
for(col=0;col<c2;col++)
{
multiply[row][col]=0;

for(k=0;k<r2;k++)
multiply[row][col]+= first[row][k]*second[k][col];
}
}
printf("Product of entered matrices:-\n");

for(row=0;row<r1;row++)
{
for(col=0;col<c2;col++)
 printf("%d\t", multiply[row][col]);

printf("\n");
}
}

}

Output

Enter the number of rows and columns of first matrix
2
3
Enter the elements of first matrix
1 2 3
4 5 6
Enter the number of rows and columns of second matrix
3
2
Enter the elements of second matrix
1 2
3 4
5 6
Product of entered matrices:-
22      28
49      64

C Programming for Transpose of matrices(Matrix)

Program

#include <stdio.h>

void main()
{
int m, n, col, row, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);

printf("Enter the elements of matrix\n");

for (row=0;row<m;row++)
for(col=0;col<n;col++)
scanf("%d",&matrix[row][col]);

for (row=0;row<m;row++)
for(col=0;col<n;col++)
transpose[col][row] = matrix[row][col];

printf("Transpose of entered matrix :-\n");

for (row=0;row<n;row++)
{
for(col=0;col<m;col++)
printf("%d\t",transpose[row][col]);
printf("\n");
}
}

Output

Enter the number of rows and columns of matrix
3
2
Enter the elements of matrix
1 2
3 4
5 6
Transpose of entered matrix :-
1       3       5
2       4       6

C Program to find min and max of given numbers using array

Program

#include<stdio.h>
void main()
{
int a[10];
int i,min,max;
printf("Enter 10 nos : ");
for(i=0;i<10;i++)
scanf("%d",&a[i]);

max=min=a[0];

for(i=1;i<10;i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}

printf("Min= %d, Max= %d",min,max);

}

Output

Enter 10 nos : 3
67
95
485
525
214
65
75
96
24
Min= 3, Max= 525

C Program to find min and max of given numbers without using array

Program

#include<stdio.h>
struct array
{
int no;
};
void main()
{
struct array a[10];
int i,min,max;
printf("Enter 10 nos : ");
for(i=0;i<10;i++)
scanf("%d",&a[i].no);

max=min=a[0].no;

for(i=1;i<10;i++)
{
if(min>a[i].no)
min=a[i].no;
if(max<a[i].no)
max=a[i].no;
}

printf("Min= %d, Max= %d",min,max);

}

Output
Enter 10 nos : 22
65
75
24
52
65
35
48
67
25
Min= 22, Max= 75

Saturday, November 14, 2015

C Program to Find the sum of negative and positive numbers

  /*

     * C program to read N integers into an array A and

     * a) Find the sum of negative numbers

     * b) Find the sum of positive numbers

     * c) Find the average of all numbers

     * Display the results with suitable headings

     */

    #include  <stdio.h>

    #define MAXSIZE 10

     

    void main()

    {

        int array[MAXSIZE];

        int i, num, negative_sum = 0, positive_sum = 0;

        float total = 0.0, average;

     

        printf ("Enter the value of N \n");

        scanf("%d", &num);

        printf("Enter %d numbers (-ve, +ve and zero) \n", num);

        for (i = 0; i < num; i++)

        {

            scanf("%d", &array[i]);

        }

        printf("Input array elements \n");

        for (i = 0; i < num; i++)

        {

            printf("%+3d\n", array[i]);

        }

        /*  Summation starts */

        for (i = 0; i < num; i++)

        {

            if (array[i] < 0)

            {

                negative_sum = negative_sum + array[i];

            }

            else if (array[i] > 0)

            {

                positive_sum = positive_sum + array[i];

            }

            else if (array[i] == 0)

            {

                ;

            }

            total = total + array[i] ;

        }

        average = total / num;

        printf("\n Sum of all negative numbers =  %d\n", negative_sum);

        printf("Sum of all positive numbers =  %d\n", positive_sum);

        printf("\n Average of all input numbers =  %.2f\n", average);

    }


output :-

Enter the value of N
10
Enter 10 numbers (-ve, +ve and zero)
-8
9
-100
-80
90
45
-23
-1
0
16
Input array elements
 -8
 +9
-100
-80
+90
+45
-23
 -1
 +0
+16

Sum of all negative numbers =  -212
Sum of all positive numbers =  160

Average of all input numbers =  -5.20

Friday, November 13, 2015

C Program for sum of digits using function

Program

//program for sum of digits using function
#include<stdio.h>
int sum(int);
void main()
{
int no,ans;
printf("Enter the no.");
scanf("%d",&no);
ans=sum(no);
printf("sum of digits=%d\n",ans);
}
int sum(int n)
{
int d,sum=0;
while(n>0)
{
d=n%10;
n=n/10;
sum=sum+d;
}
return(sum);
}


Output
Enter the no.561
sum of digits=12

C Program for prime no. using function

Program

#include<stdio.h>
int prime(int);
void main()
{
int n,ans;
printf("enter the number:");
scanf("%d",&n);
ans=prime(n);
if(ans==1)
  printf("%d is a prime number.\n",n);
else
  printf("%d is not a prime number.\n",n);
}
int prime(int no)
{
int i;
for(i=2;i<no;i++)
{
if(no%i==0)
        return 0;
        }
return 1;
}

Output
enter the number:7
7 is a prime number.

C Program for factorial using function

Program

//program for factorial
#include<stdio.h>
int facto(int);
void main()
{
int no,fact;
printf("Enter the no.");
scanf("%d",&no);
fact=facto(no);
printf("Factorial of %d=%d\n",no,fact);
}
int facto(int A)
{
int i,f=1;
for(i=1;i<=A;i++)
f=f*i;
return(f);
}


Output
Enter the no.5
Factorial of 5=120

C Program to convert a no. into words

Program

#include<stdio.h>
#include<process.h>

void main()
{
char *single_digits[] = { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty",
 "sixty", "seventy", "eighty", "ninety"};
  char *tens_power[] = {"hundred", "thousand"};
  int nos,temp,rem;
  int len=0;
  int arr[4];
  printf("enter max 4 digit no.");
  scanf("%d",&nos);
  temp=nos;

  while(temp!=0)
  {
rem=temp%10;
arr[len]=rem;

len++;
temp=temp/10;
  }

if (len == 1)
{
 printf("%s\n", single_digits[nos]);
 exit(0);
}

 while (len > 0)
 {
 /* Code for first 2 digits */
 if (len >= 3)
{
if (arr[len-1] > 0)
{
printf("%s ", single_digits[arr[len-1]]);
printf("%s ", tens_power[len-3]); // here len can be 3 or 4
}
--len;
 }

 /* Code path for last 2 digits */
 else
 {
printf("%s ",tens_multiple[arr[1]]);
printf("%s ", single_digits[arr[0]]);
exit(0);
}
 }

}

Output
enter max 4 digit no.9999
nine thousand nine hundred ninety nine

C Program for lcm and gcd [simplified]

// Program for The greatest common divisor (GCD) and The least common multiple (LCM)

#include<stdio.h>
void main()
{
int a,b,i,gcd;
printf("Enter the 2 numbers:");
scanf("%d %d",&a,&b);
if(a>b)
{

for(i=1;i<a;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}
}
if(a<b)
{

for(i=1;i<b;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}
}
printf("gcd = %d and lcm = %d",gcd,(a*b)/gcd);


}

Output
Enter the 2 numbers:
90
50
gcd = 10 and lcm=450