0% found this document useful (0 votes)
108 views

C Programming

This document provides information on various C programming concepts including character constants, variables, keywords, instructions, operators, and control structures. It defines rules for constructing character constants and variable names. It lists the four main instruction types in C and provides examples. It also describes various operators and control structures like if, else if, switch, while, do-while, and for loops. Examples of programs using these control structures to check if a number is prime, calculate grade based on percentage, find the largest of three values, etc. are included.

Uploaded by

Nitin Khanna
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

C Programming

This document provides information on various C programming concepts including character constants, variables, keywords, instructions, operators, and control structures. It defines rules for constructing character constants and variable names. It lists the four main instruction types in C and provides examples. It also describes various operators and control structures like if, else if, switch, while, do-while, and for loops. Examples of programs using these control structures to check if a number is prime, calculate grade based on percentage, find the largest of three values, etc. are included.

Uploaded by

Nitin Khanna
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 54

C Programming

Rules for Constructing Character Constants


1. A character constant is a single alphabet a single digits or a single
Special symbol enclosed within single inverted commas.
2. The Maximum length of a character constant can be 1 character.
Variables: In C a quantity that may vary during program execution is called a variable.
Rules for Constructing Variable Name
1. A variable name is any combination of alphabets, digits or underscores.
2. The first character in the variable name must be an alphabet.
3. No Commas or blank are allowed within a variable name.
4. No special symbol other than an underscore can be used in a variable name.
Keywords:
Keywords are the words whose meaning has already been explained to the C compiler
There are only 32 Keywords
Auto
Break
Case
Char
Const
Continue
Default
Do

double
else
enum
extern
float
far
for
goto

if

static
int
long
near
register
return
short
signed

struct
switch
typedef
union
unsigned
void
while

C Instructions
There are basically four types of instructions in C:
(a)
(b)
(c)
(d)

Type Declaration Instruction


Input/Output Instruction
Arithmetic Instruction
Control Instruction

(a) Type Declaration Instruction


This instruction is used to declare the type of variables being used in the program.
Ex.-

int bas;
Float rs,grosssal;
Char name,code;

(b) Input/Output Instruction


To perform the function of supplying input data to a program and obtaining
the output results from it.
Printf ()

Output

Scanf()

Input

(c ) Arithmetic Instruction
To perfome arithmetic operations between constants and
variables.
+ - * / % etc.

(d) Control Instruction


To Control the sequence of execution of various statement in a C
program
Operators
They are used to perform various operations on the variables.
Arithmetical operators (+ ,- ,* ,/ ,%)
Relational operators (>,<,<=,>=,= =,!=)
Logical operators (||, &&,!)
Increment / Decrement operators (++,--)
Assignment operator (=)

Program
1.Add Two Number
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
printf(Enter the Value of A & B \n);
scanf(%d%d,&a,&b);
sum=a+b;
printf(Sum=%d,sum)
getch();
}
2.Average of three Numbers
#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c,avg;
printf(Enter the Value of A , B & C \n);
scanf(%d%d%d,&a,&b,&c);
avg=a+b+c/3;
printf(Average=%d,avg)
getch();
}
3.Calculate the total of marks and percentage
#include<stdio.h>
#include<conio.h>
void main()
{
float m,s,e,h,tot,per;
int max=400;
printf(Enter the marks of Maths,Science,English,Hindi \n);
scanf(%f%f%f%f,&m,&s,&e,&h);
tot=m+s+e+h;
per=tot/max*100;
printf(Total=%f,tot);
printf(Percentage=%f,per);
getch();
}
The Decision Control Structure
Flow Chart
Example

Start

Dis=0
Input
Qty, rate

No

Is
Qty>1000

Yes

Dis=10

Tot=qty*rate-qty*rate*dis/100

Print
tot

Stop
Simple if
Suppose if you want to execute a part of program if a particular condition is true we can
enclose these statement within if {} statement
Syntax
If(condition)
{
Statement
}
PROGRAM
Check the number is >10
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(enter the value of n);
scanf(%d,&n);
if(n>10)
{
printf(number is grater than 10);
}
getch();
}
if .else
Suppose if you want to execute a part of program if a particular condition is true as well as
condition is false then we can enclose these statement within if ..else statement like.
Syntax
If(condition)

