0% found this document useful (0 votes)
62 views21 pages

Write A Program To Check Whether A Word Is Palindrome or Not

The document contains code for a C program that takes two matrices as input from the user, multiplies the matrices, and displays the result. It first defines functions to accept the dimensions of and values for the two matrices from the user. It then checks if matrix multiplication is possible based on the dimensions before calling a function to perform the multiplication. The product matrix is then displayed before the program returns 0, indicating successful completion.

Uploaded by

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

Write A Program To Check Whether A Word Is Palindrome or Not

The document contains code for a C program that takes two matrices as input from the user, multiplies the matrices, and displays the result. It first defines functions to accept the dimensions of and values for the two matrices from the user. It then checks if matrix multiplication is possible based on the dimensions before calling a function to perform the multiplication. The product matrix is then displayed before the program returns 0, indicating successful completion.

Uploaded by

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

display(c,m1,n1);

}
else
{
printf("\nAddition of matrix is not possible...");
}
return 0;
}

3. Write a program to check whether a word is palindrome or not.


#include<stdio.h>
#include<string.h>
void reverse(char s1[100])
{
int i,j;
char temp;
i=0;
j=strlen(s1)-1;
while(i<j)
{
temp=s1[i];
s1[i]=s1[j];
s1[j]=temp;
i++;
j--;
}
}

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;
}

4. What are bitwise and logical operators in C ?


Logical Operators:
Logical operators perform logical operations on a given expression by joining two or more
expressions or conditions.
Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then,

Operator Description Example

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then the condition
becomes true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition
becomes true.

! Called Logical NOT Operator. It is used to !(A && B) is true.


reverse the logical state of its operand. If a
condition is true, then Logical NOT operator
will make it false.

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 −

Operator Description Example

& Binary AND Operator copies a bit to the result if it (A & B) = 12,
exists in both operands. i.e., 0000 1100

| Binary OR Operator copies a bit if it exists in either (A | B) = 61,


operand. i.e., 0011 1101

^ Binary XOR Operator copies the bit if it is set in one (A ^ B) = 49,


operand but not both. i.e., 0011 0001

~ Binary One's Complement Operator is unary and has (~A ) = ~(60),


the effect of 'flipping' bits. i.e,. 1100 0011

<< Binary Left Shift Operator. The left operands value is


A << 2 = 240
moved left by the number of bits specified by the
i.e., 1111 0000
right operand.

>> Binary Right Shift Operator. The left operands value


A >> 2 = 15
is moved right by the number of bits specified by
i.e., 0000 1111
the right operand.
5. What are strings and give any four string related functions.
String is defined as character array.
It is an array of elements with data type as “char”
In string, we can store all alphabets, all digits, space and special symbols
Example:
char s1[10];
If we want to handle multiple strings or names, we can create 2D string
Example:
char s1[5][10];
Following are different string handling functions which are present in header file “string.h”

Function Syntax (or) Example Description

strcpy() strcpy(string1, string2) Copies string2 value into string1

strlen() strlen(string1) returns total number of characters in string1

strcat() strcat(string1,string2) Appends string2 to string1

strrev() strrev(string1) It reverses the value of string1

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

6. Implement a program to find transpose of a matrix.


#include<stdio.h>
int main()
{
int a[10][10],b[10][10];
int m,n,i,j;
printf("Enter number of rows and columns of matrix=");
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter Number=");
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
8. Distinguish between structure and union.

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

9. What are the tokens of c language explain with example.


Tokens in C is the most important element to be used in creating a program in C. We can
define the token as the smallest individual element in C.
There are 4 types of tokens:
1. Keywords:
Keywords are those words whose meaning is already known to the compiler
Example: include, void, main etc.
2. Variables and Constants:
Variables are those entities whose value can change throughout the program
Constants are those entities whose value remain same throughout the program
3. Identifiers:
Identifiers are names given to variables or functions
Rules of Identifiers:
1. It should not be keyword
2. It can contain all alphabets (A-Z and a-z), all digits (0-9) and special character underscore
(_)
3. It should either start with alphabet or underscore
4. Its maximum allowed length is 31
5. It is case sensitive
4. Operators:
Operators are used to perform different operations
C contains following 3 categories of operators:
1. Unary Operators:
It contains those operators which are used with single operand
2. Binary Operators:
It contains those operators which are used with two operands
3. Ternary Operators:
It contains those operators which are used with three operands

10. Explain while loop with example.


while Statement:
It is entry-controlled statement. It first checks the condition and then execute the statement; the
process gets continued until the condition becomes false.
The syntax of while loop in c language is given below:

while(condition)
{
Statement
}

Flowchart:

11. Write a program to print Fibonacci series.


#include<stdio.h>
int main()
{
int n,a,b,i,c;
printf("Enter number of terms=");
scanf("%d",&n);
a=1;
b=1;
printf("%d\t%d",a,b);
for(i=1;i<=n-2;i++)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
return 0;
}
12. Write a program using recursion to find factorial of a number.
#include<stdio.h>
int fact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
int main()
{
int n,f;
printf("Enter number=");
scanf("%d",&n);
f=fact(n);
printf("\nFactorial=%d",f);
return 0;
}

13. Explain nested structures with examples.


A nested structure in C is a structure within structure. One structure can be declared inside
another structure in the same way structure members are declared inside a structure. We can
access the member of the nested structure by
Outer_Structure_Variable.Inner_Structure_Variable.Inner_Structure_Element
Example:
#include<stdio.h>
struct student
{
int roll;
char name[30];
struct marks
{
int p,c,m;
}mk;//Inner structure ka variable outer structure ka element maana jaata hai
};

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;
}

