Write A Program To Check Whether A Word Is Palindrome or Not
Write A Program To Check Whether A Word Is Palindrome or Not
}
else
{
printf("\nAddition of matrix is not possible...");
}
return 0;
}
int main()
{
char s1[100],s2[100];
printf("Enter String=");
gets(s1);
strcpy(s2,s1);
reverse(s1);
if(strcmp(s1,s2)==0)
{
printf("\nPalindrome String");
}
else
{
printf("\nNon palindrome string");
}
return 0;
}
Bitwise Operators:
The bitwise operators are the operators used to perform the operations on the data at the bit-
level.
The following table lists the Bitwise operators supported by C. Assume variable 'A' holds 60 and
variable 'B' holds 13, then −
& Binary AND Operator copies a bit to the result if it (A & B) = 12,
exists in both operands. i.e., 0000 1100
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if
string1>string2
Structure Union
It can be created using a keyword “struct” It can be created using a keyword “union”
The total memory occupied by structure The total memory occupied by union
variable is summation of memory occupied by variable is same as largest memory
individual structure elements occupied by union elements
It takes less memory as compared to
It takes more memory as compared to union
structure
We can access individual elements We cannot access individual elements
simultaneously simultaneously
It is more efficient as compared to union It is less efficient as compared to structure
It takes less execution time as compared to It takes more execution time as compared
union to structure
while(condition)
{
Statement
}
Flowchart:
int main()
{
struct student s1,s2;
int t1,t2;
printf("Enter roll number,name and marks of physics,chemistry and maths of student
1=");
scanf("%d%s%d%d%d",&s1.roll,&s1.name,&s1.mk.p,&s1.mk.c,&s1.mk.m);
printf("\nEnter roll number,name and marks of physics,chemistry and maths of student
2=");
scanf("%d%s%d%d%d",&s2.roll,&s2.name,&s2.mk.p,&s2.mk.c,&s2.mk.m);
t1=s1.mk.p+s1.mk.c+s1.mk.m;
t2=s2.mk.p+s2.mk.c+s2.mk.m;
if(t1>t2)
{
printf("\nName of student having highest total=%s",s1.name);
}
else
{
printf("\nName of student having highest total=%s",s2.name);
}
return 0;
}
The conditional operator is also known as a ternary operator. The conditional statements are
the decision-making statements which depends upon the output of the expression. It is
represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
Example:
#include<stdio.h>
int main()
{
int a,b,c,d;
printf("Enter 3 numbers=");
scanf("%d%d%d",&a,&b,&c);
d=(a>b)?((a>c)?a:c):((b>c)?b:c);
printf("Largest number=%d",d);
return 0;
}
16. Explain the term recursion. Write a program to find the power of x raised
to n that is: xn, using recursive function.
Recursion is the process of repeating items in a self-similar way. In programming languages, if a
program allows you to call a function inside the same function, then it is called a recursive call
of the function.
Program:
#include<stdio.h>
int power(int x,int n)
{
if(n==0)
{
return 1;
}
else
{
return x*power(x,n-1);
}
}
int main()
{
int x,n,p;
printf("Enter x and n=");
scanf("%d%d",&x,&n);
p=power(x,n);
printf("\nResult=%d",p);
return 0;
}
Function Description
returns the square root of given number.
#include <stdio.h>
#include <math.h>
sqrt(n) int main ()
{
printf("Square root of %d is %f\n", 4, sqrt(4) );
printf("Square root of %d is %f\n", 5, sqrt(5) );
return 0;
}
returns the power of given number.
#include <stdio.h>
#include <math.h>
pow(a,b) int main ()
{
printf("Power of %d and %d is %f", 2, 3, pow(2,3) );
return 0;
}
rounds up the given number. It returns the integer value which is
greater than or equal to given number.
#include <stdio.h>
#include <math.h>
ceil(n)
int main ()
{
printf("Ceil of %f is %d", 2.5, ceil(2.5) );
return 0;
}
rounds down the given number. It returns the integer value which is
less than or equal to given number.
#include <stdio.h>
#include <math.h>
floor(n)
int main ()
{
printf("Floor of %f is %d", 2.5, floor(2.5) );
return 0;
}
returns the absolute value of given number.
#include <stdio.h>
#include <math.h>
int main ()
{
fabs(n) int a, b;
a = 1234;
b = -344;
printf("The absolute value of %d is %f\n", a, fabs(a));
printf("The absolute value of %d is %f\n", b, fabs(b));
return(0);
}
{
s=s+a[i];
}
average=s/10.0;
printf("\nAVERAGE=%f",average);
return 0;
}
29. Write a program to store and display at least 10 records of the name, roll
number and fees of a student using structure.
#include<stdio.h>
struct student
{
int rno;
char name[30];
float fees;
};
int main()
{
struct student s[10];
int i,j;
for(i=0;i<=9;i++)
{
printf("\nEnter Rol no Name and Fees=");
scanf("%d%s%f",&s[i].rno,&s[i].name,&s[i].fees);
}
for(i=0;i<=9;i++)
{
printf("Rollno=%d Name=%sfees=%f\n",s[i].rno,s[i].name,s[i].fees);
}
return 0;
}
* multiplication
/ division
#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;
}
31. Explain String function for the following operations with example.
Copy string from source to destination
Merging of two strings
The strcpy() function copies the string pointed by source (including the null character) to the
destination.
The strcpy() function also returns the copied string.
The strcpy() function is defined in the string.h header file.
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
Output
C programming
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";
puts(str1);
puts(str2);
return 0;
}
Output
This is programiz.com
programiz.com
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
33. Write a program to print the following pattern. (Note- Not only 4 lines, it
should print N lines taken from the user.)
A
B B
C C C
D D D D
#include<stdio.h>
int main()
{
int n,i,j,sp,k;
char ch;
printf("Enter number of lines=");
scanf("%d",&n);
ch='A';
sp=n-1
for(i=1;i<=n;i++)
{
for(k=1;k<=sp;k++)
{
printf(“ “);
}
for(j=1;j<=i;j++)
{
printf("%c ",ch);
}
printf("\n");
scanf("%d", &a[i][j]);
}
}
return 0;
}
i) getch():
Syntax:
int getch(void);
• The getch() is a predefined non-standard function that is defined in conio.h
header file.
• We use a getch() function in a Cprogram to hold the output screen for some
time until the user passes a key from the keyboard to exit the console screen.
• Parameters: The getch() function does not accept any parameter from the user.
Return value: It returns the ASCII value of the key pressed by the user as an
input.
ii) pow()
Syntax:
double pow(double x, double y)
• The first argument is a base value and second argument is a power raised to the
base value.
• The pow() function is defined in math.h header file.
iii) ceil()
Syntax:
double ceil(double x)
• This function returns the smallest integral value not less than x.
iv) puts()
Syntax:
int puts(const char* str)
• To use the puts function, you need to include the <stdio.h> library in the
program.
• The puts function writes the provided argument to the output stream and
appends a newline character at the end.
v) getchar()
Syntax:
int getchar(void)
• The getchar function is part of the <stdio.h> header file in C.
• The function reads the input as an unsigned char; then it casts and returns as an
int or an EOF.
37. What are the different ways of parameter passing to a function? Explain
with examples.
Parameters can be passed in two ways:
1. Call by Value.
2. Call by Reference.
Call by Value:
The call by value method of passing arguments to a function copies the actual value of an
argument into the formal parameter of the function. In this case, changes made to the
parameter inside the function have no effect on the argument. By default, C programming uses
call by value to pass arguments.
Example:
#include <stdio.h>
/* function declaration */
void swap(int x, int y);
int main () {
41. Give the difference between entry and exit controlled loop with an
example.
• getchar()
A getchar() function is a non-standard function whose meaning is already defined in
the stdin.h header file to accept a single input from the user. In other words, it is the C
library function that gets a single character (unsigned char) from the stdin.
#include <stdio.h>
#include <conio.h>
void main()
{
char c;
clrscr();
printf ("\n Enter a character \n");
c = getchar(); // get a single character
printf(" You have passed ");
putchar(c); // print a single character using putchar
getch();
}
Output:
Enter a character
A
You have passed A
37. What are the different ways of parameter passing to a function? Explain with
examples.
#include <stdio.h>
#include <conio.h>
int main() {
int a, b, ans;
int sum(int x, int y);
printf("Enter Two Values:);
scanf("%d%d", &a, &b)
ans=sum(a,b)
printf("sum=%d",ans);
getch();
}
int sum (int x, int y) {
int z;
z = x+y;
return(z);
}
Output:
Enter Two Values: 10 20
Sum = 30
Call by Reference:
#include <stdio.h>
#include <conio.h>
int main() {
int i, j;
int swap(int*x, int*y);
i=10;
j=20;
swap(&i, &j);
printf("%d%d", i, j);
getch();
}
int swap (int*x, int*y) {
int temp;
temp = *y;
*y = *x;
*x = temp;
}
Output:
i=20
j=10
38. Write a C program to find GCD of two numbers using recursion.
Program:
#include <stdio.h>
#include <conio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}