{
Statement
}
else
{
statement..
}
Program
Prime Number
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter the number");
scanf("%d",&num);
if(num==2)
{
printf("P");
}
else
{
if(num%2==0)
{
printf("np");
}
else
{
printf("P");
}
}
getch();
}
nested if else
Suppose if you want to execute a part of program in which a particular condition is depend
on the another condition then we can nested if ..else.
Syntax
If(condition)
{
If(condition)
{
-------

}
else
{
-------}
}
else
{
statement..
}
Disadvantage:
1 missing brace is always create a problem
2 Complex city increase
Program
Largest of three values:
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
printf(Enter three values\n);
scanf(%f%f%f,&a,&b,&c);
printf(\n Largest value is );
if(a>b)
{
if(a>c)
printf(%f\n,a);
else
printf(%f\n,c);
}
else
{
if(c>b)
printf(%f\n,c);
else
printf(%f\n,b);
}
getch();
}
else if
It remove the above problem but it has own problem it check all the condition until one
condition is true. So it is time consuming.
Syntax:If(condition1)

{
-------}
else if(condition2)
{
----}
else
{
---------}
Program
1.Calculate the total of marks and percentage & Grade
#include<stdio.h>
#include<conio.h>
void main()
{
float m,s,e,h,tot,per;
int max=400;
printf(Enter the marks of Maths,Science,English,Hindi \n);
scanf(%f%f%f%f,&m,&s,&e,&h);
tot=m+s+e+h;
per=tot/max*100;
printf(Total=%f,tot);
printf(Percentage=%f,per);
if(per>=60)
{
printf(1 st Div);
}
else if (per>=45)
{
printf(2 nd div);
}
else if (per>=33)
{
printf(3rd Div);
}
else
{
printf(fail);
}
getch();
}
2 Largest of three values:
#include<stdio.h>
#include<conio.h>
void main()
{

int a,b,c;
printf(Enter three values\n);
scanf(%d%d%d,&a,&b,&c);
printf(\n Largest value is );
if(a>b && a>c)
{
printf(%d\n,a);
}
elseif(b>a && b>c)
{
printf(%d\n,b);
}
else
{
printf(%d\n,c);
}
getch();
}
Case Control Statement
Switch :- it works on choice which user enters. it remove the problem which created by the
else if ladder and save the cup time
Syntax:
Switch(value)
{
Case 1:
----break;
case 2:
----break;
case 3:
----break;
default:
--------}
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("enter the value ");
scanf("%d",&i);
switch(i)

{
case 1:
printf("sunday\n" );
break;
case 2:
printf("moday\n" );
break;
case 3:
printf("tus\n" );
break;
case 4:
printf("web\n" );
break;
default:
printf("No number in case\n");
}
getch();
}
LOOP
Loop are two type
1.Entry Control loop: - In which condition is checked at the entry point for the
execution of the loop. For and while loop comes in this category
2.Exit control loop: - In which condition is checked at the last point for the execution of
the loop. Do while loop is exit control loop. It execute at least once.
1. While Loop: - In while loop we first write the initial value and then we
checks the condition if the condition is true it enters the loop otherwise it
comes out. In true condition it executes all the statement inside the curly
braces. And then again checks. The condition this process repeats till the
condition is true.
Syntax
Initialization;
While (Condition checking)
{
//body of the loop
Increment/decrement;
}
Program
1.Wrtie a 'c' program to find the sum of digits of a number
e.g.
num = 456
sum=4+5+6= 15
*/
#include<stdio.h>
#include<conio.h>
main()
{
int num,sum;

clrscr( );
printf("\nEnter the number :");
scanf("%d",&num);
sum=0;
while(num!=0)
{
sum=sum+num%10;
num=num/10;
}
printf("\nSum of digits = %d",sum);
getch();
}
2.Password
#include<stdio.h>
#include<conio.h>
void main()
{
char ch,s[50];
int i;
ch=getch();
i=0;
while(ch!='@')
{
s[i]=ch;
printf("*");
ch=getch();
i++;
}
s[i]='\0';
printf("\n\nYour Password : %s",s);
getch();
}

