Paper DSC 203:PROGRAMMING WITH C & C++
UNIT-I: INTRODUCTION TO C LANGUAGE, VARIABLES, DATA TYPES AND OPERATORS INTRODUCTION: TYPES OF LANGUAGES- HISTORY OF C LANGUAGE – BASIC TRUCTURE –PROGRAMMING RULES – FLOW CHARTS-ALGORITHMS–COMMONLY USED LIBRARY FUNCTIONS - EXECUTING THE C PROGRAM - PRE-PROCESSORS IN “C”- KEYWORDS & IDENTIFIERS – CONSTANTS – VARIABLES: RULES FOR DEFINING VARIABLES - SCOPE AND LIFE OF A VARIABLE–- DATA TYPES - TYPE CONVERSION - FORMATTED INPUT AND OUTPUT OPERATIONS. OPERATORS: INTRODUCTION – ARITHMETIC – RELATIONAL – LOGICAL – ASSIGNMENT - CONDITIONAL - SPECIAL - BITWISE - INCREMENT / DECREMENT OPERATOR.
UNIT-II: WORKING WITH CONTROL STATEMENTS, LOOPS CONDITIONAL STATEMENTS: INTRODUCTION - IF STATEMENTS - IF-ELSE STATEMENTS – NESTED IF-ELSE – BREAKSTATEMENT-CONTINUE STATEMENT-GO TO STATEMENT-SWITCH STATEMENTS. LOOPING STATEMENTS: INTRODUCTION-WHILE STATEMENTS – DO-WHILE STATEMENTS - FOR STATEMENTS-NESTED LOOP STATEMENTS.
UNIT-IV: POINTERS, STRUCTURES AND UNIONS POINTERS: FEATURES OF POINTERS- DECLARATION OF POINTERS-ARITHMETIC OPERATIONS WITH POINTERS STRUCTURES: FEATURES OF STRUCTURES - DECLARING AND INITIALIZATION OF STRUCTURES –STRUCTURE WITHIN STRUCTURE- ARRAY OF STRUCTURES- ENUMERATED DATA TYPE-UNIONS-DEFINITION AND ADVANTAGES OF UNIONS COMPARISON BETWEEN STRUCTURE & UNIONS.
UNIT-V: OBJECT ORIENTED CONCEPTS USING C++ OBJECT ORIENTED PROGRAMMING: INTRODUCTION TO OBJECT ORIENTED PROGRAMMING - STRUCTURE OF C++ – SIMPLE PROGRAMOF C++– STORAGE CLASSES-SIMILARITIES AND DIFFERENCES BETWEEN C & C++ - DATA MEMBERS-MEMBER FUNCTIONS - OBJECT ORIENTED CONCEPTS-CLASS-OBJECT-INHERITANCE- OLYMORPHISM- ENCAPSULATION-ABSTRACTION.
UNIT-II
WORKING WITH CONTROL STATEMENTS,
LOOPS
Introduction
Depending on the requirements of a
problem, it might be required to alter the normal sequence of execution in a
program. The order in which statements are executed is called the flow of
control. Controlling the flow of a program is a very important aspect of
programming.
·
Selection
or Branching Statements
·
Iteration
or Looping Statements
· Jumping Statements
Selection Statements
Selection statements are of three
types:
-One-way
Selection Statements
These statements can do a particular
thing or they do not do anything. In C a simple if statement can do
this.
-Two-way
Selection Statements These statements can do one thing or another
thing. In C an if..else statement can do this.
-Multi-way Selection Statements These statements can do one of many different things. In C an else..if ladder/nested if statements can do this.
Conditional
statements(Control Statements)
1.Explain Conditional statements in
c language?
Ans: if statement
One-way decisions can be handled
with a simple if statement. It has the following format:
{
Statement-T;
}
Statement-X;
TestExpr is the test expression. StmtT can be a single statement or a block of statements enclosed by curly braces { }.
The simple if statement works as
follows:
First, it verifies the ‘test
expression’. If the test expression is TRUE (1), then it executes statement-T.
if the test expression is FALSE (0) then it skips the statement-T and directly goes to statement-X.
Write a program
to find the largest among three numbers.
#include <stdio.h>
main()
{
int a, b, c, max;
printf(“\nEnter 3 numbers”);
scanf(“%d %d %d”, &a, &b, &c);
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf(“Largest No is %d”, max);
getche();
return 0;
}
if-else statement
Two-way decisions can be handled
with if-else statement. It has the following format:
{
statement-T;
}
else
{
statement-F;
}
Statement-X;
The if...else statement works as
follows:
First, it verifies the ‘test
expression’.
If the test expression is TRUE (1),
statement-T will be executed.
If the test expression is FALSE (0) then statement-F will be executed.
Write a C
program to check whether a number given by the user is odd or even.
#include <stdio.h>
int main()
{
int n,r;
printf(“\nEnter the number”);
scanf(“%d”, &n);
r=n%2;
if(r==0)
printf(“EVEN”);
else
printf(“ODD”);
return 0;
}
if..else..if statement (else..if
ladder)
Multi-way decisions can be handled
with if-else-if statement. It has the following format:
statement-T1;
else
if(TestExpr2)
statement-T2;
else
if(TestExpr3)
statement-T3;
.
else
if(TestExprN)
statement-TN;
else
statement-F;
Write a C
program to check whether the given number is zero, positive, or negative.
#include <stdio.h>
main()
{
int n;
printf(“\n ENTER A NUMBER:”);
scanf(“%d”, &n);
if(n > 0)
printf(“%d is positive \n”,n);
else if(n == 0)
printf(“%d is zero \n”,n);
else
printf(“%d is negative \n”,n);
return 0;
}
Nested if else Placing a if
statement within another if statement is called a nested if. It has the
following format:
{
if(TestExpr2)
{
statement-2T;
}
else
{
statement-2F;
}
}
else
{
if(TestExpr3)
{
statement-3T;
}
else
{
statement-3F;
}
}
Write a C program
to find the largest of three numbers.
#include <stdio.h>
main()
{
int a, b, c;
printf(“\nEnter three numbers”);
scanf(“%d %d %d”, &a, &b, &c);
if(a > b)
if(a > c)
printf(“%d”, a);
else
printf(“%d”, c);
else
if(b > c)
printf(“%d”, b);
else
printf(“%d”, c);
return 0;
}
2.Explain
Switch case statement in C language with an example?
Switch
Statement
Switch
statement is a multi-way selection statement. It has the following format:
switch(expr)
{
case constant1: stmtList-1;
break;
case constant2: stmtList-2;
break;
case constant3: stmtList-3;
break;
………………………….
default: stmtList-n;
}
Here, the case
constants must be integer or character constants.
The switch
statement works as follows:
The switch
statement verifies the expression against a list of case values. When a match
is found then the statements in the corresponding statement list are executed.
Whenever there is no match then the default statements are executed.
Example: Write
a program to perform arithmetic operations addition, subtraction,
multiplication, and division between two variables. Use the switch construct to
choose the operations.
Solution:
#include<stdio.h>
#include<conio.h>
main()
{
int a, b;
char operator;
clrscr();
printf(“Enter your expression: \n”);
scanf(“%d %c %d ”,&a, &operator, &b);
switch(operator)
{
case ‘+’:
printf(“%d \n”, a + b);
break;
case ‘-’:
printf(“%d \n”, a - b);
break;
case ‘*’:
printf(“%d \n”, a * b);
break;
case ‘/’:
if(b == 0)
printf(“division by zero. \n”);
else
printf(“%d \n”, a / b);
break;
default:
printf(“Unknown Operator \n”);
}
getche();
return 0;
}
Looping Statements or
(Iterative Statements)
A loop allows to execute the statements repeatedly.
A loop can be either a
- pre-test loop
- post-test loop.
In
a pre-test loop, first, the condition is
checked. If the test condition is true, then only it executes the statements of
that loop.
In
the post-test loop, first the code is always executed
once. The test condition is tested at the end of the loop. If the test
condition is true, the loop repeats. if it is false the loop terminates.
while and for are pre-test loops and do-while is a post-test loop.
3.Explain Various loops available in
C language with example?
Ans: C has three loop constructs: while,for,
and do-while.
While loop
Initialization;
while
(TestExpr)
{
stmT
...
...
Updating
}
stmT will be executed repeatedly till the
value of TestExpr becomes 0 (FALSE).
Example: Write a C Program to illustrate ‘while’ loop.
#include <stdio.h>
main()
{
int c;
c=5; // Initialization
while(c>0) // Test Expression
{
printf(“ \n %d”,c); //statement-T
c=c-1; // Updating
}
return 0;
}
I/O:
5
4
3
2
1
do-while loop
do
{
stmT; /*
statements block*/
}while(TestExpr);
First, it
executes the statements block once. Then it verifies the test expression. If it
is TRUE (1) then it again executes the statements block. Thus the statements
block will be executed repeatedly till the value of Test expression becomes 0
(FALSE).
For a do
while loop, it is guaranteed that the body of the loop will execute at least
once.
Example:
Write a C
Program to generate the multiplication table of the given number.
#include <stdio.h>
#include<conio.h>
main()
{
int n, i = 1;
printf("Enter a Number:");
scanf(“%d”, &n);
do
{
printf(“\n %d x %d = %d”, n, i, n*i);
i++;
} while(i<=10);
getche();
return 0;
}
I/O
Enter a Number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 =20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 =
50
For loop
for loop has the following format:
for(initialization;
TestExpr; updating)
{
stmsT; //
Body of the loop
}
2.
TestExpr: It represents a test expression. If it is TRUE (1)
then only it executes the body of the loop.
3. Updating: It
updates the looping control variable.
The for loop follows the following sequence:
1. Do the
initialization.
2. if the
test expression is TRUE(1), then go to step 3. Otherwise If the value of the
test expression is FALSE(0), then terminate the loop and go to step 5;
3. Execute
the body of the loop.
4. Update
the expression and Go to step 2.
5. Execute the next statement.
Example: Write
a C program to print the numbers from 1 to 10.
#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i = 1; i <= 10; i++)
{
printf(“%d”,i);
}
return 0’
}
I/O
1
2
3
4
5
6
7
8
9
10
---O--
While Loop
Examples
1. Write a C
program that accepts input from the keyboard and counts the positive integers
until a negative number is entered.
#include <stdio.h>
main( )
{
int x = 1;
int count = 0;
printf(“\n Enter the Number:”);
scanf(“%d”,&x);
while(x >= 0)
{
count += 1;
scanf(“%d”,&x);
}
printf(“ \n\nThe number of positive integers are:
%d”,count);
return 0;
}
I/O:
Enter the Number: 10
20
30
40
50
-60
The number of positive integers are:5
2. Write a C
program that asks the user to enter some numbers and then find their average
#include <stdio.h>
int main()
{
int n, a, c=1,s=0;
float avg;
printf(“\n HOW MANY NUMBERS?”);
scanf(“%d”, &n);
while(c<=n)
{
printf(“\n Enter the number: ”);
scanf(“%d”, &a);
s+=a;
c++;
}
avg=(float)s/n;
printf(“ \n AVERAGE IS %f ”, avg);
return 0;
}
I/O:
HOW MANY NUMBERS? 5
Enter the number: 10
Enter the number: 20
Enter the number: 30
Enter the number: 40
Enter the number: 50
AVERAGE IS =30.0
3. Write a C
Program to find the sum of the digits of the number
#include <stdio.h>
int main()
{
int n, s=0, r;
printf(“Enter the Number”);
scanf(“%d”, &n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf(“\nSum of digits %d”, s);
return 0;
}
I/O:
Enter the Number: 1234
Sum of digits 10
4. Write a C
program for counting the number of words, lines, special characters in a given
text.
#include<stdio.h>
#include<conio.h>
main( )
{
char ch;
int lines=0, words=0, chars=0;
clrscr( );
printf("Enter the required no. of
lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10)
lines++;
if(isspace(ch))
words++;
chars++;
}
printf("\nNumber of Lines : %d", lines+1);
printf("\nNumber of Words: %d", words+1);
printf("\nTotal Number of Characters: %d",
chars);
getch( );
return 0;
}
I/O
Enter the required no. of lines:
This is a Test
This is a Test
This is a Test ^Z
Number of Lines : 3
Number of Words : 12
Number of Characters: 44
Do-While loop
examples:
1. Write a c
program to find the sum of the positive numbers entered by the user. Terminate
the loop if the user enters a negative number.
#include <stdio.h>
#include<conio.h>
main()
{
int x = 1;
int sum = 0;
do {
printf("Enter a Number:");
scanf(“%d”, &x);
if(x >= 0)
{
sum +=x;
}
}while(x >= 0);
printf("\n Sum = %d", sum);
getche();
return 0;
}
I/O
Enter a Number: 10
Enter a Number: 20
Enter a Number: 30
Enter a Number: 40
Enter a Number:50
Enter a Number: -60
Sum = 150
For Loop
Examples:
1.Write a C
program to print the sum of the series 1+2+3+4+... up to n terms.
#include <stdio.h>
#include<conio.h>
main()
{
int i, n, sum=0;
clrscr();
printf(“\n Enter the No. of terms”);
scanf(“%d”, &n);
for(i=1; i<=n; i++)
{
sum+=i;
}
printf(“\n Sum is %d”, sum);
getche();
return 0;
}
I/O:
Enter the number of terms: 5
Sum is 15
2.Write a C
program to calculate the factorial of the given number.
#include<stdio.h>
#include<conio.h>
main()
{
int n, i, f=1;
printf(“\n Enter the number: ”);
scanf(“%d”,&n);
for(i=1;i<=n;++i)
{
f*=i;
}
printf(“\n Factorial is %ld”,f);
getche();
return 0;
}
I/O:
Enter the number: 5
Factorial is 120
3.Write a C
program to find whether the given number is Prime Number or not?
#include <stdio.h>
#include<conio.h>
main()
{
int n, i, flag = 0;
printf("Enter a Number: ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
{
printf("%d is a prime number.",n);
}
else
{
printf("%d is not a prime number.",n);
}
getche();
return 0;
}
I/O:
(1.) Enter a Number: 4
4 is not a prime number
(2.)Enter a Number: 7
7 is a Prime Number
4.Write a C
program to calculate the Fibonacci series of a given number.
#include<stdio.h>
#include<conio.h>
main()
{
int i,n, fib, n1=1,n2=1;
clrscr();
printf("Enter the range for Fibonacci
Series:");
scanf("%d",&n);
printf("\n The Fibonacci Series is: \n");
printf("%d %d ",n1,n2);
for(i=2;i<n;i++)
{
fib=n1+n2;
printf(" %d ",fib);
n1=n2;
n2=fib;
}
getche();
return 0;
}
I/O:
Enter the range for Fibonacci Series: 10
The Fibonacci Series is:
1 1 2 3 5 8 13 21 34 55
5.Write a C
program to find whether the given number is Strong Number?
#include<stdio.h>
#include<conio.h>
main()
{
int n,x,r,i,f,sum=0;
clrscr();
printf("Enter a Number:");
scanf("%d",&n);
for(x=n; n ;n=n/10)
{
i=f=1;
r=n%10;
while(i<=r)
{
f=f*i;
i++;
}
sum=sum+f;
}
if(sum==x)
{
printf("\n%d is a Strong Number",x);
}
else
{
printf("\n %d is not a Strong Number",x);
}
getche();
return 0;
}
I/O: (1)Enter a Number: 128
128 is not a Strong
Number
(2)Enter a Number:
145
145 is a Strong
Number
6. Program to
find whether the given number is a Palindrome?
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,rev=0,x;
clrscr();
printf("Enter any number:");
scanf("%d",&n);
x=n;
while(n!=0)
{
r=n%10;
rev=r+(rev*10);
n=n/10;
}
if( rev == x)
{
printf("%d is a palindrom number ",x);
}
else
{
printf("%d is Not a palindrom number ",x);
}
getche();
return 0;
}
I/O: (1)
Enter a Number: 12
12 is not a
Palindrome number
(2)Enter a Number: 121
121 is a Palindrome number
7.Write a C
program whether the given number is Armstrong number or not?
#include<stdio.h>
#include<conio.h>
main()
{
int n,x,r,sum=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(x=n; n!=0;n=n/10)
{
r=n%10;
sum=sum+r*r*r;
}
if(x=sum)
{
printf("\%d is a Armstrong number",n);
}
else
{
printf("\%d is not a Armstrong number",n);
}
getche();
return 0;
}
I/O: (1)Enter a Number: 12
12 is not a Armstrong number
(2)Enter a Number:
153
153 is a Armstrong
number
Nested loops
A nested
loop refers to a loop that is contained within another loop. If the program has
to repeat a loop more than once, then we use nested loop. In nested loops, the inside loop
(or loops) executes completely before the outside loop’s next iteration.
The way if
statements can be nested, similarly whiles and fors can also be nested. To
understand how nested loops work, look at the program given below:
Nested For loop example Program to
print stars Sequence
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}
Output:
*
**
***
****
*****
Nested While loop Example
#include<stdio.h>
#include<conio.h>
void
main()
{
int i,j;
i=1;
while(i<=5)
{
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("\n");
i++;
}
}
Output:
*
**
***
****
*****
Special Control Statements
3.Explain Special control statements
in c?
Ans:
C has some control statements to
terminate a loop or a function. Those control statements are: return, break,
and continue.
1. goto Statement
The goto statement is an
unconditional control statement. It transfers the control the label specified
in the goto statement.
It has the following format:
goto label_name;
A statement label must be followed
by a colon (:)
Example:
#include <stdio.h>
main()
{
int n, i,f=1;
printf("\n Enter the number:");
scanf("%d",&n);
if(n<0)
{
goto end;
}
i=1;
loop:
f=f*i;
i++;
if(i<=n)
{
goto loop;
}
printf("\n FACTORIAL IS %d", f);
end:
return 0;
}
2. return statement:
The return type is used in a function
definition to set its return value.
The return statement has two forms.
(1) return;
This format can be used for the
functions which return a blank control without value.
(2) return expression;
This format can be used for the
functions which return a particular value.
3. break
statement:
The break
statement is used in loops and switch statement to terminate execution of the
loop or switch statement. It has the
following form:
break;
The break
statement causes the control to go out of the loop/block.
The following C
program illustrate break/return statements:
#include <stdio.h>
main( )
{
int c;
for(;;)
{
if(c==5)
{
break;
}
printf(" \t %d", c);
c++;
}
getche();
return 0;
}
0 1 2 3 4
4. continue statement:
The continue statement does not terminate
the loop. But it can continue with the next iteration.
It takes the following form:
Example:
#include <stdio.h>
main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
{
continue;
}
printf("\t %d", i);
}
getche();
return 0;
}
1 2 4 5
No comments:
Post a Comment