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

Short Notes On Programming For Problem Solving

short notes

Uploaded by

Yash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Short Notes On Programming For Problem Solving

short notes

Uploaded by

Yash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT-I

1. Evolution of Programming & Languages

Explanation: Programming languages have evolved to make coding easier and more powerful,
moving from machine language to high-level languages like C, which is easier to understand and
write.

2. Problem Solving through Programming

Explanation: Programming is used to create solutions to real-world problems by breaking down


complex tasks into simpler steps and implementing them as code.

3. Writing Algorithms & Pseudo Code

Definition: An algorithm is a step-by-step procedure to solve a problem. Pseudo code is a high-level


description of an algorithm written in a simple, language-like form.

Example (Pseudo code to add two numbers):

Start

Input num1, num2

sum = num1 + num2

Print sum

End

Explanation: Pseudo code helps plan the logic of the code in simple steps before actual coding.

4. Comments in C

Single-line Comment

Syntax:

// This is a single-line comment

Explanation: Used for short notes about the code; C ignores the comment during compilation.

Multi-line Comment

Syntax:

/* This is a multi-line comment */

Explanation: Used for longer explanations or to temporarily remove code during debugging.

5. Structure of a C Program

Explanation: A basic C program consists of headers, the main() function, and statements within { }.
Example:

#include <stdio.h>