3.Fatcorial Number
#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact;
printf("enter the value of num:");
scanf("%d",&num);
fact=1;
while(num>1)
{
fact=(fact*num);
num=num-1;
}
printf("%d",fact);
getch();
}
2. Do While:-do while loop first execute the loop and then it checks the condition if
the condition is true it enters the loop next otherwise it come out. So this loop
will run at least once.
Syntax:
Initialization;
do
{
// body of the loop
increment/decrement;
}
while( condition checking);
Program
1 WAP to read a number and check whether it is palimdrome
or not.
num=121 (madam)
rev=121
Number is Palindrome
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int num,rev,org;
clrscr();
printf("\nEnter the number :");
scanf("%d",&num);

rev=0;
org=num;
do
{
rev=rev*10+num%10;
num=num/10;
}
while(num!=0);
if(rev==org)
printf("\nNUmber is Palindrome");
else
printf("\nNumber is not palindrome");
getch();
}

Question 4: WAP to read a number and check whether it is Armstrong or not.


num=153
sum=1^3+5^3+3^3=1+125+27=153
Number is Armstrong
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int num,sum,org;
clrscr();
printf("\nEnter the number :");
scanf("%d",&num);
sum=0;
org=num;
do
{
sum=sum+(num%10)*(num%10)*(num%10);

num=num/10;
}
while(num!=0);
if(sum==org)
printf("\nNUmber is Armstrong");
else
printf("\nNumber is not Armstrong");
getch();
}
3. For Loop: - In for loop first we give the initial value and then.it checks
the condition if the condition is true then it enters the loop and execute all
the statement inside the curly braces and after this it go to the third part
that is increment/decrement. And it again checks the conditions. In this
way it runs till the condition is true. When the condition becomes false it
come out of the loop. .
syntax

for(Initialization; Condition;Icrement/decrement)
{
l/body of the loop
}
Program

Question 1: Write a 'c' program to read the value of x and y and find xto power y.
e.g.
x=5

y=3
Result = 5^3=5*5*5=125

*/
#include<stdio.h>
#include<conio.h>
main( )
{
int x,y,i,result;
clrscr();
printf("\nEnter the value of x and y :");
scanf("%d %d",&x,&y);
result=1;
for(i=1;i<=y;i++)
{
result=result*x;
}

printf("\nResult =%d",result);
getch( );
}
/* WAP to read the value of n and generate the following pattern
n=4
*
i=1
**
i=2
*** i=3
**** i=4
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/* WAP to read the value of n and generate the following pattern
n=4
****
***
**
*
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;

clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/* WAP to read the value of n and generate the following pattern
n=4
1
12
123
1234
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

/* WAP to read the value of n and generate the following pattern


n=4
1234
123
12
1
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
/*
Wap to generate the following pattern ,
n=4
1
22
333
4444
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i,j,n,sp;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);

sp=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=sp;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
sp--;
}
getch();
}
/*
Wap to generate the following pattern ,
n=4
4444 i=4
333 i=3
22 i=2
1
i=1
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i,j,n;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

/*
Wap to generate the following pattern ,
n=4
1 i=1
00 i=2
111 i=3
0000 i=4
*/
#include<stdio.h>
#include<conio.h>
main( )
{
int i,j,n,term;
clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
term=0;
else
term=1;
for(j=1;j<=i;j++)
{
printf("%d",term);
}
printf("\n");
}
getch();
}
/*
Question 3: Write a 'c' program to read the number and find the reverse
num=435
rev=534
*/
#include<stdio.h>
#include<conio.h>
main( )
{

int num,rev;
clrscr();
printf("\nEnter the number :");
scanf("%d",&num);
for(rev=0;num!=0;num=num/10)
{
rev=rev*10+num%10;
}
printf("\nReverse = %d",rev);
getch();
}

/*
Question : WAP to read the value of n and display the following pattern
n=4
1
11
111
1111

0*10+1=0+1=1
1*10+1=10+1=11
11*10+1=110+1=111
111*10+1=1110+1=1111

*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,term;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
term=0;
for(i=1;i<=n;i++)
{
term=term*10+1;
printf("\n%d",term);

}
getch();
}
/*
Write a program to generate the following pattern
n=4
*
***
*****
*******
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
Write a program to generate the following pattern
n=4
*******
*****
***
*
*/
#include<stdio.h>
#include<conio.h>
main()

