pages

Showing posts with label pointer. Show all posts
Showing posts with label pointer. Show all posts

Sunday, 8 August 2021

program to find transpose of the matrix in c-programming language | c-tutorial

Transpose of the matrix 

This C program is to find transpose of a matrix.

The new matrix obtained by interchanging the rows and columns of the original 
matrix is called as the transpose of the matrix. If A = [aij] be an m × n matrix,
 then the matrix obtained by interchanging the rows and columns of A would be the 
transpose of A. of It is denoted by A′or (AT).  

]

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[3][3],i,j;
    printf("enter the value of the matrix:-");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("\n****************\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%3d",a[i][j]);
        }
        printf("\n");
    }
    printf("\n the transpose of the matrix is :-\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            printf("%3d",a[j][i]);
        }
        printf("\n");
    }
    getch();
}


output:-






Monday, 14 June 2021

c-program to add two number by using pointer | call by reference | c-programming

program to add two number by pointer in c language

In this program we will now takes two number and add them by using call by reference (pointer)
 Call by Reference :- when address are passed to the called function.


#include<stdio.h>
#include<conio.h>
int add(int *,int *);
void main()
{
         int a,b,sum=0;
         printf("enter the number:-");
         scanf("%d %d",&a,&b);
         sum=add(&a,&b);
         printf("\n the sum of the two number is %d",sum);
         getch();
}
int add(int *x,int *y)
{
         int s;
         s=*x+*y;
         return(s);
}

output :- 


















Sunday, 30 May 2021

c-program to calculate the sum of five number by using pointer in c-programming| c-language

program to calculate the sum of all five number by using pointer in c-programming.

In this program ,we will now  takes input five numbers by the user and print the sum of all five number by using pointer in c-programming.



#include<stdio.h>
#include<conio.h>
void main()
{
         int i,a[5],sum=0;
         int *p;
         p=&a[0];
         printf("enter the five numbers:-");
         for(i=0;i<=4;i++){
                  scanf("%d",p);
                  p++;
         }
         p=&a[0];
         printf("\n the number are follow:-");
         for(i=0;i<5;i++){
                  printf("%d ",*p);
                  sum=sum+*p;
                  p++;
         }
         printf("\n the sum of the numbers is %d",sum);
         getch();
}

output-





For more c-programming:-


Wednesday, 26 May 2021

c-program to print the reverse of the number by using pointer | c-programming

program to reverse the number by using pointer in c-programming

In this program ,we will now  takes input as a number and print the reverse of the number by using pointer in c-programming.



 //program to reverse of the number by using pointer
#include<stdio.h>
#include<conio.h>
void main()
{
         int n,rev,num;
         int *n1,*rev1,*num1;
         n1=&n,rev1=&rev,num1=&num;
         *num1=0;
         printf("enter the number:-");
         scanf("%d",&n);
         while(*n1!=0){
                  *rev1=*n1%10;
                  *num1=*num1*10+*rev1;
                  *n1=*n1/10;
         }
         printf("\n the reverse of the number is %d",*num1);
         getch();
}


output:



For more c-programming:-


















Friday, 21 May 2021

c-program to calculate the length of string using pointer | c-programing

 Program to calculate the length of string using pointer in c-programming

In this program , we will now taking  input as a string and calculate the length of string by using pointer.

pointer : pointer is a variables that stores address of another variables.

#include<stdio.h>
#include<conio.h>
void main()
{
         char str[50];
         char *i,count=0;
         i=str;
         printf("enter the string:-");
         gets(str);
         while(*i!='\0'){
                  count++;
                  i++;
         }
         printf("\n the length of the string is %d:-",count);
         getch();
}

output:-



For more c-programming:-















Sunday, 16 May 2021

c-program to swap three number by using pointer in c-languages

 program to swap three number by using  pointer in c-programming:

In this program, we will now takes three number from the user and swap this three number by using pointer.

#include<stdio.h>
#include<conio.h>
int fun(int *,int *,int *);
void main()
{
         int x,y,z;
         printf("enter the three number:-");
         scanf("%d %d %d",&x,&y,&z);
         printf("\n before swapping:x=%d y=%d and z=%d",x,y,z);
         fun(&x,&y,&z);
         printf("\n after swapping: x=%d y=%d and z=%d",x,y,z);
         getch();
}
int fun(int *a,int *b,int *c)
{
         int i;
         i=*b;
         *b=*a;
         *a=*c;
         *c=i;
}


output:-



For more c-programming


















c-program to calculate the area of triangle by using pointer | c-programming

 If the length of the sides of a triangle are denoted by a,b,c and  then what is the area of the triangle by using pointer ?


In  this program , we have to taking  three input i,e sides of the triangles form the user and calculate the area of the triangle .

#include<stdio.h>
#include<conio.h>
void main()
{
         float s=0,a,b,c,area=0;
         float *x,*y,*z,*sum,*area1;
         printf("enter the three number:-");
         scanf("%f %f %f",&a,&b,&c);
         x=&a,y=&b,z=&c,sum=&s,area1=&area;
         s=(a+b+c)/2;
         printf("\n the area of the triangle are follow");
         area=sqrt(s*(s-a)*(s-b)*(s-c));
         printf("\n the solution is %.2f",*area1);
         getch();
}

output :-

                              


For more c-programming:-

Tuesday, 11 May 2021

c-program to print table of any number by using pointer | c-programming

 Program to print the table of natural number by using pointer  in c-programming



In this program we will now taking input n natural number by the user and print table of the natural number  in c-programming. 








#include<stdio.h>
#include<conio.h>
void main()
{
         int n,i;
         int *x,*y;
         x=&n,y=&i;
         printf("\n enter the number:-");
         scanf("%d",&n);
         for(i=1;i<=10;i++){
                  printf("\n%d*%d=%d",*x,*y,(*x)*(*y));
         }
         getch();
}























output:-







For more c-programming:-





























Wednesday, 5 May 2021

c-program to calculate factorial of a number by using pointer | c-programming

program to calculate the factorial of the any number by using pointer 

In this program , we will now calculate the factorial of the number by taking input from the user by using  pointer in  c-programming


#include<stdio.h>
#include<conio.h>
int factorial(int *, int*);
void main()
{
         int fact,num;
         printf("enter the number:-");
         scanf("%d",&num);
         factorial(&num,&fact);
         printf("\n the factorial of the %d is %d",num,fact);
         getch();
}
int factorial(int *n,int *f)
{
         int i;
         *f=1;
         for(i=1;i<=*n;i++)
                  *f=*f*(i);
                  return(*f);
}

output:-



For more c-programming:-






















c-program to calculate the table of any number by using pointer | c-programming

program to calculate the table of any number in c-programming 

In this program ,we will now calculate the table of any number by using pointer in c-program by taking input from the user .


#include<stdio.h>
#include<conio.h>
void main()
{
         int n,i;
         int *x,*y;
         x=&n,y=&i;
         printf("\n enter the number:-");
         scanf("%d",&n);
         for(i=1;i<=10;i++){
                  printf("\n%d*%d=%d",*x,*y,(*x)*(*y));
         }
         getch();
}

output:-
















Tuesday, 4 May 2021

c-program to calculate sum and average of five number by using pointer in c-programming

 program to receives 5 integers and return the sum, average of these numbers by using pointers in c-programming.

In this c-program , we calculate to Find the Sum and Average of a number by Using the Pointer by taking input from the user .