14. Write a C program to perform multiplication of two matrices.


#include<stdio.h>
void accept(int x[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter Number=");
scanf("%d",&x[i][j]);
}
}
}

void display(int x[10][10],int m,int n)


{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",x[i][j]);
}
printf("\n");
}
}
void multiply(int a[10][10],int b[10][10],int c[10][10],int m1,int n1,int m2,int n2)
{
int i,j,k;
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
int main()
{
int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2;
printf("Enter number of rows and columns of matrix 1=");
scanf("%d%d",&m1,&n1);
printf("Enter number of rows and columns of matrix 2=");
scanf("%d%d",&m2,&n2);
if(n1==m2)
{
accept(a,m1,n1);
display(a,m1,n1);
accept(b,m2,n2);
display(b,m2,n2);
multiply(a,b,c,m1,n1,m2,n2);
printf("\nMultiplication Matrix=\n");
display(c,m1,n2);
}
else
{
printf("\nMultiplication of matrix is not possible");
}
return 0;
}

15. Explain conditional operator used in C language with proper example.

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.

Syntax of a conditional operator


Expression1? expression2: expression3;

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;
}

17. Explain following functions with example


sqrt(), fabs(), pow(), ceil(), floor()

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;
}

30. Explain five arithmetic operators used in C language with proper


examples.
Arithmetic Operators:

An arithmetic operator performs mathematical operations such as addition, subtraction,

multiplication, division etc on numerical values (constants and variables).


Operator Meaning of Operator

+ addition or unary plus

- subtraction or unary minus

* multiplication

/ division

% remainder after division (modulo 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

Merging of two strings.


The strcat() function concatenates the destination string and the source string, and the result is
stored in the destination string.

#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";

// concatenates str1 and str2


// the resultant string is stored in str1.
strcat(str1, str2);

puts(str1);
puts(str2);

return 0;
}

Output

This is programiz.com

programiz.com

32. Explain the term recursion. Write a program to find summation of n


numbers using recursion.
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.
#include <stdio.h>

int addNumbers(int n);

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]);
}
}

for(i = 0; i < m; i++)


{
for(j = 0;j < n;j++)
{
if(i==j)
{
Sum = Sum + a[i][j];
}
}
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );

return 0;
}

36. Explain the use of following in-built functions of C-language by giving


suitable programming examples and also mention their respective header
files in which they are defined.
i) getch()
ii) pow()
iii) ceil()
iv) puts()
v) getchar()

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 () {

/* local variable definition */


int a = 100;
int b = 200;

printf("Before swap, value of a : %d\n", a );


printf("Before swap, value of b : %d\n", b );

/* calling a function to swap the values */


swap(a, b);

printf("After swap, value of a : %d\n", a );


printf("After swap, value of b : %d\n", b );
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
return 0;
}

41. Give the difference between entry and exit controlled loop with an
example.

Entry Control Loop Exit Control Loop


Entry control loop checks condition The exit control loop first executes the body of the
first and then loop and
body of the loop will be executed. checks condition at last.
The body of the loop may or may The body of the loop will be executed at least once
not be executed at all. because the condition is checked at last
for, while are an example of an
Do…while is an example of an exit control loop.
entry control loop

42. Differentiate between arrays and structures.

No. Key Structure Array

Definition Structure can be defined as Array is a type of data


a data structure which can structure used as container
hold variables of different which can hold variables of
1
types. same type and do not
support multiple data type
variables.

Memory Memory allocation for input While in case of array the


Allocation data in structure does not input data stored in
2 necessary to be in contiguous memory .
consecutive memory
location.

Accessibility In order to access the On other hand in case of


element in Structure we Array we can access the
3
require to have the name of element by index.
that element.

Instantiation Structure object can be On other hand in case of


5 created after declaration Array we can't create its
later in the program. object after declaration.

DataType Structure supports multiple On other hand in case of


6 data-type variables as input. Array we can't have
different data-type variable
No. Key Structure Array

as input as it only supports


same type data variables.

Performance Structure due to use defined On other hand in case of


data type become slow in Array access and searching
performance as access and of element is faster and
7
searching of element is hence better in
slower in Structure as performance.
compare to Array.
}
Output:Data Science

• 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.

Passing Parameter to a Function:

In C Programming we have different ways of parameter passing schemes such as


Call by Value and Call by Reference.
Function is good programming style in which we can write reusable code that can
be called whenever require.
Whenever we call a function then sequence of executable statements gets executed.
We can pass some of the information to the function for processing called
argument.
Call by Value:
In case of call by value the function is invoked by a calling function, by passing
the actual values to be processed.
This method copies the value of an argument into the formal parameters of the
function.
By default, C programming language uses call by value method to pass arguments.
In general, this means that code within a function cannot alter the arguments used
to call the function.
In this method, the changes made to parameter have no effect on the argument.
Program:

#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:

In case of call by reference the function is invoked by a calling function, by


passing the address of the actual values to be processed.
In this method, the address of an argument is copied in the parameter.
Inside the sub routine; the address is used to access the actual argument in the call.
Program:

#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;
}

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