C Program Manual

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

LAB PROGRAM#1

Objective(s):
 To be familiar with syntax and structure of C- programming.
 To learn problem solving techniques using C.
Program:
Write a Program to calculate and display the volume of a CUBE having its height
(h=10cm), width (w=12cm) and depth (8cm).
Algorithm:
1. Start
2. Define variables: h(int), w(int), d(int), vol(int)
3. Assign value to variables: h = 10, w=12, d=8
4. Calculate the volume as: vol = h*w*d
5. Display the volume (vol)
6. Stop

Flowchart:

1|Page
Code: (Use comments wherever applicable)
//Following code is written and compiled in CodeBlocks
#include<stdio.h>
void main()
{
//start the program
int h,w,d,vol; //variables declaration
h=10; w=12; d=8; //assign value to variables
vol=h*w*d; //calculation using mathematical formula
printf("The Volume of the cube is: %d",vol); //display the volume
getch();
//end the main program
}
Output:
The Volume of the cube is: 960

SAMPLE PROGRAMS
(Students are to code the following programs in the lab and show the output to course
teacher)

Instructions
 Write comment to make your programs readable.
 Use descriptive variables in your programs (Name of the variables should show their
purposes)
Programs List
1. Write a C program to display “This is my first C Program”.
2. Write a C program to add two numbers (2 and 6) and display its sum.
3. Write a C program to multiply two numbers (4 and 5) and display its product.
4. Write a C program to calculate area and circumference of a circle.
5. Write a C program to perform addition, subtraction, division and multiplication of
two numbers.

2|Page
LAB PROGRAM#3
Objective(s):
 To be familiar with Operators and Expressions in C.
Program:
Write a program to take input of name, roll no and marks obtained by a student in 4
subjects of 100 marks each and display the name, roll no with percentage score
secured.

Algorithm:
1. Start
2. Define variables: name, rollno, sub1, sub2, sub3, sub4, sum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 4 subjects and also calculate the percentage score as:
sum = sub1 + sub2 + sub3 + sub4;
score = (sum/400) * 100
5. Display the name, roll number and percentage score.
6. Stop
Flowchart:

3|Page
Code: (Use comments wherever applicable)
//Following code is written and compiled in CodeBlocks
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int rollno;
float sub1, sub2, sub3, sub4, , sum, score;
printf("Enter name of student: ");
scanf(“%s”,&name[ ]);
printf ("\n Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/500)*100;
printf("\n Name of student: %s", name[]);
printf("\n Roll Number: %d", rollno);
printf ("\nPercentage score secured: %2.2f%c", score,'%');
getch();
}
Output:
Enter name of student: Ajit Singh
Roll Number: 25
Enter Marks in 4 Subjects:
50
75
85
62
Name of student: Ajit Singh
Roll Number: 25
Percentage score secured: 68.00%

4|Page
SAMPLE PROGRAMS
(Students are to code the following programs in the lab and show the output to course
teacher)

Instructions
 Write comment to make your programs readable.
 Use descriptive variables in your programs (Name of the variables should show their
purposes)
Programs List
1. Write a C program to display “This is my first C Program”.
2. Write a C program to add two numbers (2 and 6) and display its sum.
3. Write a C program to multiply two numbers (4 and 5) and display its product.
4. Write a C program to calculate area and circumference of a circle.
5. Write a C program to perform addition, subtraction, division and multiplication of
two numbers.

5|Page
6|Page
LAB PROGRAM#7
Objective(s):
 To understand the programming knowledge using Decision Statements (if, if-else,
nested if-else, else if ladder, switch and GOTO)
Program:
Write a program to take input of name, roll no and marks obtained by a student in 4
subjects of 100 marks each and display the name, roll no with percentage score
secured.

Algorithm:
1. Start
2. Define variables: name, rollno, sub1, sub2, sub3, sub4, sum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 4 subjects and also calculate the percentage score as:
sum = sub1 + sub2 + sub3 + sub4;
score = (sum/400) * 100
5. Display the name, roll number and percentage score.
6. Stop
Flowchart:

7|Page
Code: (Use comments wherever applicable)
//Following code is written and compiled in CodeBlocks
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int rollno;
float sub1, sub2, sub3, sub4, , sum, score;
printf("Enter name of student: ");
scanf(“%s”,&name[ ]);
printf ("\n Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/500)*100;
printf("\n Name of student: %s", name[]);
printf("\n Roll Number: %d", rollno);
printf ("\nPercentage score secured: %2.2f%c", score,'%');
getch();
}
Output:
Enter name of student: Ajit Singh
Roll Number: 25
Enter Marks in 4 Subjects:
50
75
85
62
Name of student: Ajit Singh
Roll Number: 25
Percentage score secured: 68.00%

