Вы находитесь на странице: 1из 37

A

Project Based Report


On
Building Various patterns Using Numbers and Special Characters Using C-Language

A project work on Coding Skilling submitted to


KL deemed to be University under the partial fulfilment of
III /IV B.Tech

Siva Surya Kumar (170040811)


Guntupalli
T. Deep (170040841)
Telluri Vani Sai (170040860)
Bhavani
Tiragati Vishwanath (170040875)

Under the guidance of

Dr. V. Saravanan
(Associate Professor)

K.L. deemed to be UNIVERSITY


Green fields, Vaddeswaram-522502,
Guntur Dist.
2019-2020

1
CERTIFICATE

This is to certify that the project entitled “ Building Various patterns Using Numbers and
Special Characters Using C-Language” is the bonafide work carried out by
Siva Surya Kumar Guntupalli (170040811) , T.Deep (170040841) , Telluri Vani Sai
Bhavani (170040860) , Tiragati Vishwanath (170040875) students of IⅠⅠ year
B.Tech, E.C.E dept, College of Engineering, K.L deemed to be University, in the Coding
Skilling for the academic year 2019-2020.

Signature of the Project guide Signature of Course Coordinator

Dr. V. Saravanan B. JOHN PHILIP

ELITE Head HOD ECE

Mr. Ramesh Siddavatam Dr. M SUMAN

2
ACKNOWLEDGMENT

Our sincere thanks to Dr. V. Saravanan in the Lab for his outstanding support throughout
the project for the successful completion of the work.

Our sincere thanks to B. JOHN PHILIP, Course coordinator of Technical Proficiency &
Training-1 for supporting in successful completion of this project.

We express our gratitude to Mr. Ramesh Siddavatam, ELITE Head and Dr. SUMAN
MALOJI, Head of the Department for Electronics &Communication Engineering for
providing us with adequate facilities, ways and means by which we are able to complete this
term paper work.

We would like to place on record the deep sense of gratitude to the honourable Vice
Chancellor, K L deemed to be University for providing the necessary facilities to carry the
concluded term paper work.

Last but not the least, we thank all Teaching and Non-Teaching Staff of our department and
especially my classmates and my friends for their support in the completion of our project
work.

Place: KL University S.No Name of the Student


Date:
1 Siva Surya Kumar Guntupalli (170040811)

2 T. Deep (170040841)

3 Telluri Vani Sai Bhavani (170040860)

4 Tiragati Vishwanath (170040875)

3
CONTENTS

Content Page No

Abstract 5

Chapter 1: Introduction 6

Chapter 2: C programming Basics 7

Chapter 3: Explanation of Basics 8

Chapter 4: Problem Identification 17

Chapter5: Solution to the problem 18

Chapter 6: Conclusions 37

References 37

4
ABSTRACT

In this project we have generated different patterns by using numbers. We


have used conditional statements like if , if-else statements, nested loops, for
loop, while loop etc..

Normally the statements inside the loop body executes sequentially. But by
using loop control statements we can change the flow of execution of statements
inside the loop body. If we are exiting the loop body then all automatic and
local variables/objects which got created in loop's scope will be destroyed. If
programmer wants to perform "specific operation" multiple times then he uses a
loop. The "specific operation" should be kept inside the loop and are called
"controlled statements" or "body of a loop".

5
INTRODUCTION

Patterns in C Programming, C is the procedural, general-purpose programming


language. It was first created between 1969 and 1973 by Dennis Ritchie. Low-level
access to memory, a simple set of keywords, and eas implementation are the main
features of the C language. There are many languages like PHP, Java, Javascript, etc.
follows features or syntax of C to some extent. A pattern is a regularity in the world,
in human-made design, or in abstract ideas. As such, the elements of a pattern repeat
in a predictable manner. A geometric pattern is a kind of pattern formed
of geometric shapes and typically repeated like a wallpaper design. Patterns provide a
sense of order in what might otherwise appear chaotic. Researchers have found that
understanding and being able to identify recurring patterns allow us to make educated
guesses, assumptions, and hypothesis; it helps us develop important skills of critical
thinking and logic. The knowledge and understanding of patterns can be transferred
into all curriculum areas and open many doors where this knowledge can be applied.
There are various patterns in C language like star pattern, number patterns, and
character patterns. In the following C programs, the user can provide the number of
rows to print the pattern . These programs print various patterns of numbers. These
codes illustrate how to create various patterns using C programming. The C programs
involve usage of nested for loops (a for loop inside a for loop). A pattern of numbers,
star or characters is a way of arranging these in some logical manner or they may form
a sequence. Some of these patterns are triangles which have special importance in
mathematics. Some patterns are symmetrical while others are not.

6
C PROGRAMMING BASICS

