0% found this document useful (0 votes)
11 views

c basic interview programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

c basic interview programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

C program to find factorial of a number a=a+b;


b=a-b;
#include <stdio.h> a=a-b;
int main() printf("\n After swapping (second method): A=
{ %d, B= %d\n",a,b);
int num,i;
long int fact; /****Third method using X-Or */
printf("Enter an integer number: "); a^=b;
scanf("%d",&num); b^=a;
fact=1; a^=b;
for(i=num; i>=1; i--) printf("\n After swapping (third method) : A=
fact=fact*i; %d, B= %d\n",a,b);
printf("\nFactorial of %d is = %ld",num,fact);
return 0; /****fourth method (single statement)*/
} a=a+b-(b=a);
Output printf("\n After swapping (fourth method): A=
Enter an integer number: 7 %d, B= %d\n",a,b);
Factorial of 7 is = 5040
2. C program to swap two numbers using four return 0;
different methods }
Given two integer numbers and we have Output
to swap them using different methods in C
language. Enter value of A ? 100
Enter value of B ? 200
The methods that we are going to use in this
program are: Before swapping : A= 100, B= 200
 Using third variable After swapping (First method) : A= 200, B= 100
 Without using third variable
 Using X-OR operator After swapping (second method): A= 100, B= 200
 Using simple statement
#include <stdio.h> After swapping (third method) : A= 200, B= 100
int main()
{ After swapping (fourth method): A= 100, B= 200
int a,b,t;
printf(" Enter value of A ? "); 3. C program to check whether number is
scanf("%d",&a); Palindrome or not
printf(" Enter value of B ? "); #include <stdio.h>
scanf("%d",&b); int main()
{
printf("\n Before swapping : A= %d, B= %d",a,b); int number, revNumber=0, rem=0,tempNumber;
printf("Enter an integer number: ");
/****first method using third variable*/ scanf("%d", &number);
t=a; tempNumber=number;
a=b; while(tempNumber!=0)
b=t; {
printf("\n After swapping (First method) : A= %d, rem=tempNumber%10;
B= %d\n",a,b); revNumber=revNumber*10+rem;
tempNumber/=10;
/****second method without using third }
variable*/
#include <stdio.h>
/* checking if number is equal to reverse number int main()
*/ {
if(revNumber==number) if(!printf("Hello "))
printf("%d is a palindrome.", number); ;
else else
printf("%d is not a palindrome.", number); printf("C\n");
return 0;
return 0; }
} Output
Output Hello C
6. C program to check whether number is Prime
First run: or Not
Enter an integer number: 12321 #include <stdio.h>
12321 is a palindrome. int main()
{
Second run: int tally;
Enter an integer number: 1234 int number;
1234 is not a palindrome. unsigned char flag=0;
4. C program to check whether number is Perfect printf("Enter an integer number : ");
Square or not scanf("%d",&number);
#include <stdio.h>
#include <math.h> for(tally=2; tally<(number/2); tally++)
int main() {
{ if(number%tally ==0)
int num; {
int iVar; flag=1;
float fVar; break;
printf("Enter an integer number: "); }
scanf("%d",&num); }
fVar=sqrt((double)num);
iVar=fVar; if(flag==0)
if(iVar==fVar) printf("\n %d is a prime number.",number);
printf("%d is a perfect square.",num); else
else printf("\n %d is not a prime
printf("%d is not a perfect square.",num); number.",number);
return 0;
return 0; }
} Output
Output First run:
First Run: Enter an integer number: 117
Enter an integer number: 16 117 is not a prime number.
16 is a perfect square. Second run:
Second Run: Enter an integer number: 112
Enter an integer number: 26 112 is not a prime number.
26 is not a perfect square.
7. C program to check whether number is
5. C program to print message (Hello World/Hello Armstrong or not
C) without using any semicolon in program #include <stdio.h>
int main() scanf("%d",&n);
{
int number, sum=0, rem=0,tempNumber; printf("Leap years from 1 to %d:\n",n);
for(i=1;i<=n;i++)
printf("Enter an integer number: "); {
scanf("%d", &number); if(checkLeapYear(i))
printf("%d\t",i);
tempNumber=number; }

while(tempNumber!=0) return 0;
{ }
rem=tempNumber%10; Output
sum=sum + (rem*rem*rem);
tempNumber/=10; Enter the value of N: 100
} Leap years from 1 to 100:
4 8 12 16 20 24 28 32 36
/* checking number is armstrong or not */ 40
if(sum==number) 44 48 52 56 60 64 68 72 76
printf("%d is an Armstrong number.",number); 80
else 84 88 92 96
printf("%d is not an Armstrong 9. C program to convert number from Decimal to
number.",number); Binary
#include <stdio.h>
return 0; int main()
} {
Output int number,cnt,i;
First run: int bin[32];
Enter an integer number: 153 printf("Enter decimal number: ");
153 is an Armstrong number. scanf("%d",&number);
Second run: cnt=0; /*initialize index to zero*/
Enter an integer number: 167 while(number>0)
167 is not an Armstrong number {
8. C program to print all Leap Year from 1 to N bin[cnt]=number%2;
#include <stdio.h> number=number/2;
//function to check leap year cnt++;
int checkLeapYear(int year) }
{ printf("Binary value is: ");
if( (year % 400==0)||(year%4==0 && year%100! for(i=(cnt-1); i>=0;i--)
=0) ) printf("%d",bin[i]);
return 1; return 0;
else }
return 0; Output
} Enter decimal number: 545
Binary value is: 1000100001
int main() 10. C program to convert number from Decimal to
{ Binary
int i,n; #include <stdio.h>
int main()
printf("Enter the value of N: "); {
int number,cnt,i; else
int bin[32]; printf("\n stringCmpi :String are not same.");

printf("Enter decimal number: ");


scanf("%d",&number); printf("\n");
return 0;
cnt=0; /*initialize index to zero*/ }
while(number>0)
{ /******** function definition *******/
bin[cnt]=number%2; int stringCmp (char *s1,char *s2)
number=number/2; {
cnt++; int i=0;
} for(i=0; s1[i]!='\0'; i++)
{
/*print value in reverse order*/ if(s1[i]!=s2[i])
printf("Binary value is: "); return 1;
for(i=(cnt-1); i>=0;i--) }
printf("%d",bin[i]); return 0;
}
return 0;
} /******** function definition *******/
Output int stringCmpi (char *s1,char *s2)
Enter decimal number: 545 {
Binary value is: 1000100001 int i=0,diff=0;
11. C program to compare two string using case for(i=0; s1[i]!='\0'; i++)
and ignoring case without using library function {
#include <stdio.h> if( toupper(s1[i])!=toupper(s2[i]) )
#include <ctype.h> return 1;
int stringCmp (char *s1,char *s2); }
int stringCmpi(char *s1,char *s2); return 0;
int main() }
{ Output
char str1[100],str2[100]; Enter string 1 : includehelp
Enter string 2 : IncludeHelp
printf("Enter string 1 : "); stringCmp :String are not same.
scanf("%[^\n]s",str1);//read string with spaces stringCmpi :String are same.

getchar(); //to read enter after first string


12. C program to copy one string into another
printf("Enter string 2 : "); #include <stdio.h>
scanf("%[^\n]s",str2);//read string with spaces void stringCpy(char* s1,char* s2);
int main()
if(!stringCmp(str1,str2)) {
printf("\n stringCmp :String are same."); char str1[100],str2[100];
else
printf("\n stringCmp :String are not same."); printf("Enter string 1: ");
scanf("%[^\n]s",str1);//read string with spaces
if(!stringCmpi(str1,str2))
printf("\n stringCmpi :String are same."); stringCpy(str2,str1);
}
printf("String 1: %s \nString 2: %s\n",str1,str2); return pos;
return 0; }
}
int main()
/******** function definition *******/ {
void stringCpy(char* s1,char* s2) int arr[MAX];
{ int n,item,pos;
int i=0;
while(s2[i]!='\0')
{ printf("\nEnter size of an Array :");
s1[i]=s2[i]; scanf("%d",&n);
i++;
} printf("\nEnter elements of Array 1:\n");
s1[i]='\0'; /*string terminates by NULL*/ readArray(arr,n);
}
Output printf("Enter an item to find :");
Enter string 1: Help in Programming scanf("%d",&item);
String 1: Help in Programming
String 2: Help in Programming pos=findElement(arr,n,item);
13. program to find a number from array if(pos==-1)
elements printf("\n%d does not exists in array.\n",item);
#include<stdio.h> else
#define MAX 20 printf("\n%d find @ %d position.\n",item,pos);
void readArray(int a[],int size)
{
int i; printf("\n\n");
for(i=0;i< size;i++) return 0;
{ }
printf("Enter %d element :",i+1);
scanf("%d",&a[i]);
} Output
}
first run
/* function : findElement(), Enter size of an Array :5
to find an item from array elements.
return : -1 for fail, Enter elements of Array 1:
position to success. Enter 1 element :12
*/ Enter 2 element :23
int findElement(int a[],int size,int item) Enter 3 element :34
{ Enter 4 element :45
int i,pos=-1; Enter 5 element :56
for(i=0;i< size;i++) Enter an item to find :45
{
if(a[i]==item) 45 find @ 3 position.
{
pos=i; second run
break; Enter size of an Array :5
}
Enter elements of Array 1: printf("Enter number of Cols of matrix a: ");
Enter 1 element :12 scanf("%d",&c1);
Enter 2 element :23 printf("\nEnter elements of matrix a: \n");
Enter 3 element :34 readMatrix(a,r1,c1);
Enter 4 element :45 printf("Enter number of Rows of matrix b: ");
Enter 5 element :56 scanf("%d",&r2);
Enter an item to find :10 printf("Enter number of Cols of matrix b: ");
scanf("%d",&c2);
10 does not exists in array. printf("\nEnter elements of matrix b: \n");
14. C program to find multiplication of two readMatrix(b,r2,c2);
matrices if(r1==c2)
#include <stdio.h> {
#define MAXROW 10 /*Multiplication of two matrices*/
#define MAXCOL 10 for(i=0;i< r1;i++)
/*User Define Function to Read Matrix*/ {
void readMatrix(int m[][MAXCOL],int row,int col) for(j=0;j< c1;j++)
{ {
int i,j; sum=0;
for(i=0;i< row;i++) for(k=0;k< r1;k++)
{ {
for(j=0;j< col;j++) sum=sum + (a[i][k]*b[k][j]);
{ }
printf("Enter element [%d,%d] : ",i+1,j+1); result[i][j]=sum;
scanf("%d",&m[i][j]); }
} }
}
} /*print matrix*/
printf("\nMatrix after multiplying elements
/*User Define Function to Read Matrix*/ (result matrix):\n");
void printMatrix(int m[][MAXCOL],int row,int col) printMatrix(result,r1,c1);
{
int i,j; }
for(i=0;i< row;i++) else
{ {
for(j=0;j< col;j++) printf("\nMultiplication can not be done.");
{ }
printf("%d\t",m[i][j]); return 0;
} }
printf("\n"); Output
}
} Enter number of Rows of matrix a: 3
int main() Enter number of Cols of matrix a: 3
{
int a[MAXROW][MAXCOL],b[MAXROW] Enter elements of matrix a:
[MAXCOL],result[MAXROW][MAXCOL]; Enter element [1,1] : 1
int i,j,r1,c1,r2,c2; Enter element [1,2] : 2
int sum,k; Enter element [1,3] : 3
printf("Enter number of Rows of matrix a: "); Enter element [2,1] : 4
scanf("%d",&r1); Enter element [2,2] : 5
Enter element [2,3] : 6 //swap adjacent elements
Enter element [3,1] : 7 for(i=0;i < n;i+=2)
Enter element [3,2] : 8 {
Enter element [3,3] : 9 temp = arr[i];
arr[i] = arr[i+1];
Enter number of Rows of matrix b: 3 arr[i+1]= temp;
Enter number of Cols of matrix b: 3 }

Enter elements of matrix b: printf("\nArray elements after swapping


Enter element [1,1] : 1 adjacent elements:\n");
Enter element [1,2] : 1 for(i=0;i < n;i++)
Enter element [1,3] : 1 {
Enter element [2,1] : 2 printf("%d\n",arr[i]);
Enter element [2,2] : 2 }
Enter element [2,3] : 2 return;
Enter element [3,1] : 3 }
Enter element [3,2] : 3 Output
Enter element [3,3] : 3 First Run:
Enter total number of elements: 11
Matrix after multiplying elements (result matrix): Total number of elements should be EVEN.
14 14 14 Second Run:
32 32 32 Enter total number of elements: 10
50 50 50 Enter array elements:
15. C program to swap adjacent elements of a Enter element 1: 10
one dimensional Enter element 2: 20
#include <stdio.h> Enter element 3: 30
#define MAX 100 Enter element 4: 40
int main() Enter element 5: 50
{ Enter element 6: 60
int arr[MAX],n,i; Enter element 7: 70
int temp; Enter element 8: 80
Enter element 9: 90
printf("Enter total number of elements: "); Enter element 10: 100
scanf("%d",&n); Array elements after swapping adjacent
elements:
//value of n must be even 20
if(n%2 !=0) 10
{ 40
printf("Total number of elements should be 30
EVEN."); 60
return 1; 50
} 80
//read array elements 70
printf("Enter array elements:\n"); 100
for(i=0;i < n;i++) 90
{
printf("Enter element %d:",i+1);
scanf("%d",&arr[i]);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy