#include
int main()
{
int n, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (c = 0; c < n ; c++)
scanf("%d", &a[c]);
for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
for (c = 0; c < n; c++)
a[c] = b[c];
printf("Reverse array is\n");
for (c = 0; c < n; c++)
printf("%d\n", a[c]);
return 0;
}
This program reverses the array elements. For example if a is an array of integers with three elements such that
a[0] = 1
a[1] = 2
a[2] = 3
Then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1
C Programming,C Objective,Advanced C/C++ Programming,C++ Programming,Linux System Programming,RTOS,Interview Questions,Multimedia,Stagefright,Android Application Programming
My Blog List
Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts
C program to reverse an array
C Program to Print a pyramid string triangle
#include <stdio.h>
#include <string.h>
int main()
{
int i,j,length;
char arr[] = "Programming";
length = strlen(arr);
for (i = length; i >= 0; i--)
{
printf("\n");
for (j = 0; j< i; j++)
{
printf("%c",arr[j]);
}
}
return 0;
}
#include <string.h>
int main()
{
int i,j,length;
char arr[] = "Programming";
length = strlen(arr);
for (i = length; i >= 0; i--)
{
printf("\n");
for (j = 0; j< i; j++)
{
printf("%c",arr[j]);
}
}
return 0;
}
Output:
Programming
Programmin
Programmi
Programm
Program
Progra
Progr
Prog
Pro
Pr
P
C Program to count Number of Words in a Line
#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
char *p;
int count=0;
clrscr();
printf("Enter the Line:");
gets(str);
p=str;
while(*p!='\0')
{
if(*p=='')
{
count++;
}
p++;
}
printf("The number of Word is: %d",count+1);
getch();
}
Output:Enter the Line: C Programming Language
The number of Word is: 3
#include<string.h>
void main()
{
char str[20];
char *p;
int count=0;
clrscr();
printf("Enter the Line:");
gets(str);
p=str;
while(*p!='\0')
{
if(*p=='')
{
count++;
}
p++;
}
printf("The number of Word is: %d",count+1);
getch();
}
Output:Enter the Line: C Programming Language
The number of Word is: 3
Write a C program to reverse the given number and check if the number is palindrome.
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
/* Checking if number entered by user and it's reverse number is equal. */
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}
Output:
Enter an integer: 12321
12321 is a palindrome.
Write a C program to find the sum of the first 15 even numbers and calculate the square of the sum
void main()
{
int c=1,i;
unsigned int s=0;
for(i=1;i<=15;i++)
{
if(i%2==0)
{
s=s+i;
c=c+i;
}
}
printf("Sum of the first 15 even numbers is: %d",s);
printf("Square of the sum is: %d",(s*s));
}
Write a C program to find the total and average of the marks given in an array.
void main()
{
int i;
int Avg;
int maks[20];
clrscr();
sum=0;
for(i=0;i<=9;i++)
{
printf(" Enter Marks/n ");
scanf("%d",&marks[i] );
}
for(i=0;i<=9;i++)
{
sum = sum + marks[i];
Avg = sum/10;
printf(" Total= %d\n" , sum);
printf(" Average Marks = %d\n" , Avg);
}
}
Write a C program to search the given number in an array using binary search
int main()
{
int a[10],i,n,m,c,l,u;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: " );
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("Number is not found.");
else
printf("Number is found.");
return 0;
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else if(m<a[mid])
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
Output:
Enter the size of an array: 5
Enter the elements of the array: 8 9 10 11 12
Enter the number to be search: 8
Number is found.
Write a C program to convert the given decimal number to binary, Octal and Hexadecimal
Convert Decimal to Binary:
int main()
{
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return 0;
}
output:
Enter any decimal number: 50
Equivalent binary value of decimal number 50: 110010
Convert Decimal to Octal:
int main(){
long int decimalNumber,remainder,quotient;
int octalNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
octalNumber[i++]= quotient % 8;
quotient = quotient / 8;
}
printf("Equivalent octal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",octalNumber[j]);
return 0;
}
Output:
Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62
2. Easy way to convert decimal number to octal number in c
#include
int main(){
long int decimalNumber;
printf("Enter any decimal number : ");
scanf("%d",&decimalNumber);
printf("Equivalent octal number is: %o",decimalNumber);
return 0;
}
output:
Enter any decimal number: 25
Equivalent octal number is: 31
Convert Decimal to Hexadecimal:
int main(){
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
temp = quotient % 16;
//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadecimalNumber[j]);
return 0;
}
Output:
Enter any decimal number: 45
Equivalent hexadecimal value of decimal number 45: 2D
2. Easy way to convert decimal number to hexadecimal number:
#include
int main(){
long int decimalNumber;
printf("Enter any decimal number: ");
scanf("%d",&decimalNumber);
printf("Equivalent hexadecimal number is: %X",decimalNumber);
return 0;
}
Output:
Enter any decimal number: 45
Equivalent hexadecimal number is: 2D
Write a C program to generate a multiplication table
int main(){
int r,i,j,k;
printf("Enter the number range: ");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
return 0;
}
Output:
Enter the number range: 5
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
Write a C program to demonstrate passing structures to functions.
In C, structure can be passed to functions by two methods:
1) Passing by value (passing actual value as argument)
2) Passing by reference (passing address of an argument)
Passing structure by value:
#includePassing structure by reference:struct student{ char name[50]; int roll; }; void Display(struct student stu); /* function prototype should be below to the structure declaration otherwise compiler shows error */ int main(){ struct student s1; printf("Enter student's name: "); scanf("%s",&s1.name); printf("Enter roll number:"); scanf("%d",&s1.roll); Display(s1); // passing structure variable s1 as argument return 0; } void Display(struct student stu){ printf("Output\nName: %s",stu.name); printf("\nRoll: %d",stu.roll); } Output: Enter student's name: Kevin Amla Enter roll number: 149 Output Name: Kevin Amla Roll: 149
#includestruct distance{ int feet; float inch; }; void Add(struct distance d1,struct distance d2, struct distance *d3); int main() { struct distance dist1, dist2, dist3; printf("First distance\n"); printf("Enter feet: "); scanf("%d",&dist1.feet); printf("Enter inch: "); scanf("%f",&dist1.inch); printf("Second distance\n"); printf("Enter feet: "); scanf("%d",&dist2.feet); printf("Enter inch: "); scanf("%f",&dist2.inch); Add(dist1, dist2, &dist3); /*passing structure variables dist1 and dist2 by value whereas passing structure variable dist3 by reference */ printf("\nSum of distances = %d\'-%.1f\"",dist3.feet, dist3.inch); return 0; } void Add(struct distance d1,struct distance d2, struct distance *d3) { /* Adding distances d1 and d2 and storing it in d3 */ d3->feet=d1.feet+d2.feet; d3->inch=d1.inch+d2.inch; if (d3->inch>=12) { /* if inch is greater or equal to 12, converting it to feet. */ d3->inch-=12; ++d3->feet; } } Output: First distance Enter feet: 12 Enter inch: 6.8 Second distance Enter feet: 5 Enter inch: 7.5 Sum of distances = 18'-2.3"
How to swap two numbers in c without using third variable
#includeint main(){ int a=5,b=10; //process one a=b+a; b=a-b; a=a-b; printf("a= %d b= %d",a,b); //process two a=5;b=10; a=a+b-(b=a); printf("\na= %d b= %d",a,b); //process three a=5;b=10; a=a^b; b=a^b; a=b^a; printf("\na= %d b= %d",a,b); //process four a=5;b=10; a=b-~a-1; b=a+~b+1; a=a+~b+1; printf("\na= %d b= %d",a,b); //process five a=5,b=10; a=b+a,b=a-b,a=a-b; printf("\na= %d b= %d",a,b); return 0; }
Wrie a C program to print Pascal triangle using for loop
long fact(int);
int main()
{
int line,i,j;
printf("Enter the no. 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;
}
Output:
Enter the no. of lines: 8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Check given number is prime number or not using c program
Definition of prime number:
A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself.
For example: 5 Their divisors are 1 and 5.
Note: 2 is only even prime number.
Logic for prime number in c: We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.
Example of prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
Find given number is prime or not:
For example: 5 Their divisors are 1 and 5.
Note: 2 is only even prime number.
Logic for prime number in c: We will take a loop and divide number from 2 to number/2. If the number is not divisible by any of the numbers then we will print it as prime number.
Example of prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
Find given number is prime or not:
int main(){
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
Output:
Enter a number: 5
5 is a prime number
To Print Prime numbers from 1 to 100:
int main(){
int num,i,count;
for(num = 1;num<=100;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Write a C program whether the given number is Perfect number or not.
Definition of Perfect number: A positive integer n is called a perfect number if it is equal to the sum of all of its positive divisors, excluding n itself.
For example, 6 is perfect integer number, because 1, 2 and 3 are its proper positive divisors and 1+2+3=6.
The next perfect number is 28 because 1+2+4+7+14=28.
The next perfect number is 496 because
1+2+4+8+16+31+62+124+248=496.
/*program to check whether a number is perfect or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,num,sum=0;
printf("Enter a number : ");
scanf("%d",&num);
n=num;
for(i=1; i<n; i++)
{
if(num%i==0)
sum=sum+i;
}
if(num==sum)
printf("Given number is Perfect Number ");
else
printf("Given number is Not Perfect Number ");
getch();
return 0;
}
For example, 6 is perfect integer number, because 1, 2 and 3 are its proper positive divisors and 1+2+3=6.
The next perfect number is 28 because 1+2+4+7+14=28.
The next perfect number is 496 because
1+2+4+8+16+31+62+124+248=496.
/*program to check whether a number is perfect or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,num,sum=0;
printf("Enter a number : ");
scanf("%d",&num);
n=num;
for(i=1; i<n; i++)
{
if(num%i==0)
sum=sum+i;
}
if(num==sum)
printf("Given number is Perfect Number ");
else
printf("Given number is Not Perfect Number ");
getch();
return 0;
}
Output:-
Enter a number : 7543
Given number is Not Perfect Number
Enter a number : 8128
Given number is Perfect Number
Enter a number : 7543
Given number is Not Perfect Number
Enter a number : 8128
Given number is Perfect Number
Write a C program to add, subtract, multiply and divide two numbers using functions
int add (int x, int y)
{
int z;
z = x + y;
return (z);
}
int sub(int x, int y)
{
int z;
z = x - y;
return (z);
}
int mul(int x, int y)
{
int z;
z = x * y;
return (z);
}
int div(int x, int y)
{
int z;
z = x / y;
return (z);
}
main ()
{
int i,j,k,l,m,n;
i = 10;
j = 20;
k = add(i, j);
l=sub(i,j);
m=mul(i,j);
n=div(i,j);
printf("\n Sum %d",k);
printf("\n Subtraction %d",l);
printf("\n Multiplication %d",m);
printf("\n divide %d",n);
}
Write a C program for matrix addition
Rule: Addition of two matrices is only possible if both matrices are of same size.
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]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",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<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Write a C program for matrix subtraction
Rule: Subtraction of two matrices is only possible if both matrices are of same size.
Suppose two matrices A and B is of same size m X n
Subtraction of two matrices is defined as
(A - B)ij = Aij - Bij
Where 1 ≤ i ≤ m and 1 ≤ j ≤ n
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]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",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 Subtraction of two matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Write a C program to transpose the given matrix
Transpose: Transpose of a Matrix means changing Rows into Columns and vice-versa.
void main()
{
int A[2][3] , B[3][2];
int i, j; /* 'i' used for rows and 'j' used for columns */
clrscr();
printf(" Enter the elements of A\n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf("%d" , &A[i][j] );
}
}
printf(" Matrix is\n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<3 ; j++)
{
printf("%d\t",A[i][j] ); /* '\t' used for Tab */
}
printf("\n"); /* '\n' used for next line character */
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<2 ; j++)
{
B[i][j] = A[j][i];
}
}
printf(" After Transpose\n");
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<2 ; j++)
{
printf("%d\t" , B[i][j] );
}
printf("\n");
}
getch();
}
Write a C program to find string palindrome without using String functions
int main()
{
char text[100];
int begin, middle, end, length = 0;
gets(text);
while ( text[length] != '\0' )
length++;
end = length - 1;
middle = length/2;
for( begin = 0 ; begin < middle ; begin++ )
{
if ( text[begin] != text[end] )
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.\n");
return 0;
}
Write a C program for demonstrating Tower of Hanoi using Recursion
#include#include #include void hanoi(int x, char from, char to, char aux) { if(x==1) printf("Move Disk From %c to %c\n",from,to); else { hanoi(x-1,from,aux,to); printf("Move Disk From %c to %c\n",from,to); hanoi(x-1,aux,to,from); } } void main( ) { int disk; int moves; clrscr(); printf("Enter the number of disks you want to play with:"); scanf("%d",&disk); moves=pow(2,disk)-1; printf("\nThe No of moves required is=%d \n",moves); hanoi(disk,'A','C','B'); getch( ); } Output: Enter the number of disks you want to play with: 3 The No of moves required is=7 Move Disk from A to C Move Disk from A to B Move Disk from C to B Move Disk from A to C Move Disk from B to A Move Disk from B to C Move Disk from A to C
Subscribe to:
Posts (Atom)