#include<stdio.h>
#include<conio.h>
void main()
{
         float a[5],ave=0,sum=0;
         int i;
         float *pa,*pave=0,*psum=0;
         pa=a;
         pave=&ave,psum=&sum;
         printf("enter the number:-");
         for(i=0;i<5;i++){
                  scanf("%f",(pa+i));
                  *psum=*psum+*(pa+i);
         }
         printf("\nthe sum of the number is %.2f",*psum);
         *pave=*psum/5;
         printf("\nthe average of the number is %.2f",*pave);
         getch();
}

output:-



Another way to solve this problem are follow :-


#include<stdio.h>
#include<conio.h>
void input(int *p,int  size);
void display(int *p,int size);
void main()
{
         int a[5];
         input(a,5);
         display(a,5);
         getch();
}
void input(int *p,int  size)
{
         int i;
         printf("enter %d number",size);
         for(i=0;i<size;i++)
                  scanf("%d",(p+i));
}
void display(int *p,int size)
{
         int i,sum=0;
         float ave=0;
         printf("\nthe number are as follow:-");
         for(i=0;i<size;i++){
                  printf("%d ",*(p+i));
                  ave=ave+*(p+i);
                  sum=sum+*(p+i);
         }
         printf("\n the sum of the number is %d",sum);
         ave=ave/5.00;
         printf("\n \naverage of the number is %.2f",ave);
}

Output :- 




















c-program to swap two number by using pointer | c-programming

program to swap two number by using pointer in c-programming 

In this program ,we will now swap two number by using pointer in c-program by taking input form the user.
When we call the function, we pass the  address of the variable, so this method is called “Call by Reference“.


when we  ask the user to enter the values for variable a and b. We passes the address of variable a and b to the function swap().
Inside the function swap() we take a local variable i. Since address of variable a and b are passed to swap() function , we take 2 pointer variables *x and *y. Pointer variable x holds the address of a and pointer variable y holds the address of b. Using below logic we swap the values present at address a( or x ) and b( or y ).

#include<stdio.h>
#include<conio.h>
int swap(int *,int *);
void main()
{
         int a,b;
         printf("enter the two number:-");
         scanf("%d %d",&a,&b);
         printf("\nbefore swapping:- a=%d and b=%d",a,b);
         swap(&a,&b);
         printf("\nafter swapping:- a=%d and b=%d",a,b);
         getch();
}
int swap(int *x,int *y)
{
         int i;
         i=*x;
         *x=*y;
         *y=i;
}


output:-




For more c-programming:-











Thursday, 29 April 2021

c-program to copy the string without using strcmp fuction by using pointer in while loop

program to copy the string without using strcmp funtion by using pointer. 

In this program, we will now copy the string without using  strcmp function by using pointer by taking input form the user.


#include<stdio.h>
#include<conio.h>
int copy(char *s2,char *s1);
void main()
{
         char str1[50],str2[50];
         printf("enter the string:-");
         gets(str1);
         copy(str2,str1);
         printf("\n first string of str1 is :-%s",str1);
         printf("\n second string of str2 is:- %s",str2);
         getch();
}
int copy(char *s2,char *s1)
{
         while(*s1!='\0'){
                  *s2=*s1;
                  s1++;
                  s2++;
         }
         s2='\0';
}

output:-




For more c-programming:-

1.











c-program to calculate the length of the string by pointer using while loop .

program to calculate the length of the string by pointer using while loop

In this  program ,we will  now calculate the length of the  string by pointer using while loop by taking taking string  as  a input from the user.
pointer :- pointer is  a variable which contain address of another variable.

  • gets() is used to accept string with spaces.
  • the base address is stored in the variable i . Once the base address is obtained in i , *i would  yield  the value at this address .
  • Inside while loop we are going to count single letter and incrementing pointer further till we get null character.

#include<stdio.h>
#include<conio.h>
void main()
{
         char str[50];
         char *i,count=0;
         i=str;
         printf("enter the string:-");
         gets(str);
         while(*i!='\0'){
                  count++;
                  i++;
         }
         printf("\n the length of the string is %d:-",count);
         getch();
}


output:-




For more c-programming:-