{
int i,j,n;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
/*
Write a program to generate the following pattern
n=4
1
23
456
78910
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n,term=1;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",term);
term++;
}
printf("\n");
}

getch();
}
/*
Write a program to generate the following pattern
n=4
1
23
345
4567
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n,term=1;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",term);
term++;
}
printf("\n");
}
getch();
}
/*
Write a program to generate the following pattern
n=4
*
**
***
****
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n,sp;

clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
sp=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=sp;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("*");
}
sp--;
printf("\n");
}
getch();
}
/*
Write a program to generate the following pattern
n=4
****
***
**
*
*/
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,n,sp;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
sp=0;
for(i=n;i>=1;i--)

{
for(j=1;j<=sp;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("*");
}
sp++;
printf("\n");
}
getch();
}
Arrays:
An Array is a collection of elements .In the case of the array all the
elements
are identified by the same array name but the different index
number or the sub-script
reference.
The general form of defining the array is ,
datatype arrayname[sizeofarray];
e.g.
int x[5];
This statement will declare the array with the name x and of size 5 ,
and the elements of the array x will be ,
x[0] , x[1] , x[2] , x[3] and x[4] .
In this case , all elements are identified with the same name x and the
index
number will starts from 0 and ranges from 0 to n-1 ,where n is the size of the array.
All the elements of the array are of same type , thus , the array is
nature.
Initialisation of array :
(a) int x[5]={10,12,14,16,19};
x[0]=10
x[1]=12
x[2]=14
x[3]=16
x[4]=19

homogenous in

All the elements through which we want to initialise the array are given in the curley
braces and are separated by the comma.
(b) int x[5]={10,12,14};
x[0]=10
x[1]=12
x[2]=14
and the remaining elements will get initlaised with zero.
(c) int x[3]={0};
In this case , all the elements will get intialised with zero.
(d) int x[ ] = {10,12,14,16,18};
x[0]=10
x[1]=12
x[2]=14
x[3]=16
x[4]=18
And the size of the array will get set to 5.As we have given the 5 elements within the
curley braces.
(e) int x[5]={10,3,44,55,21,23,445,65,656,777,555,34544};
Error, Too Many Intialisers.
Q Write a 'c' program to read and display the array
#include<stdio.h>
#include<conio.h>
main( )
{
int x[50],i,n;
clrscr();
/*read the array */
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element :");
scanf("%d",&x[i]);
}

/*display all the elements of array */


for(i=0;i<n;i++)
{
printf("\nData = %d",x[i]);
}
getch();
}
Q . Write a "C" program to read the array and find the sum of all the elements of the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[50],i,n,sum;
clrscr();
/*read the array */
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the value of n:");
scanf("%d",&x[i]);
}
/* find the sum of all the elements of the array */
sum=0;
for(i=0;i<=n-1;i++)
{
sum=sum+x[i];
}
printf("\nSum = %d",sum);
getch();
}
Q Write a "C" programs to find the maximum elements out of an array.
#include<stdio.h>
#inclulde<conio.h>

void main()
{
int x[50],i,n,max;
clrscr();
/*read the array*/
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the value of element:");
scan("%d",&x[i]);
}
/*find the maximum element out of an array */
max=x[0];
for(i=1;i<n;i++)
{
if(x[i]>max)
max=x[i];
}
printf("\nMaximum = %d",max);
getch();
}
Q WAP to read the array and find the maximum as well as the minimum
out of an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[50],i,n,max,min;
printf("\nEnter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element :");
scanf("%d",&x[i]);
}
/*find the maximum as well as minimum */

elements

max=x[0];
min=x[0];
for(i=1;i<n;i++)
{
if(x[i]>max)
max=x[i];
else
if(x[i]<min)
min=x[i];
}
printf("\nMaximum = %d , Minimum = %d",max,min);
getch();
}
Q Write a "C" program to read the value of n and find the position of an
array.

element in the

Suppose the array contains the following elements ,


5,10,15,20,25
and we want to search for the value , 15 ,
then the program will return the position 2
as the index starts with 0.
And suppose we want to search , 80 , then the program will return -1 , which is an indication
that the search value is not found.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[50],i,n,position,sval;
clrscr();
/*read the array*/
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element :");
scanf("%d",&x[i]);
}
/*read the value you want to search */

printf("\nEnter the search value :");


scanf("%d",&sval);
/*search for the value */
position=-1;
for(i=0;i<n&&position==-1;i++)
{
if(x[i]==sval)
position=i;
}
if(position==-1)
printf("\nNot found");
else
printf("\nPosition = %d",position);
getch();
}
Question : WAP to read two arrays and find the union .
Array 1 : 5,10,15,20,25,30
Array 2 : 10,20,45,60,70
Array 3 : 5,10,15,20,25,30,45,60,70
#include<stdio.h>
#include<conio.h>
void main()
{
int x[50],y[50],z[50],i,j,k,m,n,flag;
clrscr();
/*read the array */
printf("\nEnter the value of m:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nEnter the element :");
scanf("%d",&x[i]);
}

/*read the second array */


printf("\nEnter the value of n:");
scanf("%d",&n);
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&y[j]);
}
/*find the union*/
/*copy the first array into the third*/
k=0;
for(i=0;i<m;i++)
{
z[k]=x[i];
k++;
}
/*copy only the uncommon elements of array second into the third */
for(j=0;j<n;j++)
{
flag=0;
for(i=0;i<k;i++)
{
if(y[j]==z[i])
flag=1;
}
if(flag==0)
{
z[k]=y[j];
k++;
}
}
/* display the union of two arrays*/
for(i=0;i<k;i++)

{
printf("\nData = %d",z[i]);
}
getch();
}
Question : WAP to read two arrays and find the insertion.
Array 1 : 5,10,15,20,25,30
Array 2 : 10,20,45,60,70
Array 3 : 10,20
#include<stdio.h>
#include<conio.h>
void main()
{
int x[50],y[50],z[50],i,j,k,m,n;
clrscr();
/*read the array */
printf("\nEnter the value of m:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nEnter the element :");
scanf("%d",&x[i]);
}
/*read the second array */
printf("\nEnter the value of n:");
scanf("%d",&n);
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&y[j]);
}
/*find the intersaction*/
k=0;
for(i=0;i<m;i++)

{
for(j=0;j<n;j++)
{
if(x[i]==y[j])
{
z[k]=x[i];
k++;
}
}
}
/* display the insertion of two arrays*/
for(i=0;i<k;i++)
{
printf("\nData = %d",z[i]);
}
getch();
}
Double - Dimension Array :
In the case of the double dimension arrays or 2d arrays , we have the concept of arranging
the elements in the tabular form, i.e. , in rows and columns.
The rows are horizontal and the columns are vertical.
The general form of defining the 2d array is ,
datatype arrayname[nos of rows][nos of columns];
e.g.
int x[5][3];
This statement will declare the 2-d array containing the 5 rows and 3
columns, and the array will be declared with the name x.
Q Write a 'c' program to read and display the 2-d array.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[50][50],i,j,m,n;
clrscr();
/*read the matrix */
printf("\nEnter the value of m and n :");
scanf("%d %d",&m,&n);

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&x[i][j]);
}
}
/*display the matrix*/
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
getch();
}
Q Write a 'c program to read the matrix and find its transpose.

Transpose :

#include<stdio.h>
#include<conio.h>
void main()

{
int x[50][50],y[50][50],i,j,m,n;
clrscr();
/*read the matrix */
printf("\nEnter the value of m and n :");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&x[i][j]);
}
}
/*display the matrix*/
clrscr();
printf("\nOriginal Matrix is : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
/*find the transpose */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
y[j][i]=x[i][j];
}
}
/*print the transpose */
printf("\n\n Transpose : \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)

{
printf("%d\t",y[i][j]);
}
printf("\n");
}
getch();
}
Q . Wap to read the two arrays and find the sum .
#include<stdio.h>
#include<conio.h>
void main( )
{
int x[50][50],y[50][50],z[50][50],i,j,m,n;
clrscr();
/*read the first matrix*/
printf("\nEnter the value of m and n:");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&x[i][j]);
}
}
/*read the second matrix */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&y[i][j]);
}
}
/*add the matrices*/
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
z[i][j]=x[i][j]+y[i][j];

}
}
/*display the matrix*/
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",z[i][j]);
}
printf("\n");
}
getch();
}
Q . WAP to read the two matrices and multiply the two matrices.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x[50][50],y[50][50],z[50][50],i,j,k,m,n,p,q;
clrscr();
/*read the first matrix*/
printf("\nEnter the value of m and n:");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter the element :");
scanf("%d",&x[i][j]);
}
}
/*read the second matrix */
printf("\nEnter the value of p and q:");
scanf("%d %d",&p,&q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{

printf("\nEnter the element :");


scanf("%d",&y[i][j]);
}
}
if(n!=p)
{
printf("\nCannot Multiply");
return;
}
/*multiply the matrices*/
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
z[i][j]=0;
for(k=0;k<n;k++)
{
4

z[i][j]=z[i][j]+x[i][k]*y[k][j];
}
}
}
/*display the matrix*/
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",z[i][j]);
}
printf("\n");
}
getch();
}
Functions:

Function is the block of code which is used to perform a particular operation.


There are two type of the functions :
(i) Pre-defined functions
(ii) User -defined functions .

Monolithic Programming : In this case , we write the entire login in the single program/
The disadvantages of the monolithic programming are,
(a) Difficult to understand.
(b) Difficult to modify
(c) Difficult of debug.
[Bug means computer program error and debugging is
and removing the computer program error.]

the process of locating

Modular Programming : In the case of the modular programming , we divide the entire
complex program into the simple sub-programs or modules.These modules are also known
as functions.
Advantages of the modular programming :
(a) Easy to understand
(b) Easy to debug
(c) Easy to modify
(d) Resuability of code.
(i) Pre-defined Functions : These functions are defined in the header files.
e.g. stdio.h (printf() , scanf( ))
conio.h ( clrscr( ) , getch( ) )
(ii) User-defined functions : These functions are defined by the user for there own prupose.
These functions contains the following components :
(a) Function Prototype.
(b) Function call
(c) Function definition
(a) Function Prototype : The function prototype is used to
specify the layout of the function.
The function prototype specifies the following information :
(i) The function name
(ii) The number of arguments the function requires
(iii) The type of arguments
(iv) The returntype.

The general form is ,


returntype functionname(argument list);
Example 1 :
int sum(int ,int);
It specifies that there will be a function with the name
sum( ) and this function will take two arguments and
and the sum() will also return
the integer type value.

both are of type integers

float average(float,float,float);
It speicifes that there will be a function with the name
average( ) and this
function will take the three arguments
and all of them are of type float and the
average( ) will
return the float type value .
void show(char , int );
It speicfies that there will be a function with the name
show( ) and this function
will take the two arguments and the first argument will be of type char and the second
argument will be of the type int and the function
will not return any value.
(b) Function Call :
Consider the following statement ,
main( )
{
::

:
:

:
:

:
:

clrscr( );
:
:

:
:

:
:

}
In the above mentioned code , we are calling the
function in the main( ) .
The clrscr() function will be known as the Called
main( )
function will be known as
function .
The function call transfers the control from the
funtion to the called
function.The function
execution of the function
There are two types of the function calls,

clrscr()
Function and the
the calling
calling
call results in the
definition.

(i) When the function returns a value .


The general form is,
variablename=functionname(argument list);
e.g.
c=sum(a,b);
(ii) When the function doesnot return a value .
The general form is ,
functioname(argument list);
e.g.
show(ch,n);
(c) Function definition :
The function definition will define the actual logic of
function .
The general form of the function definition is ,
returntype functionname(argument list)
{
body of the function
}
Q Write a "C" function to add two numbers.
#include<stdio.h>
#include<conio.h>
/*function prototype*/
int sum(int,int);
void main( )
{
int a,b,c;
clrscr();
printf("\nEnter the value of a and b:");
scanf("%d %d",&a,&b);
c=sum(a,b);/*function call*/
printf("\nSum = %d",c);

the

getch();
}
/*function definition */
int sum(int i,int j)
{
int k;
k=i+j;
return k;
}
(b) When the function does not return the value .
#include<stdio.h>
#include<conio.h>
/*function prototype*/
void sum(int,int);
void main( )
{
int a,b;
clrscr();
printf("\nEnter the value of a and b:");
scanf("%d %d",&a,&b);
sum(a,b);/*function call*/

getch();
}
/*function definition */
void sum(int a,int b)
{
int c;
c=a+b;
printf("\nSum = %d",c);
}
Q Write a 'c' function to calculate the factorial of the number
(i) When the function returns the value
#include<stdio.h>

