Bca
Bca
C Programming language provides many built-in function to read given input and
write data on screen, printer or in any file.
The header file required by the standard input/output functions is called stdio.h
Single charactercan be entered into the computer using the C library function
getchar().
The getchar() function is a part of the standard C I/O library. It returns a single
character from a standard input device( typically a keyboard). The function does not
require any arguments, though a pair of empty parentheses must follow the word
getchar().
Syntax
variable =getchar();
char ch;
ch=getchar();
The first statement declares that ch is a character type variable. The second
statement causes a single character to be entered from the standard input device
and then assigned to ch.
Syntax
putchar(variable)
Example
char ch;
putchar(ch);
The first statement declares that ch is a character type variable. The second
statement causes the current value of ch to be transmitted to the standard output
device where it will be displayed.
Input data can be entered into the computer from a standard input device by
means of the C library function scanf(). This function can be used to enter any
combination of numerical values, single characters and strings. The function
returns the number of data items that have been entered successfully.
Syntax
The control string consists of individual groups of characters, with one character
group for each input data item. Each character group must begin with a percent
sign (%) followed by a conversion character which indicate the type of the
corresponding data item.
Example-1:
int x;
scanf(“%d”, &x);
Example-2:
int x,y;
scanf(“%d%d”, &x,&y);
Example-3:
int x,y;
float price;
x, y are integer data type variable, price is float data type variable.
scanf(“%d%d%f”, &x,&y,&price);
Output data can be written from the computer onto a standard output device
using the library function printf(). This function can be used to output any
combination of numerical values, single characters and strings.
Syntax
where control string refers to a string that contains formatting information, and
arg1,agr2,.agrn are arguments that represent the individual output data items.
Example-1:
Example-2:
int x=10;
printf(“%d”, x);
it display 10
Example-3:
int x=10;
Example-4:
float S=123.456;
a) if Statement
b) if-else Statement
c) Nested if-else Statement
d) if-elseif ladder
e) switch case
a) if Statement
If statement is a conditional branching statement. In conditional branching
statement a condition is evaluated, if it is evaluate true a group of statement is
executed. The simple format of an if statement is as follows:
Syntax:
if(condition)
{
statements;
}
In above syntax, the condition is checked first. If it is true, then the program
control flow goes inside the braces and executes the block of statements
associated with it. If it returns false, then program skips the braces.
If there are more than one statements in if statement then use { } braces else it
is not necessary to use.
Example:
if (number>0)
If the value of the number is greater than 0, then the condition is true, it displays
the message Given number is Positive on the screen; otherwise the statement
is skipped.
b) if-else Statement
The if-else statement permits the programmer to write a single comparison, and
then execute one of the two statements depending upon whether the condition is
true or false.
Syntax:
if(condition)
{
Statements1;
}
else
{
Statements2;
}
Example:
if (number==0)
else
If the value of the number is 0(zero), then statement1 “Given number is Zero”
is displayed on the screen; otherwise the statement2”Given number is not
Zero” is displayed on the screen.
c) Nested if Statement
An if statement containing one more if statement either in true part or in else
part is called nested if statement.
Syntax:
if(condition1)
{
if(condition2)
{
Statement1;
}
else
{
Statement2;
}
}
else
{
Statement3;
}
Example:
if (number==0)
else
if (number>0)
else
}
d) If-Else-If Ladder Statement
Consider the example, to display the student‟s grade based on the following
table:
e) switch Statement
Switch statement accepts single input from the user and based on that input
executes a particular block of statements.
You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
The switch expression requires only one argument of int or char data type,
which is checked with number of case options.
when a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at
the end of the switch. The default case can be used for performing a task when
none of the cases is true. No break is needed in the default case.
Here switch, case, break and default are reserved words or keywords.
Syntax
switch (expression)
{
case constant1:
statement-1;
break;
case constant2:
statement-2;
break;
..........
default:
statement-n;
}
Example 1:
switch(dayno)
{
case 1: printf(“Sunday”);
break;
case 2: printf(“Monday”);
break;
case 3: printf(“Tuesday”);
break;
case 4: printf(“Wednesday”);
break;
case 5: printf(“Thursday”);
break;
case 6: printf(“Friday”);
break;
case 7: printf(“Saturday”);
break;
default: printf(“Invalid day number””);
}
Example 2:
switch(Light)
{
case „R‟: printf(“RED Light Please STOP”);
break;
case „Y‟: printf(“YELLOW Light Please Check and Go”);
break;
case „G‟: printf(“GREEN Light Please GO”);
break;
default: printf(“THERE IS NO SIGNAL POINT ”);
}
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("enter a character\n");
ch=getchar();
switch(toupper(ch))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':printf("vowel");
break;
default:printf("consonant");
}
getch();
}
Program to find the roots of the given quadratic equation using switch case.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,disc,x1,x2,xi,xr,x; int choice=1;
clrscr();
printf("input co-efficient\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>0) choice=1;
else if(disc<0)choice=2;
else if(disc==0)choice=3;
switch(choice)
{
case 1:printf("real and distinct roots\n");
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("x1=%.2f\n",x1);
printf("x2=%.2f\n",x2);
break;
case 2:printf("complex roots\n");
xr=-b/(2*a);
xi=sqrt(abs(disc))/(2*a);
printf("real part=%.2f\n",xr);
printf("imaginary part=%.2f\n",xi);
break;
case 3:printf("repeated roots\n");
x=-b/(2*a);
printf("x=%.2f\n",x);
}
getch();
}
Loops Statements
A loop statement allows us to execute a statement or group of statements
multiple times
a) while Loop
A while loop statement in C programming language repeatedly executes
the statement as long as a given condition is true. Here, statement(s)
may be a single statement or a block of statements.
The while loop checks whether the condition is true or not. If it is true,
code/s inside the body of while loop is executed, that is, code/s inside the
braces { } are executed. Then again the condition is checked whether
condition is true or not. This process continues until the condition becomes
false.
while loop is called as top tested loop, because the condition is tested at the
top (ie prior to entering into the loop).
Syntax:
while(condition)
{
statements;
}
Example:
x=1;
while(x<5)
{
printf(“Hello”);
x=x+1
}
The above code displays the output as
Hello
Hello
Hello
Hello
#include<stdio.h>
#include<conio.h>
void main()
{
int n,t,r,rev=0;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
t=n;
while(n>0)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
printf("the reverse of a number is %d\n",rev);
if (t==rev)
printf("The number is palindrome");
else
printf("The number is not a palindrome");
getch();
}
b) do…while Loop
In do...while loop, the body of loop is executed at first then the condition is
checked, So the statements are executed at least once in do...while loop.
do…while loop is called bottom tested loop, since the condition is checked at
bottom.
Syntax:
do
{
statements;
}
while(condition);
Example:
x=1;
do
{
printf(“Hello”);
x=x+1
}
while(x<5);
The above code displays the output as
Hello
Hello
Hello
Hello
c) for Loop
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of times.
The init(initialization) expression is executed only once at the beginning of the for
loop. Then the condition is checked. If the condition is false, for loop is
terminated. But if condition is true then the statements inside body of for loop is
executed and then increment expression executed. This process repeats until
condition is false.
Syntax
for (init; condition; increment)
{
statements;
}
Example:
Loop body will execute if condition Loop body will be executed at-least once
is true only. even thou the condition is failed
Semicolon is not used after while Semicolon is used after while condition
condition
Like a while statement, except that it tests the condition at the end
do...while loop
of the loop body
You can use one or more loop inside any another while, for or
nested loops
do..while loop.
Program to display the number upto N terms using for loop
void main()
{
int x,n;
clrscr();
printf("Enter the value for n\n");
scanf("%d",&n);
for(x=1; x<=n; x++)
{
printf("%d\t",x);
}
getch();
}
Program to display odd number upto N terms
void main()
{
int x,n;
clrscr();
printf(“Enter the value for n \n”); scanf(“%d”,&n);
printf(“\n The odd numbers are \n”);
for(x=1; x<=n; x=x+2)
{
printf("%d\t”,x)
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=3,f1=0,f2=1,f3,n;
clrscr();
printf("\nHow many terms:");
scanf("%d",&n);
printf("%d \t %d ",f1,f2);
for(i=3;i<=n;i++)
{
f3=f1+f2;
printf("%d ",f3);
f1=f2;
f2=f3;
}
getch();
}
Output
When you run this program you will get the following output:
r=1c=1
r=1c=2
r=2c=1
r=2c=2
r=3c=1
r=3c=2
Here, for each value of r the inner loop is cycled through twice, with the variable c
taking values from 1 to 2. The inner loop terminates when the value of c exceeds
2, and the outer loop terminates when the value of r exceeds 3.
As you can see, the body of the outer for loop is indented, and the body of the
inner for loop is further indented. These multiple indentations make the program
easier to understand.
The way for loops have been nested here, similarly, two while loops can also be
nested. Not only this, a for loop can occur within a while loop, or a while within a
for.
Unconditional jump Statements (break, continue, exit())
break Statement
void main()
{
int i,j;
clrscr();
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
www.gkmvkalyan.blogspot.in
BCA105T- Programming Concepts using C Page 2 of 29
*
* *
* * *
* * * *
* * * * *
void main()
{
int i,j;
for(i=1; i<=5; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
getch();
}
www.gkmvkalyan.blogspot.in
BCA105T- Programming Concepts using C Page 3 of 29
1
23
456
7 8 9 10
void main()
{
int rows,i,j,k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; ++j)
printf("%d ",k+j);
++k;
printf("\n");
}
getch();
}
www.gkmvkalyan.blogspot.in
BCA105T- Programming Concepts using C Page 4 of 29
goto Keyword
Avoid goto keyword! It shows you are not a good programmer. Because of
the reasons that programs become unreliable, unreadable, and hard to
debug.
The big problem with goto is that when we use them we can never be sure how
we got to a certain point in our code. They obscure the flow of control. So as far
as possible skip them.
main( )
{
int s ;
printf ( "Enter the number” ) ;
scanf ( "%d", &goals ) ;
if ( s < 5 )
goto sos ;
else
{
printf ( "you have entered the value more than 5" ) ;
exit( ) ;
}
sos :
printf ( "you have entered the values less than 5!" ) ;
}
www.gkmvkalyan.blogspot.in
BCA105T- Programming Concepts using C Page 5 of 29
#include<stdio.h>
#include<conio.h>
void main()
{
int i=3,f1=0,f2=1,f3,n;
clrscr();
printf("\nHow many terms:");
scanf("%d",&n);
printf("%d \t %d",f1,f2);
for(i=3;i<=n;i++)
{
f3=f1+f2;
printf("\t %d",f3);
f1=f2;
f2=f3;
}
getch();
}
Output
How many terms:8
0 1 1 2 3 5 8 13
www.gkmvkalyan.blogspot.in
BCA105T- Programming Concepts using C Page 6 of 29
Write a C Program to find the GCD and LCM of two integer numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,temp,lcm,gcd;
clrscr();
printf("Enter the two numbers\n");
scanf("%d%d",&m,&n);
temp=m*n;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
gcd=n;
lcm=temp/gcd;
printf("LCM =%d\t GCD =%d",lcm,gcd);
getch();
}
Output
www.gkmvkalyan.blogspot.in