• Header Files
• Main Function
• Comments in C
• Data Types in C
• Variables
• Input and Output
• For Loop
• While Loop
• Do-While Loop
• Conditional Statements
• Operators
• String Functions
• Numeric Functions
• Functions and Parameters
• Actual and Formal Parameters
• Arrays
• Structures
• Type def
• Pointers
• Call by Value
• Call by Reference

7
Explanation of Basics

➔ Header Files:

• The files that are specified in the include section is called as header file

• These are precompiled files that has some functions defined in them

• We can call those functions in our program by supplying parameters

• Header file is given an extension .h

• C Source file is given an extension .c

➔ Main Program :

• This is the entry point of a program

• When a file is executed, the start point is the main function

• From main function the flow goes as per the programmers choice.

• There may or may not be other functions written by user in a program

• Main function is compulsory for any c program.

➔ Comments in C:

• Single line comment –// (double slash)

–Termination of comment is by pressing enter key

• Multi line comment /*… .......... */

This can span over to multiple lines

➔ Data Types in C

• Primitive data types –int, float, double, char

• Aggregate data types

8
–Arrays come under this category

–Arrays can contain collection of int or float or char or double data

• User defined data types

–Structures and enum fall under this category.

➔ Variables

• Variables are data that will keep on changing

• Declaration

<<Data type>> <<variable name>>;

int a;

• Definition

<<varname>>=<<value>>;

a=10;

• Usage

<<varname>>

a=a+1; //increments the value of a by 1

Rules for variables:

• Should not be a reserved word like int etc..

• Should start with a letter or an underscore(_)

• Can contain letters, numbers or underscore.

• No other special characters are allowed including space

• Variable names are case sensitive –A and a are different.

➔ Input and output

• Input

–scanf(“%d”,&a);

9
–Gets an integer value from the user and stores it under the name “a”

• Output

–printf(“%d”,a);

–Prints the value present in variable a on the screen

➔ For Loop

• The syntax of for loop is


for(initialisation;condition checking;increment)
{
set of statements
}
Eg: Program to print Hello 10 times
for(I=0;I<10;I++)
{
printf(“Hello”);
}

➔ While Loop

• The syntax for while loop


while(condn) {
statements;
} Eg:
i=10;
do{
printf(“ %d”,i);
i--;

10
}
While(i!=0)
Output:
10 9 8 7 6 5 4 3 2 1

➔ Conditional Statements

if (condition)
{
stmt 1; //Executes if Condition is true
}
else
{
stmt 2; //Executes if condition is false
}

➔ switch(var)
{
case 1: //if var=1 this case executes
stmt;
break;
case 2: //if var=2 this case executes
stmt;
break;
default: //if var is something else this will execute
stmt;
}

➔ Operators

• Arithmetic (+,-,*,/,%)
•Relational (<,>,<=,>=,==,!=)
• Logical (&&,||,!)

11
• Bitwise (&,|)
• Assignment (=)
• Compound assignment(+=,*=,-=,/=, %=,&=,|=)
• Shift (right shift >>, left shift <<)
➔ String Functions

• strlen(str) – To find length of string str


• strrev(str) – Reverses the string str as rts
• strcat(str1,str2) – Appends str2 to str1 and returns str1
• strcpy(st1,st2) – copies the content of st2 to st1
• strcmp(s1,s2) – Compares the two string s1 and s2
• strcmpi(s1,s2) – Case insensitive comparison of strings

➔ Numeric Functions

• pow(n,x) – evaluates n^x


• ceil(1.3) – Returns 2
• floor(1.3) – Returns 1
• abs(num) – Returns absolute value
• log(x) - Logarithmic value • sin(x) • cos(x) • tan(x)
* Procedure is a function whose return type is void.
*Functions will have return types int, char, double, float or even structs and arrays
*Return type is the data type of the value that is returned to the calling point after the
called function execution completes

➔ Functions and Parameters:

• Syntax of function
Declaration section <<Returntype>> funname(parameter list);
Definition section <<Returntype>> funname(parameter list) {
body of the function

12
}

➔ Function Call

Funname(parameter);
Example:
#include<stdio.h>
void fun(int a); //declaration
int main() {
fun(10); //Call
}
void fun(int x) //definition {
printf(“%d”,x);
}
• Actual parameters are those that are used during a function call
• Formal parameters are those that are used in function definition and
function declaration

➔ Arrays:

• Arrays fall under aggregate data type


• Aggregate – More than 1
• Arrays are collection of data that belong to same data type
• Arrays are collection of homogeneous data
• Array elements can be accessed by its position in the array called as index
• Array index starts with zero
• The last index in an array is num – 1 where num is the no of elements in a array
• int a[5] is an array that stores 5 integers
• a[0] is the first element where as a[4] is the fifth element
• We can also have arrays with more than one dimension