#include<conio.h>
/*function prototype*/
int factorial(int);
void main( )
{
int num,fact;
clrscr();
printf("\nEnter the number :");
scanf("%d",&num);
fact=factorial(num); /*function call*/
printf("\nFactorial = %d",fact);
getch();
}
/*function defintion */
int factorial(int num)
{
int i,fact;
fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}
(ii) When the function does not return the value
#include<stdio.h>
#include<conio.h>
/*function prototype */
void factorial(int);
void main()
{
int num;
clrscr( );
prindtf("\nEnter the number :");
scanf("%d",&num);
factorial(num); /* function call*/
getch();

}
/* function definition */
void factorial(int num)
{
int i,fact;
fact=1;
for(i=1;i<=num;i++)
fact=fact*i;
printf("\nFactorial = %d",fact);
}
Q Write a 'c' program using function to calculate the value
of c.
c = n!
---------r!*(n-r)!
#include<stdio.h>
#include<conio.h>
/*function prototype*/
int factorial(int);
void main( )
{
int n,r,c,factn,factr,factnr;
clrscr();
printf("\nEnter the value of n and r:");
scanf("%d%d",&n,&r);
/*calculate the factorial of n */
factn=factorial(n);
/*calculate the factorial of r */
factr=factorial(r);
/* calculate the factorail of n-r*/
factnr=factorial(n-r);
/*calculate the value of c */
c=factn/(factr*factnr);

printf("\nc=%d",c);
getch();
}
/*function definition */
int factorial(int num)
{
int fact=1,i;
for(i=1;i<=num;i++)
fact=fact*i;
return fact;
}
Q Write a 'C' function to calculate the x ^ y.
x=5 y=3 x^ y=5^3=5*5*5=125
(i) When the functin returns the value
(ii) When the function doesnot returns the value .
Solution :
#include<stdio.h>
#include<conio.h>
/*function prototype*/
int power(int,int);
void main()
{
int x,y,result;
clrscr();
printf("\nEnter the value of x and y :");
scanf("%d %d",&x,&y);
result=power(x,y); /*function call*/
printf("\nResult = %d",result);
getch();
}
/*function defintion*/
int power(int x,int y)
{
int i,result;

result=1;
for(i=1;i<=y;i++)
result=result*x;
return result;
}
Strings:
String is a sequence or a group of characters. The char type variable is capable of storing a
single character while the character array is capable of storing the group of characters.
So,string is just a character array.
The general form of string declaration is ,
char stringname[size of array];
e.g.
char s[50];
This statement will declare the character arrray capable
characters.

of storing the 50

Initialisation of string
To initialise the string there are two ways,
(a) char str[5]={'a','j','a','y','\0'};
In this case , all the characters are enclosed in the single
within the curley braces separated by comma.

quotes and are given

'\0' is the NULL character and is used to end the string.


(b) char s[5]="ajay";
When the string is initialised by enclosing the group of
characters in the double
quotes , the '\0' or NULL
character will automatically get inserted.
Reading and Displaying the strings :
To read and display the string we will again used the scanf( ) and printf( ) functions
respectively, but the format specifier will be %s.
Consider the following program,
#include<stdio.h>
#include<conio.h>
void main( )

