#include<stdio.h>
#include<string.h>
void main() {
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);
length = strlen(string);
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0; i--) {
reverse_string[length - i - 1] = string[i];
}
for (i = 0; i < length; i++) {
if (reverse_string[i] == string[i])
flag = 1;
else
flag = 0;
}
if (flag == 1)
printf("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Monday, 5 October 2015
Find given string is palindrome or not using string library function.
Write a program convert character(String) into TOggLe character(String)
#include<stdio.h>
void main() {
char str[100];
int i;
printf("Please enter a string: ");
gets(str);
for (i = 0; str[i] != NULL; i++) {
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
else if (str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
}
printf("String in toggle case is: %s", str);
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to join two strings.
#include<stdio.h>
#include<string.h>
//#include<conio.h>
void main() {
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the first string \n");
gets(b);
strcat(a, b);
printf("String after joining is %s\n", a);
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to copy one string to another string.
#include<stdio.h>
//#include<conio.h>
void main() {
char name1[100], name2[100];
int i = 0;
//clrscr();
printf(" Please Give The STRING OF name1 \n:");
gets(name1);
while (name1[i] != '\0') {
name2[i] = name1[i];
i++;
}
name2[i] = name1[i];
printf("string name1 is : % s\n", name1);
printf("string name2 is : % s\n", name2);
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Find length of string using strlen( ) function
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//#include<conio.h>
void main() {
int strlength;
char str[100];
//clrscr();
printf("\nEnter the string: ");
gets(str);
strlength = strlen(str);
printf("\nThe length of the string is %d.", strlength);
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to count total words in text.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
//#include<conio.h>
void main() {
char sen[100];
int i, count = 1;
//clrscr();
printf("Enter a Sentence : \n");
gets(sen);
for (i = 0; i < strlen(sen); i++) {
if (isspace(sen[i])) {
count++;
}
}
printf("Number of words in the sentence = %d", count);
printf("\n");
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to read array of integers and print it in reverse order
#include<stdio.h>
//#include<conio.h>
void main() {
int i, j = 0, a[10], b[10];
//clrscr();
for (i = 0; i < 10; i++) {
printf("enter value of a[%d]= ", i);
scanf("%d", &a[i]);
}
for (i = 9; i >= 0; i--) {
b[j] = a[i];
j++;
}
printf("reverse of array is= \n");
for (j = 0; j < 10; j++) {
printf("b[%d]=%d", j, b[j]);
printf("\n");
}
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to given numbers in ascending order.
#include<stdio.h>
//#include<conio.h>
void main() {
int a[10], i, j, temp = 0, no;
//clrscr();
printf("Enter the no of elements in the array:\n");
scanf("%d", &no);
printf("Enter the array a:\n");
for (i = 0; i < no; i++)
scanf("%d", &a[i]);
for (i = 0; i < no; i++) {
for (j = i + 1; j < no; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("The Ascending array:\n");
for (i = 0; i < no; i++)
printf("%d\n", a[i]);
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to add two matrixes.
#include<stdio.h>
//#include<conio.h>
void main() {
int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q;
//clrscr();
printf("\nProgram to Add Two Matrices\n");
printf("\nHow Many Rows & Columns of A Matrix: ");
scanf("%d %d", &n, &m);
printf("\nHow Many Rows & Columns of B Matrix: ");
scanf("%d %d", &p, &q);
if ((n == p)&&(m == q)) {
printf("\nMatrices Can Be Added");
printf("\nEnter Elements for A Matrix: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
printf("nEnter Elements for B Matrix: \n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
printf("\nSum of Two Matrices:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("\n%5d", c[i][j]);
}
printf(" ");
}
} else
printf("\nMatrices Cannot Be Added");
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Add, subtract and multiply two nos. using switch statement.
#include<stdio.h>
//#include<conio.h>
void main() {
int ch, a, b, c;
//clrscr();
printf("enter a:");
scanf("%d", &a);
printf("enter b:");
scanf("%d", &b);
printf("----------MENU-----------\n");
printf("1.addition\n");
printf("2.subtraction\n");
printf("3.multiplication\n");
scanf("%d", &ch);
switch (ch) {
case 1:
c = a + b;
printf("addition of a and b=%d", c);
break;
case 2:
c = a - b;
printf("subtraction of a and b=%d", c);
break;
case 3:
c = a*b;
printf("Multiply of a and b=%d", c);
break;
default:
printf("wrong choice");
}
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to reverse the digit.
#include<stdio.h>
//#include<conio.h>
void main() {
int num, sum = 0;
//clrscr();
printf("enter the value of num");
scanf("%d", &num);
while (num > 0) {
sum = sum * 10 + num % 10;
num = num / 10;
}
printf("reverse = % d", sum);
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Write a program to print Fibonacci series. 1,1,2,3,5,......N
#include<stdio.h>
//#include<conio.h>
void main() {
int n1 = 0, n2 = 1, n3, n, i = 0;
//clrscr();
printf("enter n ");
scanf("%d", &n);
printf("\n the fibonacci series till the %dth term is::", n);
do {
printf("%d ", n1);
printf("\t");
n3 = n1 + n2;
n1 = n2;
n2 = n3;
i++;
} while (i <= n);
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Program to print odd/even number Patterns.
#include<stdio.h>
//#include<conio.h>
#include
//#include
void main() {
int i, j, l, k = 0;
//clrscr();
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++) {
k++;
printf("%d", k % 2);
}
for (l = i; l <= 4; l++) {
k++;
}
printf("\n");
}
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Labels:
C
Program to print Alphabet Patterns.
#include<stdio.h>
//#include<conio.h>
void main() {
int i, j;
char a = 'A';
//clrscr();
for (i = 5; i > 0; i--) {
for (j = i; j > 0; j--) {
printf("%c", a);
}
a++;
printf("\n");
}
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Labels:
C
Program to print numbers Patterns.
#include<stdio.h>
//#include<conio.h>
void main() {
int i, j, k;
//clrscr();
for (i = 1; i <= 5; i++) {
for (k = 1; k < i; k++) {
printf(" ");
}
for (j = i; j <= 5; j++) {
printf("%d", j);
}
printf("\n");
}
//getch();
}
Note: Remove the comment line when you are use Turbo C IDE for run your program.
Labels:
C
Wednesday, 30 September 2015
Program to print star Patterns
#include<stdio.h>
#include<conio.h>
void main()
{
inti,j;
clrscr();
for(i=1;i<=5;i++)
{
printf(“\n”);
for(j=1;j<=i;j++)
{
printf(“*”);
}
}
getch();
}
Write a program for use of putchar( ) and getchar( ) function.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("enter any one lowercase letter\n");
c=getchar();
printf("lowercase character = ");
putchar(c);
printf("converted to uppercase character = ");
putchar(c-32);
getch();
}
Tuesday, 30 June 2015
Program 5 : Write a program to find sum of first N odd numbers. Ex. 1+3+5+7+_ _+N.
#include<stdio.h>
void main()
{
int i, n, sum = 0;
i = 1;
scanf("%d", &n);
while (i <= n)
{
if (i % 2 != 0)
sum = sum + i;
i++;
}
printf("sum : %d", sum);
}
Labels:
C
Program 4 : The distance between two cities (In KM) is input through key board. Write a program to convert and print this distance in meters, feet, inches & centimeters
#include<stdio.h>
void main()
{
int km;
float meter, feet, cm, inches;
printf("Enter the distance between two city");
scanf("%d", &km);
meter = km * 1000;
feet = km * 3280;
cm = km * 100000;
inches = km * 39370;
printf("km is :%d\n meter is:%f\n feet is:%f\n cm is:%f\n inches is:%f ", km, meter, feet, cm, inches);
}
Labels:
C
Program 3 : Write a program to find sum of all integers greater than 100 & less than 200 and are divisible by 5.
#include<stdio.h>
void main()
{
int i, sum = 0;
for (i = 101; i < 200; i++) {
if (i % 5 == 0) {
sum = sum + i;
}
}
printf("SUM is :%d", sum);
}
Labels:
C
Subscribe to:
Posts (Atom)