8|Page
SAMPLE PROGRAMS
(Students are to code the following programs in the lab and show the output to course
teacher)

Instructions
 Write comment to make your programs readable.
 Use descriptive variables in your programs (Name of the variables should show their
purposes)
Programs List
1. Write a C program to display “This is my first C Program”.
2. Write a C program to add two numbers (2 and 6) and display its sum.
3. Write a C program to multiply two numbers (4 and 5) and display its product.
4. Write a C program to calculate area and circumference of a circle.
5. Write a C program to perform addition, subtraction, division and multiplication of
two numbers.

Control Structure at C Program


Control statements control the flow of execution of the statements of a program. The
various types of control statements in C language are as under:-
I. Sequential
II. Conditional
III. Iteration
Sequential control:
In sequential control, the C program statements are executed sequentially i.e., one
after another from beginning to end.
Example:
#include<stdio.h>
void main()
{
int x , y, sum;
printf(“enter the two numbers”);
scanf(“%d%d”,&x,&y);
sum=x+y;
printf(“the sum of the two numbers is =%d”,sum);

9|Page
}

Conditional Control (Selection Control or Decision Control):


In conditional control, the execution of statements depends upon the condition-test.
If the condition evaluates to true, then a set of statements is executed otherwise
another set of statements is followed.
This control is also called Decision Control because it helps in making decision about
which set of statements is to be executed.
Decision control structure in C can be implemented by using:-
1. If statement
2. If-else statement
3. Nested if else statement
4. else-if ladder
Iteration Control (Loops)
Iterations or loops are used when we want to execute a statement or block of
statements several times. The repetition of loops is controlled with the help of a test
condition. The statements in the loop keep on executing repetitively until the test
condition becomes false.
There are the following three types of loops:-
1. While loop
2. Do-while loop
3. For loop
Decision control/ Conditional Control /Selection Control
Decision control structure in C can be implemented by using:-
1. If statement
2. If-else statement
3. Nested if else statement
4. else-if ladder
if statement
if statements are executed only if the condition given with if evaluates to true. Its
general syntax and flow chart is as under:-

10 | P a g e
if (condition)
{
Statements;
}
Example: A program to understand if
statement.
/* program to check whether the given number
is negative*/

#include<stdio.h>
main()
{
int num;
printf(“enter the number”);
scanf(“%d”,&num);
if(num<0)
{
printf(“the entered number is negative”);
}
}

if…..else ... Statement


It is bidirectional conditional control statement that contains one condition &
two possible action. and take one of the two possible actions.
If the condition evaluates to true then one statement (or block of statements) is
executed otherwise other statement (or block of statements) is executed. The general
form and flow chart of if-else statement is as under:
if(condition)
{
block of statements;
}
else
{
block of statements;
}
Example:
/*Program to find the biggest of two
numbers*/
#include<stdio.h>
main()
{
int num1,num2 ;
printf (“enter the two numbers”);
scanf (“%d %d”,&num1,&num2);
if (num1>num2)
{

11 | P a g e
Printf (“the number %d is big”,num1);
}
else
{
printf(“the number %d is big”,num2);
}
}
Task:
1. To check a student is fail or pass.
2. To check a number is even or odd.
Nested if-else statement
If we have if-else statement within either the body of an if statement or the body of else
statement or in the body of both if and else, then this is known as nesting of if else
statement.
The general form of nested if-else statement is as follows:-
if(condition1)
{
if(condition2)
statements;
else
statements;
}
else
{
if(condition3)
Statements;
else
Statements;
}
Example:
/*program to find the largest of three numbers*/
#include<stdio.h>
main()
{
int a, b,c,large;
printf(“enter the three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
large=a;
else
large=c;
12 | P a g e
}
else
{
if(b>c)
large=b;
else
large=c;
}
printf(“largest number is %d”,large);
}
If….else LADDER
In this type of nesting there is an if else statement in every else part except the last
part. If condition is false control pass to block where condition is again checked with
its if statement.
The general syntax and flow chart of else if ladder is as follows:
If(condition1)
statementA;
else if(condition2)
statementB;
else if(condition3)
statementC;
else
statementD;
Example:
/*program to find the grade of the student*/
#include<stdio.h>
main()
{
int marks;
printf(“enter the percentage of the student”);
scanf(“%d”,&marks);
if(marks>=80)
intf(“your grade is a”);
else if(marks>=70)
printf(“your grade is b”);
else if(marks>=60)
printf(“your grade is c”);
else if(marks>=50)
printf(“your grade is d”);
else
printf(“you are fail”);
}

