C Programming: Operators and Simple Programs
C Programming: Operators and Simple Programs
a. Write a simple program that prints the results of all the operators available in C (including
pre /post increment, bitwise etc) Read required operand values from the standard input
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
c=a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
C Program to demonstrate the working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d = %d \n", a, b, a == b);
printf("%d == %d = %d \n", a, c, a == c);
printf("%d > %d = %d \n", a, b, a > b);
printf("%d > %d = %d \n", a, c, a > c);
printf("%d < %d = %d \n", a, b, a < b);
printf("%d < %d = %d \n", a, c, a < c);
printf("%d != %d = %d \n", a, b, a != b);
printf("%d != %d = %d \n", a, c, a != c);
printf("%d >= %d = %d \n", a, b, a >= b);
printf("%d >= %d = %d \n", a, c, a >= c);
printf("%d <= %d = %d \n", a, b, a <= b);
printf("%d <= %d = %d \n", a, c, a <= c);
return 0;
Output
5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
C Program to demonstrate the working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d \n", c);
c += a;
printf("c = %d \n", c);
c -= a;
printf("c = %d \n", c);
c *= a;
printf("c = %d \n", c);
c /= a;
printf("c = %d \n", c);
c %= a;
printf("c = %d \n", c);
return 0;
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C Program to demonstrate the working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
C Program to demonstrate the working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
return 0;
}
Output
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}
Output
Output = 8
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a^b);
return 0;
}
Output
Output = 29
C Program to demonstrate the working of conditional operators
#include <stdio.h>
int main(){
char February;
int days;
printf("If this year is leap year, enter 1. If not enter any integer: ");
scanf("%c",&February);
days = (February == '1') ? 29 : 28;
printf("Number of days in February = %d",days);
return 0;
}
Output
#include <stdio.h>
int main()
{
int a, e[10];
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
return 0;
}
Output
#include<stdio.h>
int main()
{
int x = 10;
char y = 'a';
x = x + y;
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Output
x = 107, z = 108.000000
#include<stdio.h>
int main()
{
double x = 1.2;
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Output
sum = 2
Simple numeric problems:
a. Write a program for fiend the max and min from the three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Please enter the value of A: ");
scanf("%d",&a);
printf("\n Please enter the value of B: ");
scanf("%d",&b);
printf("\n Please enter the value of C: ");
scanf("%d",&c);
if (a<b && a<c)
printf("\n %d is Minimum which is value of A",a);
elseif(b<a && b<c)
printf("\n %d is Minimum which is value of B",b);
else
printf("\n %d is Minimum which is value of C",c);
getch();
}
Output
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
Output
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Compound Interest = 1333.099243
c. Write program that declares Class awarded for a given percentage of marks, where
mark <40%= Failed, 40% to <60% = Second class, 60% to <70%=First class, >=
70% = Distinction. Read percentage from standard input.
#include<stdio.h>
void main()
{
int m1,m2,m3,total;
float per;
clrscr();
printf("Enter 3 Nos.");
scanf("%d%d%d",&m1,&m2,&m3);
total=m1+m2+m3;
per=total*100/300;
if(per>=60&&per<=100)
printf("You are 1st :");
else if(per>=50&&per<=60)
printf("You are 2nd");
else if(per>=40&&per<=50)
printf("You are 3rd");
else
printf("You are Fail");
getch();
}
Output:
Enter 3Nos.
60
40
70
You are 2nd
d. Write a program that prints a multiplication table for a given number and the number
of rows in the table.
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}
Output:
Enter an integer: 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Expression Evaluation:
Write a C program, which takes two integer operands and one operator from the user, performs
the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch
Statement)
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
char ch;
clrscr() ;
printf("Enter your operator(+, -, /, *, %)\n");
scanf("%c", &ch);
printf("Enter the values of a and b\n");
scanf("%d%d", &a, &b);
switch(ch)
{
case '+': c = a + b;
printf("addition of two numbers is %d", c);
break;
case '-': c = a - b;
printf("substraction of two numbers is %d", c);
break;
case '*': c = a * b;
printf("multiplication of two numbers is %d", c);
break;
case '/': c = a / b;
printf("remainder of two numbers is %d", c);
break;
case '%': c = a % b;
printf("quotient of two numbers is %d", c);
break;
default: printf("Invalid operator");
break;
}
getch();
}
Output:
Enter you operator(+, -, /, *, %)
+
Enter the values of a and b
13
addition of two numbers is 4
Write a program that finds if a given number is a prime number
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output
Enter a positive integer: 29
29 is a prime number.
Write a C program to find the sum of individual digits of a positive integer and test given number
is palindrome
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
printf("enter a +ve integer");
scanf("%d",&n);
while(n>0)
{
sum=sum+n%10;
n=n/10;
}
printf("sum of individual digits of a positive integer is %d",sum);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0,num;
printf("enter a +ve integer");
scanf("%d",&n);
num=n;
while(n>0)
{
n=n%10;
rev=rev*10+n;
n=n/10;
}
if(num==rev)
{
printf(“the given number is palindrome”);
}
else
{
printf(“the given number is not palindrome”);
}
getch();
}
Output:
A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and
1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 0, b = 1, lengthOfSeries = 0, counter, sum = 0;
clrscr();
printf("Enter the length of series \n ");
scanf("%d", &lengthOfSeries);
printf("Fibonacci series\n");
printf("%d %d", a, b);
for(counter = 2; counter < lengthOfSeries; counter++)
{
sum = a + b;
printf(" %d",sum);
a = b;
b = sum;
}
getch();
}
Output:
Enter the length of series
15
Fibonacci series
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, count;
clrscr();
printf("Prime no.series\n");
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i <= n; i++)
{
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0)
{
count++;
}
if(count == 2)
{
printf("%d\t", i);
}
}
getch();
}
Output:
#include <stdio.h>
#include <math.h>
void main()
{
int a,b,c,d;
float x1,x2;
printf("Input the value of a,b & c : ");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf("Both roots are equal.\n");
x1=-b/(2.0*a);
x2=x1;
printf("First Root Root1= %f\n",x1);
printf("Second Root Root2= %f\n",x2);
}
else if(d>0)
{
printf("Both roots are real and diff-2\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("First Root Root1= %f\n",x1);
printf("Second Root root2= %f\n",x2);
}
else
printf("Root are imaginary;\nNo Solution. \n");
}
Output:
1-x/2 +x^2/4-x^3/6
#include <stdio.h>
void main()
{
float x,sum,t,d;
int i,n;
printf("Input the Value of x :");
scanf("%f",&x);
printf("Input the number of terms : ");
scanf("%d",&n);
sum =1; t = 1;
for (i=1;i<n;i++)
{
d = (2*i)*(2*i-1);
t = -t*x*x/d;
sum =sum+ t;
}
printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x = %f\n",sum,n,x);
}
Write a C program to find the minimum, maximum and average in an array of integers.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int i, max, min, size;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max = arr[0];
min = arr[0];
for(i=1; i<size; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
Output:
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}
Output:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j;
clrscr();
printf("Enter the elements of 3*3 matrix a \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &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("The resultant 3*3 matrix c is \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("Enter the elements of 3*3 matrix a \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of 3*3 matrix b \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = 0
for(k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j])
}
}
}
printf("The resultant 3*3 matrix c is \n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Write a C program that uses functions to perform Transpose of a matrix with memory
dynamically allocated for the new matrix as row and column counts may not be same.
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
return 0;
}
Output
Entered Matrix:
2 3 4
5 6 4
Transpose of Matrix:
2 5
3 6
4 4
Write C programs that use both recursive and non-recursive functions To find the factorial of a
given integer.
#include <stdio.h>
#include <conio.h>
void main()
{
int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);
a = recfactorial(n);
printf("The factorial of a given number using recursion is %d \n", a);
b = nonrecfactorial(n);
printf("The factorial of a given number using nonrecursion is %d ", b);
getch();
}
int recfactorial(int x)
{
int f;
if(x == 0)
{
return(1);
}
else
{
f = x * recfactorial(x - 1);
return(f);
}
}
int nonrecfactorial(int x)
{
int i, f = 1;
for(i = 1;i <= x; i++)
{
f = f * i;
}
return(f);
}
Output:
Enter any number
5
The factorial of a given number using recursion is 120
The factorial of a given number using non recursion is 120
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, d;
clrscr();
printf("Enter two numbers a, b\n");
scanf("%d%d", &a, &b);
c = recgcd(a, b);
printf("The gcd of two numbers using recursion is %d\n", c);
d = nonrecgcd(a, b);
printf("The gcd of two numbers using nonrecursion is %d", d);
getch();
}
int recgcd(int x, int y)
{
if(y == 0)
{
return(x);
}
else
{
return(recgcd(y, x % y));
}
}
int nonrecgcd(int x, int y)
{
int z;
while(x % y != 0)
{
z = x % y;
x = y;
y = z;
}
return(y);
}
Output:
Enter two numbers a, b
36
The gcd of two numbers using recursion is 3
The gcd of two numbers using non recursion is 3
Write C programs to find x^n
#include<stdio.h>
#include<math.h>
int main()
{
float x = 2;
int y = -3;
printf("%f", pow (x, y));
return 0;
}
Output :
0.125000
Write a program for reading elements using pointer into array and display the values using array.
#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter elements: ");
return 0;
}
Output
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4
Write a program for display values reverse order from array using pointer.
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
printf("\nEnter %d integers into array: ", size);
for (i = 0; i < size; i++) {
scanf("%d", ptr);
ptr++;
}
ptr = &arr[size - 1];
printf("\nElements of array in reverse order are :");
for (i = size - 1; i >= 0; i--) {
printf("\nElement%d is %d : ", i, *ptr);
ptr--;
}
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
void main() {
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray; /* a=&a[0] */
for (i = 0; i < 10; i++) {
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
}
Output:
Enter 10 elements: 11 12 13 14 15 16 17 18 19 20
The sum of array elements is 155
Write a C program to display the contents of a file to standard output device.
#include<stdio.h>
#include<conio.h>
FILE *fp1,*fp2;
char c;
void main()
{
clrscr();
printf("enter the text\n");
fp1 = fopen("abc.txt", "w");
while((c = getchar()) != EOF)
putc(c, fp1);
fclose(fp1);
fp1 = fopen("abc.txt","r");
fp2=fopen("xyz.txt","w");
while(!feof(fp1))
{
c = getc(fp1);
putc(c,fp2);
}
fclose(fp1);
fclose(fp2);
printf("the copied data is \n");
fp2 = fopen("xyz.txt", "r");
while(!feof(fp2))
{
c = getc(fp2);
printf("%c", c);
}
getch();
}
Output:
enter the text
Engineering students are very good.
^Z
the copied data is
Engineering students are very good.
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *f1, *f2;
char c;
clrscr();
printf(“Enter the data to the file1.txt file \n”);
f1 = fopen(“file1.txt”, “w”);
while((c = getchar()) != EOF)
putc(c, f1);
fclose(f1);
f2 = fopen(“file2.txt”, “w”);
f1 = fopen(“file1.txt”, “r”);
while((c = getc(f1)) != EOF)
putc(c,f2);
fclose(f1);
fclose(f2);
printf(“after copying the data in file2.txt file is \n”);
f2 = fopen(“file2.txt”, “r”);
while((c = getc(f2)) != EOF)
printf(“%c”, c);
fclose(f2);
getch();
}
Output:
Write a C program to count the number of times a character occurs in a text file. The file
name and the character are supplied as command line arguments.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l);
for(i = 0; i <= l-1; i++)
{
if(str[i] == ' ')
{
f = f + 1;
}
}
printf("The number of words in the string are %d", f);
getch();
}
Output:
Write a C program to merge two files into a third file (i.e., the contents of the firs t file
followed by those of the second are put in the third file).
#include<stdio.h>
void concatenate(FILE *fp1, FILE *fp2, char *argv[], int argc);
int main(int argc, char *argv[]){
FILE *fp1, *fp2;
concatenate(fp1, fp2, argv, argc);
return 0;
}
File1:
Sumathi Reddy
File2:
This is PPS Lab.
File 3:
Sumathi Reddy This is PPS Lab.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char rom[30];
int a[30], l, i, k, dec;
clrscr();
printf("Enter the roman number\n");
scanf("%s", &rom);
l =strlen(rom);
for(i = 0; i < l; i++)
{
switch (rom[i])
{
case 'I': a[i] = 1;
break;
case 'V': a[i] = 5;
break;
case 'X': a[i] = 10;
break;
case 'L': a[i] = 50;
break;
case 'C': a[i] = 100;
break;
case 'D': dec = dec + 500;
break;
case 'M': a[i] = 1000;
break;
default : printf("Invalid choice");
break;
}
}
k = a[l - 1];
for(i = l - 1; i > 0; i--)
{
if(a[i] > a[i - 1])
{
k = k - a[i - 1];
}
if(a[i] <= a[i - 1])
{
k = k + a[i - 1];
}
}
printf("decimal equivalent is %d", k);
getch();
}
Output:
Enter the roman number
XIV
Decimal equivalent is 14
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
clrscr();
puts("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
puts("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
getch();
}
Output:
Enter the string 1
Sumathi
Enter the string 2
Reddy
Enter the position where the string is to be inserted
4
After inserting the string is SumaReddythi
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int i, n, l, pos;
clrscr();
puts("Enter the string\n");
gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i < l; i++)
{
str[i - n] = str[i];
}
str[i - n] = '\0';
printf("The string is %s", str);
getch();
}
Output:
Enter the string
sachin
Enter the position where characters are to be deleted
2
Enter the number of characters to be deleted
2
The string is sain
Write a C program to determine if the given string is a palindrome or not (Spelled same
in both directions with or without a meaning like madam, civic, noon, abcba, etc.)
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[20];
int i, l, f = 0;
clrscr();
printf("Enter any string\n");
gets(str);
l = strlen(str);
for(i = 0; i <= l - 1; i++)
{
if(str[i] == str[l - 1 - i])
f = f + 1;
}
if(f == l)
{
printf("The string is palindrome");
}
else
{
printf("The string is not a palindrome");
}
getch();
}
Output:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l);
for(i = 0; i <= l-1; i++)
{
if(str[i] == ' ')
{
f = f + 1;
}
}
printf("The number of words in the string are %d", f);
getch();
}
Output:
#include <stdio.h>
int main()
{
int i, j, rows;
Output:
1
12
123
1234
12345
#include <stdio.h>
int main()
{
int i, j, rows;
return 0;
}
Output:
12345
1234
123
12
1
Write a C program that uses non recursive function to search for a Key value in a given
List of integers using linear search method.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a[20], n, key, flag = 0;
clrscr();
printf(“Enter the size of an array \n”);
scanf(“%d”, &n);
printf(“Enter the array elements”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key elements”);
scanf(“%d”, &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
printf(“The key elements is found at location %d”, i + 1);
else
printf(“The key element is not found in the array”);
getch();
}
Output:
Enter the size of an array 6
Enter the array elements 50 10 5 200 20 1
Enter the key element 1
The key Element is found at location 6
Write a C program that uses non recursive function to search for a Key value in a
Given sorted list of integers using binary search method.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
clrscr();
printf(“Enter the array elements in ascending order”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key element\n”);
scanf(“%d”, &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf(“The key element is found at location %d”, mid + 1);
else
printf(“the key element is not found”);
getch();
}
Output:
Enter the size of the array 7
Enter the array elements in ascending order 23 45 68 90 100 789 890
Enter the key element 789
The key Element is found at location 6
Write a C program that implements the Bubble sort method to sort a given list of
Integers in ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, a[20], temp, i, j;
clrscr();
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("The sorted array is\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
Output:
Enter the size of the array: 5
Enter the array elements: 50 40 30 20 10
The sorted array is: 10 20 30 40 50
Write a C program that sorts the given array of integers using selection sort in descending
order
#include<stdio.h>
#include<conio.h>
void main()
{
int n, a[20], min, temp, i, j;
clrscr();
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i < n - 1; i++)
{
min = i;
for(j = i + 1; j < n; j++)
{
if(a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
printf("The sorted array is\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
Output:
Enter the size of the array: 7
Enter the array elements: 7 6 5 4 3 2 1
The Sorted array is: 1 2 3 4 5 6 7
Write a C program that sorts the given array of integers using insertion sort in
Ascending order
#include<stdio.h>
int main()
{
int data[100],n,temp,i,j;
printf("Enter number of terms ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
for(i=1;i<n;i++)
{
temp = data[i];
j=i-1;
while(temp<data[j] && j>=0)
{
data[j+1] = data[j];
--j;
}
data[j+1]=temp;
}
printf("In ascending order: ");
for(i=0; i<n; i++)
printf("%d\t",data[i]);
return 0;
}
Output