Menus

Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Saturday, 22 October 2022

How to Swap Two Arrays In C Language

 

#include<stdio.h>

void main()

{


  int a[5] = {5,6,7,8,9};

  int b[5] = {1,2,3,4,5};

  

  int i,swap;


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

  {


    swap = a[i];

    a[i] = b[i];

    b[i] = swap;


  } 


  printf("A = ");

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

    printf("%d ",a[i]);


  printf("\nB = ");

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

    printf("%d ",b[i]); 


  printf("\n");

}







Saturday, 5 March 2022

C language MCQ (Multiple Choise Questions)

Art of C Programming


1 . Address stored in the pointer variable is of type __________
 
 
 
 

 

2. "*" is called as ___________
 
 
 
 


3. Comment on the following pointer declaration?

     int *ptr, p;
 
 
 
 


4. Which of the following are correct syntaxes to send an array as a parameter to function

 
 
 
 


5. Which of the following is not possible in C?

 
 
 
 


 

Answers

1. Integer

2.  

3. ptr is a pointer to integer, p is not.

4. func(&array);

5. None of the mentioned



Tuesday, 20 April 2021

C program to Count the Number of Digits





#include<stdio.h>


void main(){


int n,count=0;


printf("Enter an number ");

scanf("%d",&n);


while(n>0)

{

  

  n=n/10; 

  count++;

 

}


printf("Number of digits %d", count);

 


}


 

Thursday, 23 April 2020

Program to add two matrices in C


#include<stdio.h>

void main()
{

int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;

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

printf("Enter the rows and column of second matrix : ");
scanf("%d%d",&p,&q);

if((m==p) && (n==q))
{

printf("Enter first matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 scanf("%d",&a[i][j]);
}
}


printf("Enter second matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 scanf("%d",&b[i][j]);
}
}


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 c[i][j] = a[i][j] + b[i][j];
}
}


printf("Sum of matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 printf(" %d",c[i][j]);
}
printf("\n");
}


}
else
{
  printf("\nInvalid rows and columns\n");
}

}








Wednesday, 22 April 2020

C program to print two dimensional array





#include<stdio.h>

void main()
{

int a[10][10],i,j,m,n;

printf("Enter size of rows and columns of array : ");
scanf("%d%d",&m,&n);

printf("Enter elements in the array :");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
  printf("a[%d][%d] = ",i,j);
  scanf("%d",&a[i][j]);
}
}


printf("Array elements \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
  printf(" %d",a[i][j]);
}
printf("\n");
}


}




Tuesday, 21 April 2020

C program for add two one dimensional array


#include<stdio.h>

void main()
{

int a[10],b[10],c[10];
int n,i;

printf("Enter the size : ");
scanf("%d",&n);

printf("Enter first array \n");

for(i=0;i<n;i++)
 scanf("%d",&a[i]);

printf("Enter second array \n");

for(i=0;i<n;i++)
 scanf("%d",&b[i]);


for(i=0;i<n;i++)
 c[i]=a[i]+b[i];

printf("Sum of arrays : ");
for(i=0;i<n;i++)
 printf("\n%d",c[i]);


}




Thursday, 2 April 2020

C Program to find largest element in an array

#include<stdio.h>

void main()
{

int arr[100],size,i,max,location;

printf("Enter the number of elements in an array : ");
scanf("%d",&size);

for(i=0;i<size;i++)
{
printf("Enter array value ");
scanf("%d",&arr[i]);
}


max= arr[0];

for(i=1;i<size;i++)
{
if(arr[i]>max)
{
max=arr[i];
location=i+1;
}
}

printf("Maximum element in location %d and its value is %d \n",location,max);


}




Wednesday, 1 April 2020

C Program to reverse the number


#include<stdio.h>

int reversenum(int n)
{

int rev=0,a;

while(n>0)
{
a=n%10;
n=n/10;
rev=rev*10+a;
}

return rev;


}


void main()
{

int n,rev;
printf("Enter the number : ");
scanf("%d",&n);

rev = reversenum(n);

printf("The reverse of the number is %d \n",rev);


}





Check whether a Number is Palindrome or Not - C Program


#include<stdio.h>

void main()
{

int n,m,a,r=0;

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

m=n;

while(n>0)
{
a=n%10;
n=n/10;
r=r*10+a;
}


if(m==r)
 printf("%d is palindrome \n",m);
else
 printf("%d is not palindrome \n",m);

}




Tuesday, 31 March 2020

To check given number is armstrong number or not



#include<stdio.h>

void main()
{

int n,m,b=0,a;

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

m=n;

while(n>0)
{

a=n%10;
n=n/10;
b=b+(a*a*a);

}

if(m==b)
  printf("%d is amstrong\n",m);
else
  printf("%d is not amstrong\n",m);


}






Friday, 27 March 2020

Write C program to find factorial of a number


#include<stdio.h>

void main()
{


int n,fact;

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

fact=1;

while(n!=0)
{
fact = fact * n;
n--;
}

printf("Factorial of the nmber is %d\n",fact);


}