13
• float a[5][5] is a two dimensional array. It can store 5x5 = 25 floating point numbers
• The bounds are a[0][0] to a[4][4]

➔ Structures

• Structures are user defined data types


• It is a collection of heterogeneous data
• It can have integer, float, double or character data in it
• We can also have array of structures
struct <<structname>> {
members;
}element;
We can access element.members;
Example:
struct Person {
int id;
char name[5];
}P1;
P1.id = 1;
P1.name = “yash”;

➔ Type def

• The typedef operator is used for creating alias of a data type


• For example I have this statement
typedef int integer; Now I can use integer in place of int
i.e instead of declaring int a;, I can use integer a;
This is applied for structures too.

14
➔ Pointers

 Pointer is a special variable that stores address of another variable


• Addresses are integers. Hence pointer stores integer data
• Size of pointer = size of int
• Pointer that stores address of integer variable is called as integer pointer and is
declared as int *ip;
• Pointers that store address of a double, char and float are called as double pointer,
character pointer and float pointer respectively.
• char *cp • float *fp • double *dp;
• Assigning value to a pointer int *ip = &a; //a is an int already declared
int a;
a=10; //a stores 10
int *ip;
ip = &a; //ip stores address of a (say 1000)
ip : fetches 1000
*ip : fetches 10
* Is called as dereferencing operator
->Call By Value
• Calling a function with parameters passed as values
int a=10;
fun(a);
void fun(int a)
{
defn;
}
Here fun(a) is a call by value. Any modification done with in the function is local to it
and will not be effected outside the function

15
➔ Call by Reference

• Calling a function by passing pointers as parameters (address of variables is passed


instead of variables)
int a=1;
fun(&a);
void fun(int *x) {
defn;
}
Any modification done to variable a will effect outside the function also
• Call by value => copying value of variable in another variable. So any change made
in the copy will not affect the original location.
• Call by reference => Creating link for the parameter to the original location. Since
the address is same, changes to the parameter will refer to original location and the
value will be over written.

16
PROBLEM IDENTIFICATION

There are various patterns in C language like star pattern, number patterns, and
character patterns. In the following C programs, the user can provide the number of
rows to print the pattern . These programs print various patterns of numbers. These
codes illustrate how to create various patterns using C programming. The C programs
involve usage of nested for loops (a for loop inside a for loop). A pattern of numbers,
star or characters is a way of arranging these in some logical manner or they may form
a sequence. Time complexity of a function (or set of statements) is considered as O(1)
if it doesn’t contain loop, recursion and call to any other non-constant time function.
Time Complexity of a loop is considered as O(n) if the loop variables is incremented /
decremented by a constant amount. For example following functions have O(n) time
complexity. Time complexity of nested loops is equal to the number of times the
innermost statement is executed. For example the following sample loops have O(n 2)
time complexity. Time complexity is the main drawback in implementing patterns in
c. To some extinct this can be reduced by implementing patterns in matrices

17
SOLUTION TO THE PROBLEM

PATTERN 1

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("%d ",a);
a++;
}printf("\n");
}
}

PATTERN 2

1
21
321
4321
54321

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n,a=1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
18
printf("%d ",a-j);

}
a++;
printf("\n");
}
}

PATTERN 3
1
23
456
7 8 9 10

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n,a=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=n;j>0;j--)
{
if(j<=i)
{
printf("%d ",a);
a++;
}
else
printf(" ");
}printf("\n");
}
}

PATTERN 4

A
ba
CBA
dcba
EDCBA

19
PROGRAM

#include<stdio.h>
int main()
{
int i,j,n,a=64,b=96;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
a=a+i;
else
b=b+i;
for(j=1;j<=i;j++)
{
if(i%2!=0)
{
printf("%c ",a);
a--;
}
else
{
printf("%c ",b);
b--;
}
}printf("\n");
}
}

PATTERN 5
z
ab
wxy
cdef
rstuv
ghijkl
mnopq

PROGRAM

#include<stdio.h>
int main()
{
int i,j,n,a=122,b=97,c=0,p;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i%2==0&&i>0)
{
if(c==0)
{
21
a=a-i-2;
p=a;c=1;

}
else
{
a=p-i-1;
}
}
for(j=0;j<=i;j++)
{
if(i%2!=0)
{
printf("%c ",b);
b++;
}
else
{
printf("%c ",a);
a++;
}
}printf("\n");
}
}

PATTERN 6
*****
****
***
**
*

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n,a=0;
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=n;j++)
{
if(j>a)
printf("* ");
else
printf(" ");
}a++;
printf("\n");
}
}

22
PATTERN 7
1
1*2
1*2*3
1*2*3*4
1*2*3*4*5
1*2*3*4
1*2*3
1*2
1