int main() {

printf("Hello, World!");

return 0;

Explanation: #include imports libraries, and main() is where execution starts.

6. Input and Output Statements

Input (scanf) and Output (printf)

Example:

int age;

printf("Enter your age: ");

scanf("%d", &age);

printf("Your age is %d", age);

Explanation: scanf reads user input, and printf displays text to the screen.

7. Variables and Identifiers

Definition: A variable is a named location in memory to store data. An identifier is the name given to a
variable.

Syntax:

int age;

Explanation: int declares an integer variable age that can store a whole number.

8. Constants, Keywords

Constants: Fixed values that do not change during program execution.

Keywords: Reserved words in C, like int, float, return, which cannot be used as variable names.

Example:

const int MAX = 100;

Explanation: const defines a constant integer MAX.


9. Values, Names, Scope, Binding

Scope

Example:

int main() {

int x = 5; // x has local scope within main

Explanation: Variables declared inside a function have a local scope, accessible only within that
function.

Binding - Explanation: Binding is the association of variables with values, such as int x = 5;, where x
is bound to 5.

10. Storage Classes

Definition: Storage classes determine the scope, lifetime, and visibility of variables.

Example:

static int count = 0;

Explanation: static preserves a variable’s value even after the function in which it’s declared ends.

11. Numeric Data Types

Integer

Example:

int age = 21;

Explanation: Declares an integer age to store whole numbers.

Floating Point

Example:

float price = 19.99;

Explanation: Declares a floating-point variable price for decimal values.

12. Non-Numeric Data Types

Char

Example:

char grade = 'A';


Explanation: Declares a character variable grade to store single characters.

String

Example:

char name[] = "John";

Explanation: Declares a character array name to store a string.

13. L-value and R-value in Expressions

Example:

int x = 5; // x is L-value, 5 is R-value

Explanation: L-value is a variable on the left of =, R-value is a value on the right.

14. Increment and Decrement Operators

Syntax:

int x = 5;

x++; // Post-increment

++x; // Pre-increment

Explanation: ++ increases x by 1; -- decreases it by 1.

15. Comma, Arrow, and Assignment Operators

Comma Operator

Example:

int a = 1, b = 2;

Explanation: Allows multiple expressions to be evaluated in a single line.

Arrow Operator (For Pointers to Structs)

Example:

struct Point { int x, y; };

struct Point *p = &point;

p->x = 10;

Explanation: Accesses members of a struct through a pointer.

Assignment Operator

Example:
int x = 5;

Explanation: Assigns a value to a variable.

16. Bitwise and Size-of Operators

Bitwise Operator

Example:

int x = 5 & 3;

Explanation: Performs bitwise operations, here & for bitwise AND.

Size-of Operator

Example:

printf("%lu", sizeof(int));

Explanation: Returns the size of a data type in bytes.

17. Arithmetic, Relational, and Logical Operators

Arithmetic Operators

Example:

int sum = 5 + 3;

Explanation: +, -, *, / for addition, subtraction, multiplication, and division.

Relational Operators

Example:

int x = 5, y = 3;

if (x > y) { printf("x is greater"); }

Explanation: >, <, >=, <= compare two values.

Logical Operators

Example:

if (x > 0 && y < 5) { printf("Condition met"); }

Explanation: &&, ||, ! are used to combine logical expressions.

18. Conditional (Ternary) Operator

Syntax:

int x = (y > 0) ? 1 : 0;
Explanation: Returns 1 if y is positive, otherwise 0.

19. Operator Precedence

Explanation: Determines the order in which operators are evaluated. Multiplication * and division /
have higher precedence than addition + and subtraction -.

20. Expressions with Pre/Post-Increment Operators

Example:

int x = 5;

int y = x++;

Explanation: y is assigned 5, then x is incremented to 6.

UNIT-II

1. Conditional Control Statements

Simple if Statement

Definition: Executes a block of code if a specified condition is true.

Syntax:

if (condition) {

// code to execute if condition is true

Example:

int age = 18;

if (age >= 18) {

printf("You are eligible to vote.\n");

Explanation: This program checks if the age is 18 or older. If true, it prints a message indicating
eligibility to vote.

if...else Statement

Definition: Provides an alternative set of code to execute if the condition is false.

Syntax:
if (condition) {

// code if true

} else {

// code if false

Example:

int age = 16;

if (age >= 18) {

printf("Eligible to vote.\n");

} else {

printf("Not eligible to vote.\n");

Explanation: If age is less than 18, it will print "Not eligible to vote"; otherwise, it prints "Eligible to
vote."

else if and Nested if Statements

Definition: else if provides multiple conditions, and a nested if contains an if within another if.

Syntax:

if (condition1) {

// code if condition1 is true

} else if (condition2) {

// code if condition2 is true

} else {

// code if both conditions are false

Example:

int marks = 85;

if (marks >= 90) {

printf("Grade: A\n");

} else if (marks >= 80) {

printf("Grade: B\n");

} else {
printf("Grade: C\n");

Explanation: This code assigns grades based on marks, demonstrating both else if and

conditional branches.

2. Switch Case

Definition: The switch statement allows a variable to be tested for equality against multiple values.

Syntax:

switch (expression) {

case constant1:

// code

break;

case constant2:

// code

break;

default:

// code

Example:

int day = 3;

switch (day) {

case 1: printf("Monday\n"); break;

case 2: printf("Tuesday\n"); break;

case 3: printf("Wednesday\n"); break;

default: printf("Invalid day\n");

Explanation: The code displays the day based on the value of day. Each case provides a different
message.

3. Unconditional Control Statements

break Statement

Definition: Exits a loop or switch statement.


Example:

for (int i = 1; i <= 5; i++) {

if (i == 3) break;

printf("%d ", i);

Explanation: The loop stops when i equals 3, so only 1 2 are printed.

continue Statement

Definition: Skips the current loop iteration and continues with the next.

Example:

for (int i = 1; i <= 5; i++) {

if (i == 3) continue;

printf("%d ", i);

Explanation: The code skips 3 and prints 1 2 4 5.

goto Statement

Definition: Transfers control to a labeled statement.

Example:

int x = 1;

if (x == 1) goto label;

printf("This won't print.\n");

label:

printf("Jumped to label!\n");

Explanation: If x equals 1, it jumps to label.

4. Looping Control Statements

for Loop

Syntax:

for (initialization; condition; update) {

// code

Example:
for (int i = 1; i <= 5; i++) {

printf("%d ", i);

Explanation: Prints numbers from 1 to 5.

while Loop

Syntax:

while (condition) {

// code

Example:

int i = 1;

while (i <= 5) {

printf("%d ", i);

i++;

Explanation: Prints numbers from 1 to 5 while the condition is true.

do...while Loop

Syntax:

do {

// code

} while (condition);

Example:

int i = 1;

do {

printf("%d ", i);

i++;

} while (i <= 5);

Explanation: Executes the code at least once, then checks the condition.

Nested Loops

Example:

for (int i = 1; i <= 3; i++) {


for (int j = 1; j <= 3; j++) {

printf("%d ", j);

printf("\n");

Explanation: Uses a for loop inside another for loop to print a matrix.

5. Introduction to Arrays

1D Array Declaration and Initialization

Syntax:

int arr[5] = {1, 2, 3, 4, 5};

Explanation: Declares an array of 5 integers.

Accessing and Indexing 1D Arrays

Example:

printf("First element: %d\n", arr[0]);

Explanation: Accesses the first element using index 0.

2D Array Declaration and Initialization

Syntax:

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

Explanation: Declares a 2x3 array.

Accessing Elements in 2D Arrays

Example:

printf("Element at [1][2]: %d\n", arr[1][2]);

Explanation: Accesses the element at row 1, column 2.

6. Pointers

Pointer Declaration and Dereferencing

Syntax:

int x = 10;

int *ptr = &x;

printf("Value at ptr: %d\n", *ptr);


Explanation: Declares a pointer to x and uses * to access the value.

Void Pointers

Definition: A pointer with no specific data type; can point to any data type.

Example:

void *ptr;

int x = 10;

ptr = &x;

printf("Void pointer points to: %d\n", *(int*)ptr);

Explanation: A void pointer points to x, cast to an int.

Pointer-based Array Manipulation

Example:

int arr[3] = {1, 2, 3};

int *ptr = arr;

for (int i = 0; i < 3; i++) {

printf("%d ", *(ptr + i));

Explanation: Uses a pointer to iterate through an array.

UNIT-III

1. String Basics

String Declaration and Initialization

Syntax:

char str[20] = "Hello, world!";

Explanation: Declares a character array str and initializes it with the string "Hello, world!".

2. String Functions

gets() and puts()

Example:

char str[50];

printf("Enter a string: ");

gets(str);
puts("You entered:");

puts(str);

Explanation: gets() takes a string input from the user, and puts() outputs it.

getchar() and putchar()

Example:

char ch;

printf("Enter a character: ");

ch = getchar();

printf("You entered: ");

putchar(ch);

Explanation: getchar() reads a single character, and putchar() prints it.

printf()

Example:

printf("Hello, %s!", "world");

Explanation: printf() outputs formatted text; %s is used to print strings.

3. Built-in String Functions

atoi

Example:

char str[] = "123";

int num = atoi(str);

printf("Converted number: %d", num);

Explanation: atoi() converts a string to an integer.

strlen

Example:

char str[] = "Hello";

printf("Length: %lu", strlen(str));

Explanation: strlen() returns the length of the string.

strcat

Example:

char str1[20] = "Hello, ";


char str2[] = "world!";

strcat(str1, str2);

printf("%s", str1);

Explanation: strcat() appends str2 to str1.

strcmp

Example:

char str1[] = "apple";

char str2[] = "orange";

if (strcmp(str1, str2) == 0) {

printf("Strings are equal");

} else {

printf("Strings are not equal");

Explanation: strcmp() compares two strings lexicographically.

4. Additional String Functions

sprintf

Example:

char buffer[50];

int num = 123;

sprintf(buffer, "Number: %d", num);

puts(buffer);

Explanation: sprintf() formats data into a string stored in buffer.

sscanf

Example:

char str[] = "123 456";

int a, b;

sscanf(str, "%d %d", &a, &b);

printf("a = %d, b = %d", a, b);

Explanation: sscanf() reads formatted data from a string.

strrev (if available in your compiler)


Example:

char str[] = "Hello";

strrev(str);

printf("%s", str);

Explanation: strrev() reverses the string in place.

strcpy

Example:

char src[] = "Hello";

char dest[20];

strcpy(dest, src);

printf("%s", dest);

Explanation: strcpy() copies src into dest.

strstr

Example:

char str[] = "Hello, world!";

char *pos = strstr(str, "world");

printf("Found at position: %s", pos);

Explanation: strstr() finds a substring within a string.

strtok

Example:

char str[] = "apple,banana,orange";

char *token = strtok(str, ",");

while (token != NULL) {

printf("%s\n", token);

token = strtok(NULL, ",");

Explanation: strtok() splits a string into tokens based on a delimiter.

5. Operations on Strings

Example:

char str1[20] = "Hello";


char str2[20] = "World";

printf("Concatenated: %s", strcat(str1, str2));

Explanation: Demonstrates concatenation as one of the common string operations.

6. Function Basics

Function Prototype Declaration and Definition

Example:

void greet(); // Prototype

void greet() { // Definition

printf("Hello, student!");

Explanation: Defines a function greet and its prototype.

Actual and Formal Parameters

Example:

void display(int num) { // Formal parameter

printf("Number: %d", num);

int main() {

int n = 5;

display(n); // Actual parameter

Explanation: Shows how n is the actual parameter passed to display.

Function with and without Arguments

Example:

void greet() { printf("Hello!"); }

void greetUser(char *name) { printf("Hello, %s!", name); }

Explanation: greet() has no arguments; greetUser takes a string as an argument.

Function with and without Return Values

Example:

int add(int a, int b) { return a + b; }

void display() { printf("Hello"); }


Explanation: add returns an integer, while display has no return value.

7. Call by Value and Call by Reference

Call by Value

Example:

void increment(int x) { x++; }

Explanation: Call by value passes a copy; changes in the function do not affect the original variable.

Call by Reference

Example:

void increment(int *x) { (*x)++; }

Explanation: Call by reference passes an address, so changes affect the original variable.

8. Passing Array to Function

Example:

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

Explanation: The entire array arr is passed to the function to print each element.

Passing Array Elements to Function

Example:

void displayElement(int element) {

printf("Element: %d", element);

Explanation: Passes individual elements instead of the entire array.

9. Function Pointers

Example:

void greet() { printf("Hello!\n"); }

int main() {
void (*ptr)() = greet;

ptr();

Explanation: ptr is a function pointer that stores the address of greet, which is then called.

You might also like