13 | P a g e
Switch statement
When there are several options and we have to choose only one option from the
available ones, we can use switch statement. Depending on the selected option, a
particular task can be performed. A task represents one or more statements.
Syntax:
switch(expression)
{
case value-1:
statement/block-1;
break;
case value-2:
statement/block t-2;
break;
case value-3:
statement/block -3;
break;
default:
default- statement/block t;
break;
Example:
#include<stdio.h>
main()
{
int a;
printf("Please enter a no between 1 and 5: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("You chose One");
break;
case 2:
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
printf("You chose Four");
break;
case 5: printf("You chose Five.");
break;
default :
printf("Invalid Choice. Enter a no between 1 and 5"); break;
}
}

14 | P a g e
LAB PROGRAM#7
Objective(s):
 To understand the programming using Loop & Nested Loop Statements (for, while,
do-while)
Theory:
C language provides three iterative/repetitive loops.
1 : while loop
2 : do-while loop
3 : for loop
While Loop: Syntax:
variable initialization ;
while (condition)
{
statements;
variable increment or decrement ;
}
The while loop is an entry controlled loop statement, i.e means the condition is evaluated
first and it is true, then the body of the loop is executed. After executing the body of the loop,
the condition is once again evaluated and if it is true, the body is executed once again, the
process of repeated execution of the loop continues until the condition finally becomes false
and the control is transferred out of the loop.

Do-While Loop: Syntax:


variable initialization ;
do
{
statements ;
variable increment or decrement ;
}
while (condition);
The do-while loop is an exit controlled loop statement the body of the loop are executed
first and then the condition is evaluated. If it is true, then the body of the loop is executed once
again. The process of execution of body of the loop is continued until the condition finally
becomes false and the control is transferred to the statement immediately after the loop. The
statements are always executed at least once.

for Loop: Syntax:


for(initialization; condition; increment/decrement)
{
Statements;
}
This is an entry controlled looping statement. In this loop structure, more than one
variable can be initialized.

15 | P a g e
Program:
Write a program to print positive integers from 1 to 10.
Code: (Use comments wherever applicable)
//Following code is written and compiled in CodeBlocks
//Using FOR LOOP
#include<stdio.h>
void main()
{
int i;
for(i=1; i<=10;i++)
printf(“%d \n”, i);
}
//Using WHILE LOOP
#include<stdio.h>
void main()
{
int i=1;
while(i<=10)
{
printf(“%d \n”, i);
}
i++;
}
//Using DO-WHILE LOOP
#include<stdio.h>
void main()
{
int i=1;
do
{
printf(“%d \n”, i);
i++;
}
while(i<=10);
}
Output:
1
2
3
4
5
6
7
8
9
10
16 | P a g e
Infinitive for loop in C
If you don't initialize any variable, check condition and increment or decrement
variable in for loop, it is known as infinitive for loop. In other words, if you place 2
semicolons in for loop, it is known as infinitive for loop.
for(; ;)
{
printf("infinitive for loop example by javatpoint");
}
Where to Use for Loop, while Loop and do while Loop:
The for loop is appropriate when we know in advance how many times the loop will be
executed.
The other two loops i.e. while and do while loops are more suitable in the situations
where it is not known before hand when the loop will terminate.
Nested for loop:
Syntax:
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement;
}
}
Example:
Program to print half Pyramid of numbers
#include<stdio.h>
void main( )
{
int i,j;
for(i=1;i<5;i++)
{
printf("\n");
for(j=i;j>0;j--)
{
printf("%d",j);
}
}
}
Output
1
21
321
4321
54321

17 | P a g e
SAMPLE PROGRAMS
(Students are to code the following programs in the lab and show the output to course
teacher)

Instructions
 Write comment to make your programs readable.
 Use descriptive variables in your programs (Name of the variables should show their
purposes)
Programs List
1. Write a C program to display “Factorial Program using loop”.
2. Write a C Program to calculate the sum of first n natural numbers.
3. Write a C Program to print first ten multiple of 5.
4. Write a program to display the following pattern.

5. Write a program to check whether a number is Palindrome or not.

18 | P a g e

You might also like