PROGRAM

#include<stdio.h>
int main()
{
int i,j,n,a,c=1;
scanf("%d",&n);
int k=(n*2)-1;
for(i=0;i<k;i++)
{a=1;
if(i!=0)
{
if(i<n)
c=c+2;
else
c=c-2;
}
for(j=0;j<c;j++)
{
if(j%2==0)
{
printf("%d",a);
a++;
}
else
printf("*");
}printf("\n");
}
}

PATTERN 8
1
32
654
10 9 8 7
15 14 13 12 11

23
PROGRAM

#include<stdio.h>
int main(){
int i,j,n,a=0,k;
scanf("%d",&n);
for(i=1;i<=n;i++)
{ k=a+i;
for(j=1;j<=i;j++)
{
printf("%d ",k);
k--;
}printf("\n");
a=a+i;
}

PATTERN 9
11 12 13 14 15
7 8 9 10
456
23
1
PROGRAM

#include<stdio.h>
int main()
{
int i,j,n,a,p;
scanf("%d",&n);
int k=(n*(n+1))/2;
for(i=n;i>=1;i--)
{ if(i==n)
{
a=k-i+1;
p=a;
}
else
{
a=a-i;
p=a;
}

for(j=i;j>=1;j--)
{
printf("%d ",p);
p++;
}printf("\n");
}
}

24
PATTERN 10
1
01
101
0101
10101
010101
1010101
01010101

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(i%2!=0&&j%2==0)
{
printf("0 ");
}
if(i%2!=0&&j%2!=0)
{
printf("1 ");
}
if(i%2==0&&j%2!=0)
{
printf("0 ");
}
if(i%2==0&&j%2==0)
{
printf("1 ");
}
}printf("\n");
}
}

PATTERN 11
10101
1010
101
10
1

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n;
scanf("%d",&n);
25
for(i=1;i<=n;i++)
{
for(j=n;j>=i;j--)
{
if(j%2!=0)
printf("1 ");
else
printf("0 ");
}printf("\n");
}
}

PATTERN 12

54321
1234
432
12
3

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n,a,b,k=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i%2==0)
k++;
a=n-k;
b=1;
for(j=n;j>i;j--)
{
if(i%2==0)
{
printf("%d ",a);
a--;
}
else
{
printf("%d ",b);
b++;
}
}printf("\n");
}
}

26
PATTERN 13
EDCBA
EDCB
EDC
ED
E

PROGRAM:

#include<stdio.h>
int main()
{
int i,j,n,k=65;
int a=k;
scanf("%d",&n);
a=a+n-1;
for(i=1;i<=n;i++)
{
for(j=n;j>=i;j--)
{
printf("%c ",a);
a--;
}printf("\n")
a=k+n-1;
}
}

PATTERN 14
*********
* *
* *
* *
*

PROGRAM

#include<stdio.h>
int main()
{
int i,j,n,a=1;
scanf("%d",&n);
int b=(2*n)-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=(2*n)-1;j++)
{
if(i==1)
printf("*");
else
{
27
if(j==a||j==b)
printf("*");
else
printf(" ");
}
}a++;b--;
printf("\n");
}
}

PATTERN 15

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

PROGRAM

#include<stdio.h>
int main()
{
int i,j,n,a,q;
scanf("%d",&n);
int b=((2*n))/2;
a=b;q=a;
for(i=1;i<=n;i++)
{ int k=1;
for(j=1;j<=(2*n)-1;j++)
{
if(i==1)
{
if(j==a)
printf("%d",k);
else
printf(" ");
}
else
{
if((j==a||j==q+2)&&j<=b){
printf("%d",k);
k++;q=j;
28
}
else
printf(" ");
}
}a--;b++;
q=a;
printf("\n");
}
}

29
OUTPUTS:

PATTERN 1:

PATTERN 2:

30
PATTERN 3:

PATTERN 4:

31
PATTERN 5:

PATTERN 6:

32
PATTERN 7:

PATTERN 8:

33
PATTERN 9:

PATTERN 10:

34
PATTERN 11:

PATTERN 12:

35
PATTERN 13:

PATTERN 14:

36
PATTERN 15:

37
CONCLUSIONS:

we have generated different patterns by using numbers . we have used


conditional statements like if , if-else statements, nested loops, for
loop, while loop etc..

REFERENCES:

▪ https :/ / www.prog rami z.co m/ c -prog ra mmi ng/ c -fo r -lo o p

▪ http:// sca nftree.co m/ pro gra ms / c/ prog rams -to-print-py ra mid -


pa tterns -in-c-part -1/

▪ https :/ / www.tuto ri als poi n t.co m/cpro g rammi ng/ c_fo r_l oo
p.htm

35

Вам также может понравиться