{
char s[50];
clrscr( );
printf("\nEnter the string :");
scanf("%s",s);
printf("\nYour string : %s",s);
getch();
}
Note : When we read the string we only specify the string name , because string is an array
and the array name will specify the base address i.e. address of the zeroth position element.
Thus , the statements ,
scanf("%s",s);
and,
scanf("%s",&s[0]);
are same.
output :
Enter the string : computer
Your string : computer
Enter the string : computer science
Your string : computer
This is because the scanf( ) function will get terminated by pressing the space.
But if you want to read the multiple words string , we have to make use of the gets( )
function.
gets( ) : This function is present in the header file stdio.h and
is used to read the string which can even contain the
spaces.Thus,the gets( ) will get terminated only by
pressing the enter.
The general form is ,
gets(stringname);
Consider the following program,
#include<stdio.h>
#include<conio.h>
void main( )
{
char s[50];

clrscr( );
printf("\nEnter the string :");
gets(s);
printf("\nYour string : %s",s);
getch();
}
output :
Enter the string : computer science
Your string : computer science
In "C" programming language , we are provided with a separate file "string.h" which will be
used for the string handling.
It contains various pre-defined functions,which will used for manipulating the strings.
(a) strlen( ) :
This function will calculate the length of the
string.by length,we means the number of
characters present in the string,excluding the
NULL character.
The general form is ,
int strlen(char [ ])
Consider the following ,
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[50];
int len;
clrscr( );
printf("\nEnter the String :");
scanf("%s",s);
len=strlen(s);
printf("\nLength=%d",len);
getch( );

}
output :
Enter the string : ajay
Length=4
Q Write a 'c' program to calculate the length of the string .
(without using the library function)
#include<stdio.h>
#include<conio.h>
void main( )
{
char s[50];
int i;
clrscr( );
printf("\nEnter the string :");
gets(s);
i=0;
while(s[i]!='\0')
{
i++;
}
printf("\nLength=%d",i);
getch( );
}
Q Write a "C" program to read the string and convert it into the uppercase.
String : Ajay
output :AJAY
'a' : ASCII value 97
'A' : ASCII value 65
#include<stdio.h>
#include<conio.h>
void main( )
{
char s[50];

int i;
clrscr( );
printf("\nEnter the string :");
gets(s);
i=0;
while(s[i]!='\0')
{
if(s[i]>='a'&&s[i]<='z')
{
/*lowercase,so convert it into uppercase*/
s[i]=s[i]-32;
}
i=i+1;
}
printf("\nResult : %s",s);
getch( );
}
(b) strcpy( ) :
This function is used to copy the one string
into another.
The general form is ,
void strcpy(target,source);
Consider the following program ,
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[50],str2[50];
int i;
clrscr( );
printf("\nEnter the string :");
scanf("%s",str1);

strcpy(str2,str1);
printf("\nResult : %s",str2);
getch( );
}
output :
Enter the string : computer
Result : computer
Q Write a "C" program to copy the one string into another without using the library
functions.
#include<stdio.h>
#include<conio.h>
void main( )
{
char str1[50],str2[50];
int i;
clrscr( );
printf("\nEnter the string :");
gets(str1);
i=0;
while(str1[i]!='\0')
{
str2[i]=str1[i];
i=i+1;
}
str2[i]='\0';
printf("\nResult : %s",str2);
getch( );
}
(c) strcat( ) :
This function is used to concatenate two string.By concatenation , we means that the
second string is added at the end of the first string.
The general form is ,
void strcat(string1,string2);

Consider the following program ,


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[50],str2[50];
int i;
clrscr( );
printf("\nEnter the first string :");
scanf("%s",str1);
printf("\nEnter the second string :");
scanf("%s",str2);
strcat(str1,str2);
printf("\nResult : %s",str1);
getch( );
}
output :
Enter the first string : Jatin
Enter the second string :Lalit
Result:JatinLalit
Q Write a "C" program to concatenate the one string into
library functions.
#include<stdio.h>
#include<conio.h>
void main( )
{
char str1[50],str2[50];
int i,j;
clrscr( );
printf("\nEnter the first string :");
gets(str1);
printf("\nEnter the second string :");
gets(str2);
/*move to the end of the first string*/

another

without using the

i=0;
while(str1[i]!='\0')
{
i=i+1;
}
/*Copy second string into first string*/
j=0;
while(str2[j]!='\0')
{
str1[i]=str2[j];
i=i+1;
j=j+1;
}
str1[i]='\0';
printf("\nResult : %s",str1);
getch( );
}
Q1. Write a "c" program to reverse the string .
String : kapil
Reverse:lipak
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
char str[50],rev[50];
int i,n,j;
/*read the string */
printf("\nEnter the string : ");
gets(str);
/*find the length of the string */
n=strlen(str)-1;

i=n;
j=0;
while(i>=0)
{
rev[j]=str[i];
j++;
i--;
}
rev[j]='\0';
printf("\nReverse = %s",rev);
getch();
}
Q2. Write a "c" program to generate the following pattern
String : jack
Pattern
j
ja
jac
jack
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50];
int i,j,n;
clrscr();
printf("\nEnter the string :");
gets(str);
/*calculate the length of the string */
n=strlen(str)-1;
for(i=1;i<=n;i++)
{
for(j=0;j<i;j++)
{

printf("%c",str[j]);
}
printf("\n");
}
getch();
}

You might also like