Sunday, 18 January 2026

Programming for Problem Solving-Control Structures

 1. Check Positive or Negative (if)

#include <stdio.h>

#include<conio.h>

int main() {

    int n = 5;

    if (n > 0)

        printf("Positive number");

    return 0;

   getch();

}


Output

Positive number


2. Check Even or Odd (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int n = 4;

    if (n % 2 == 0)

        printf("Even number");

    else

        printf("Odd number");

    return 0;

    getch();

}


Output

Even number


3. Find Largest of Two Numbers (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10, b = 20;

    if (a > b)

        printf("A is greater");

    else

        printf("B is greater");

    return 0;

    getch();

}

Output

B is greater


4. Check Voting Eligibility (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int age = 18;

    if (age >= 18)

        printf("Eligible to vote");

    else

        printf("Not eligible");

    return 0;

    getch();

}


Output

Eligible to vote


5. Check Leap Year (if–else)

#include <stdio.h>

#include<conio.h>

int main() {

    int year = 2024;

    if (year % 4 == 0)

        printf("Leap year");

    else

        printf("Not a leap year");

    return 0;

    getch();

}


Output

Leap year


6. Largest of Three Numbers (else–if ladder)

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10, b = 30, c = 20;

    if (a > b && a > c)

        printf("A is largest");

    else if (b > c)

        printf("B is largest");

    else

        printf("C is largest");

    return 0;

    getch();

}


Output

B is largest


7. Grade System (else–if ladder)

#include <stdio.h>

#include<conio.h>

int main() {

    int marks = 85;

    if (marks >= 90)

        printf("Grade A");

    else if (marks >= 75)

        printf("Grade B");

    else if (marks >= 50)

        printf("Grade C");

    else

        printf("Fail");

    return 0;

    getch();

}


Output

Grade B


8. Simple Calculator (switch)

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10, b = 5;

    char op = '+';

    switch (op) {

        case '+': printf("Sum = %d", a + b); break;

        case '-': printf("Difference = %d", a - b); break;

        case '*': printf("Product = %d", a * b); break;

        case '/': printf("Quotient = %d", a / b); break;

    }

    return 0;

    getch();

}

Output

Sum = 15


9. Print Numbers 1 to 5 (for loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

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

        printf("%d ", i);

    return 0;

    getch();

}

Output

1 2 3 4 5


10. Sum of First 5 Numbers (for loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i, sum = 0;

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

        sum += i;

    printf("Sum = %d", sum);

    return 0;

    getch();

}

Output

Sum = 15


11. Print Numbers Using while Loop

#include <stdio.h>

#include<conio.h>

int main() {

    int i = 1;

    while (i <= 5) {

        printf("%d ", i);

        i++;

    }

    return 0;

    getch();

}

Output

1 2 3 4 5


12. Reverse a Number (while loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int n = 123, rev = 0;

    while (n != 0) {

        rev = rev * 10 + n % 10;

        n /= 10;

    }

    printf("Reverse = %d", rev);

    return 0;

    getch();

}

Output

Reverse = 321


13. Print Numbers Using do–while Loop

#include <stdio.h>

#include<conio.h>

int main() {

    int i = 1;

    do {

        printf("%d ", i);

        i++;

    } while (i <= 5);

    return 0;

}

Output

1 2 3 4 5


14. Use of break Statement

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

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

        if (i == 3)

            break;

        printf("%d ", i);

    }

    return 0;

    getch();

}

Output

1 2


15. Use of continue Statement

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

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

        if (i == 3)

            continue;

        printf("%d ", i);

    }

    return 0;

    getch();

}

Output

1 2 4 5

Saturday, 17 January 2026

Programming for Problem Solving-Basics of C Programming

Programming for Problem Solving-Basics of C Programming

 1. Simple C Program (Basic Structure)

#include <stdio.h>

#include<conio.h>

int main() 

{

    printf("Welcome to Information Technology Dept.);

    return 0;

    getch();

}

Output

Welcome to Information Technology Dept.


2. Program Using return 0

#include <stdio.h>

#include<conio.h>

int main() 

{

    printf("Program ends with return");

    return 0;

    getch();

}


Output

Program ends with return


3. Print Name

#include <stdio.h>

#include<conio.h>

int main()

{

    printf("My name is James");

    return 0;

    getch();

}

Output

My name is James


4. Print Multiple Lines

#include <stdio.h>

#include<conio.h>

int main() {

    printf("C Programming\nBasics");

    return 0;

    getch();

}

Output

C Programming

Basics


5. Program with Comments

#include <stdio.h>

#include<conio.h>

int main() {

    // This is a comment

    printf("Comments example");

    return 0;

    getch();

}

Output

Comments example


6. Integer Variable

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 10;

    printf("%d", a);

    return 0;

    getch();

}

Output

10


7. Float Variable

#include <stdio.h>

#include<conio.h>

int main() {

    float x = 3.5;

    printf("%f", x);

    return 0;

    getch();

}

Output

3.500000


8. Character Variable

#include <stdio.h>

#include<conio.h>

int main() {

    char ch = 'A';

    printf("%c", ch);

    return 0;

    getch();

}

Output

A


9. Multiple Variables

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 5, b = 10;

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

    return 0;

    getch();

}

Output

5 10


10. User Input Integer

#include <stdio.h>

#include<conio.h>

int main()

{

    int n;

    scanf("%d", &n);

    printf("%d", n);

    return 0;

    getch();

}

Output

5


11. User Input Float

#include <stdio.h>

#include<conio.h>

int main() 

{

    float f;

    scanf("%f", &f);

    printf("%f", f);

    return 0;

    getch();

}


Output

2.500000


12. Size of Data Types

#include <stdio.h>

#include<conio.h>

int main()

{

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

    return 0;

    getch();

}

Output

4


13. Constant Variable

#include <stdio.h>

#include<conio.h>

int main() 

{

    const int a = 10;

    printf("%d", a);

    return 0;

    getch();

}

Output

10


14. Sum Using Variables

#include <stdio.h>

#include<conio.h>

int main() 

{

    int a = 3, b = 7;

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

    return 0;

    getch();

}

Output

10


15. Swap Using Temporary Variable

#include <stdio.h>

#include<conio.h>

int main() 

{

    int a = 5, b = 10, t;

    t = a;

    a = b;

    b = t;

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

    return 0;

    getch();

}

Output

10 5


16. Arithmetic Operators

#include <stdio.h>

#include<conio.h>

int main()

{

    int a = 10, b = 5;

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

    return 0;

    getch();

}

Output

15


17. Subtraction Operator

#include <stdio.h>

#include<conio.h>

int main()

{

    int a = 10, b = 4;

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

    return 0;

    getch();

}

Output

6


18. Multiplication Operator

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 3, b = 4;

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

    return 0;

    getch();

}

Output

12


19. Division Operator

#include <stdio.h>

#include<conio.h>

int main()

{

    int a = 10, b = 2;

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

    return 0;

    getch();

}

Output

5


20. Modulus Operator

#include <stdio.h>

#include<conio.h>

int main()

{

    int a = 10, b = 3;

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

    return 0;

    getch();

}

Output

1


21. Relational Operator

#include <stdio.h>

#include<conio.h>

int main() 

{

    int a = 5, b = 5;

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

    return 0;

    getch();

}

Output

1


22. Logical AND

#include <stdio.h>

#include<conio.h>

int main() 

{

    int a = 1, b = 1;

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

    return 0;

    getch();

}

Output

1


23. Logical OR

#include <stdio.h>

#include<conio.h>

int main() 

{

    int a = 0, b = 1;

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

    return 0;

    getch();

}

Output

1


24. Increment Operator

#include <stdio.h>

#include<conio.h>

int main()

{

    int a = 5;

    a++;

    printf("%d", a);

    return 0;

    getch();

}

Output

6


25. Assignment Operator

#include <stdio.h>

#include<conio.h>

int main() 

{

    int a = 10;

    a += 5;

    printf("%d", a);

    return 0;

    getch();

}

Output

15

Wednesday, 22 January 2025

Basic-3 C programming

Course Practical List-Programming for Problem Solving 

1.Write a program that performs basic arithmetic operations (addition, subtraction, multiplication, and division) and demonstrates the use of different data types.

 

#include <stdio.h>

 int main() {

    int a = 10, b = 3;

    float div;

     printf("Addition: %d\n", a + b);

    printf("Subtraction: %d\n", a - b);

    printf("Multiplication: %d\n", a * b);

     div = (float)a / b;

    printf("Division: %.2f\n", div);

     return 0;

}

  

2.Create a program that uses if, else, and switch statements to implement a simple menu-driven application. Use loops (for, while, and do-while) to repeat tasks.

 

#include <stdio.h>

 int main() {

    int choice, a, b;

     do {

        printf("\n1.Add\n2.Subtract\n3.Multiply\n4.Exit\n");

        printf("Enter choice: ");

        scanf("%d", &choice);

         if (choice >= 1 && choice <= 3) {

            printf("Enter two numbers: ");

            scanf("%d %d", &a, &b);

        }

         switch (choice) {

            case 1: printf("Sum = %d\n", a + b); break;

            case 2: printf("Difference = %d\n", a - b); break;

            case 3: printf("Product = %d\n", a * b); break;

            case 4: printf("Exiting...\n"); break;

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

        }

    } while (choice != 4);

     return 0;

}

  

Develop a program that calculates the factorial of a number using both iterative and recursive functions.

 

#include <stdio.h>

 int factRec(int n) {

    if (n == 0) return 1;

    return n * factRec(n - 1);

}

 int main() {

    int n, fact = 1;

     printf("Enter number: ");

    scanf("%d", &n);

     for (int i = 1; i <= n; i++)

        fact *= i;

     printf("Iterative Factorial: %d\n", fact);

    printf("Recursive Factorial: %d\n", factRec(n));

     return 0;

}

  Write a program to perform various operations on arrays (e.g., sorting, searching) and strings (e.g., concatenation, comparison).

 

#include <stdio.h>

#include <string.h>

 int main() {

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

    char s1[20] = "Hello";

    char s2[20] = "World";

     // Sorting array

    for (int i = 0; i < 5; i++)

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

            if (a[i] > a[j]) {

                temp = a[i];

                a[i] = a[j];

                a[j] = temp;

            }

    printf("Sorted Array: ");

    for (int i = 0; i < 5; i++)

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

     // String operations

    strcat(s1, s2);

    printf("\nConcatenation: %s", s1);

    printf("\nComparison: %d\n", strcmp("abc", "abc"));

     return 0;

}

Implement a program that uses pointers to create and manipulate dynamic arrays, demonstrating the useof malloc, calloc, realloc, and free.

 

#include <stdio.h>

#include <stdlib.h>

 int main() {

    int *a, n = 3;

     a = (int *)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++)

        a[i] = i + 1;

     a = (int *)realloc(a, 5 * sizeof(int));

    a[3] = 4;

    a[4] = 5;

     for (int i = 0; i < 5; i++)

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

     free(a);

    return 0;

}

 

Design a student record system using structures that store and display information such as name, roll number, and grades.

 

#include <stdio.h>

 struct student {

    char name[20];

    int roll;

    float marks;

};

 int main() {

    struct student s;

     printf("Enter name, roll, marks: ");

    scanf("%s %d %f", s.name, &s.roll, &s.marks);

     printf("Name: %s\nRoll: %d\nMarks: %.2f\n",

           s.name, s.roll, s.marks);

     return 0;

}

 

Write a program to read from and write to files, such as creating a simple text editor that performs basic file operations.

 

#include <stdio.h>

 int main() {

    FILE *fp;

    char text[50];

     fp = fopen("file.txt", "w");

    fprintf(fp, "Hello File");

    fclose(fp);

     fp = fopen("file.txt", "r");

    fscanf(fp, "%s", text);

    printf("File Content: %s", text);

    fclose(fp);

     return 0;

}

 

Implement a singly linked list with operations like insertion, deletion, and traversal.

#include <stdio.h>

#include <stdlib.h>

 struct node {

    int data;

    struct node *next;

};

 int main() {

    struct node *head = NULL, *newnode, *temp;

    int n = 3;

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

        newnode = (struct node *)malloc(sizeof(struct node));

        newnode->data = i + 1;

        newnode->next = NULL;

         if (head == NULL)

            head = newnode;

        else {

            temp = head;

            while (temp->next)

                temp = temp->next;

            temp->next = newnode;

        }

    }

 

    temp = head;

    while (temp) {

        printf("%d ", temp->data);

        temp = temp->next;

    }

    return 0;

 

Develop programs to simulate stack operations (push, pop, peek) and queue operations (enqueue, dequeue) using arrays and linked lists.

 

#include <stdio.h>

 int main() {

    int stack[5], top = -1;

     stack[++top] = 10;

    stack[++top] = 20;

    printf("Popped: %d\n", stack[top--]);

 

    int queue[5], front = 0, rear = 0;

    queue[rear++] = 5;

    queue[rear++] = 15;

    printf("Dequeued: %d\n", queue[front++]);

     return 0;

}

 

Provide students with a program containing intentional errors and inefficiencies. Have them use debugging tools (like gdb) to find and fix the errors and optimize the code for better performance.

 #include <stdio.h>

 int main() {

    int i;

    for (i = 0; i <= 5; i++)   // error: should be i < 5

        printf("%d ", i);

    return 0;

}



Tuesday, 21 January 2025

Basic-2 C programming

 16. Program to Check Prime Number

#include <stdio.h>

#include<conio.h>

int main() {

    int num = 7, i, flag = 0;

    for (i = 2; i <= num / 2; i++) {

        if (num % i == 0) {

            flag = 1;

            break;

        }

    }

    if (flag == 0)

        printf("Prime number");

    else

        printf("Not a prime number");


    return 0;

    getch();

}


Output

Prime number


17. Program to Print Prime Numbers from 1 to 10

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, j, flag;

    for (i = 2; i <= 10; i++) {

        flag = 0;

        for (j = 2; j <= i / 2; j++) {

            if (i % j == 0) {

                flag = 1;

                break;

            }

        }

        if (flag == 0)

            printf("%d ", i);

    }

    return 0;

     getch();

}


Output

2 3 5 7


18. Program to Find Fibonacci Series

 #include <stdio.h>

#include<conio.h>

int main() {

    int n = 5, a = 0, b = 1, c, i;

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

    for (i = 2; i < n; i++) {

        c = a + b;

        printf("%d ", c);

        a = b;

        b = c;

    }

    return 0;

    getch();

}


Output

0 1 1 2 3


19. Program Using Function (Addition)

#include <stdio.h>

#include<conio.h>

int add(int a, int b) {

    return a + b;

}

int main() {

    int result;

    result = add(10, 20);

    printf("Sum = %d", result);

    return 0;

    getch();

}


Output

Sum = 30


20. Program to Find String Length (Without strlen)

 #include <stdio.h>

#include<conio.h>

int main() {

    char str[] = "Hello";

    int i = 0;

    while (str[i] != '\0') {

        i++;

    }

    printf("Length = %d", i);

    return 0;

    getch();

}


Output

Length = 5


21. Program to Reverse a String

 #include <stdio.h>

#include<conio.h>

int main() {

    char str[] = "Cprogram";

    int i, len = 0;


    while (str[len] != '\0') {

        len++;

    }

    for (i = len - 1; i >= 0; i--) {

        printf("%c", str[i]);

    }

    return 0;

    getch();

}


Output

margorpC


22. Program to Copy One String to Another

 #include <stdio.h>

#include<conio.h>

int main() {

    char str1[] = "Hello";

    char str2[10];

    int i = 0;

    while (str1[i] != '\0') {

        str2[i] = str1[i];

        i++;

    }

    str2[i] = '\0';

    printf("Copied string: %s", str2);

    return 0;

    getch();

}


Output

Copied string: Hello


23. Program Using Pointer

 #include <stdio.h>

#include<conio.h>

int main() {

    int a = 10;

    int *p;

    p = &a;

    printf("Value of a = %d\n", a);

    printf("Value using pointer = %d", *p);

    return 0;

    getch();

}


Output

Value of a = 10

Value using pointer = 10


24. Program to Swap Two Numbers (Using Function)

 #include <stdio.h>

#include<conio.h>

void swap(int *x, int *y) {

    int temp;

    temp = *x;

    *x = *y;

    *y = temp;

}

int main() {

    int a = 5, b = 10;

    swap(&a, &b);

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

    return 0;

    getch();

}


Output

a = 10, b = 5




Monday, 20 January 2025

Basic-1 C programming

 1. Hello World Program

#include <stdio.h>

#include<conio.h>

int main() {

    printf("Hello, World!");

    return 0;

    getch();

}

Output

Hello, World!


2. Program to Add Two Numbers

#include <stdio.h>

#include<conio.h>

int main() {

    int a, b, sum;

    a = 10;

    b = 20;

    sum = a + b;

    printf("Sum = %d", sum);

    return 0;

    getch();

}

Output

Sum = 30


3. Program to Find Even or Odd Number

#include <stdio.h>

#include<conio.h>

int main() {

    int num = 7;

    if (num % 2 == 0)

        printf("Even number");

    else

        printf("Odd number");


    return 0;

     getch();

}

Output

Odd number


4. Program to Find Largest of Two Numbers

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 15, b = 25;


    if (a > b)

        printf("A is greater");

    else

        printf("B is greater");

    return 0;

    getch();

}

Output

B is greater


5. Program to Print Numbers from 1 to 5 (Using Loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

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

        printf("%d ", i);

    }

    return 0;

}

Output

1 2 3 4 5


6. Program to Find Factorial of a Number

#include <stdio.h>

#include<conio.h>

int main() {

    int i, n = 5;

    int fact = 1;


    for (i = 1; i <= n; i++) {

        fact = fact * i;

    }

    printf("Factorial = %d", fact);

    return 0;

    getch();

}


Output

Factorial = 120


7. Program to Check Vowel or Consonant

#include <stdio.h>

#include<conio.h>

int main() {

    char ch = 'a';

    if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

        printf("Vowel");

    else

        printf("Consonant");

    return 0;

    gecth();

}


Output

Vowel

8. Program to Take Input from User

#include <stdio.h>

#include<conio.h>


int main() {

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);


    printf("You entered: %d", num);

    return 0;

    gecth();

}

Output

Enter a number: 5

You entered: 5


9. Program to Find Largest of Three Numbers

 #include <stdio.h>

#include<conio.h>

int main() {

    int a, b, c;

    a = 10;

    b = 25;

    c = 15;

    if (a > b && a > c)

        printf("A is largest");

    else if (b > c)

        printf("B is largest");

    else

        printf("C is largest");


    return 0;

    gecth();

}


Output

B is largest


10. Program to Print Multiplication Table

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, n = 5;


    for (i = 1; i <= 10; i++) {

        printf("%d x %d = %d\n", n, i, n * i);

    }


    return 0;

    getch();

}

Output

5 x 1 = 5

5 x 2 = 10

...

5 x 10 = 50

11. Program to Reverse a Number

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 123, rev = 0;


    while (num != 0) {

        rev = rev * 10 + num % 10;

        num = num / 10;

    }

    printf("Reversed number = %d", rev);

    return 0;

    gecth();

}


Output

Reversed number = 321


12. Program to Check Palindrome Number

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 121, temp, rev = 0;

    temp = num;

    while (num != 0) {

        rev = rev * 10 + num % 10;

        num = num / 10;

    }

    if (temp == rev)

        printf("Palindrome number");

    else

        printf("Not a palindrome");


    return 0;

    getch();

}


Output

Palindrome number


13. Program to Find Sum of Digits

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 123, sum = 0;


    while (num != 0) {

        sum = sum + num % 10;

        num = num / 10;

    }

    printf("Sum of digits = %d", sum);

    return 0;

    getch();

}


Output

Sum of digits = 6


14. Program Using Array (Print Elements)

 #include <stdio.h>

#include<conio.h>

int main() {

    int i;

    int arr[5] = {10, 20, 30, 40, 50};

    for (i = 0; i < 5; i++) {

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

    }

    return 0;

    getch();

}


Output

10 20 30 40 50


15. Program to Find Sum of Array Elements

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, sum = 0;

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

    for (i = 0; i < 5; i++) {

        sum = sum + arr[i];

    }

    printf("Sum = %d", sum);

    return 0;

getch():

}


Output

Sum = 15

Thursday, 21 November 2024

PHP Advanced-2

 Example-1


<?php

 echo "<h3>Date/Time functions</h3><br>";

 echo date("d/m/y")."<br/>";

 echo date("d M,Y")."<br/>";

 $date_array = getdate(); 

 foreach ( $date_array as $key => $val )

 {  

  print "$key = $val<br />"; 

 }

 echo var_dump(checkdate(2,29,2003))."<br>";

 echo var_dump(checkdate(2,29,2004))."<br>";

 echo time();

 echo(date("M-d-Y",mktime(0,0,0,12,36,2001))."<br>");

 echo(date("M-d-Y",mktime(0,0,0,1,1,99))."<br>");

 $formated_date = "Today's date: "; 

 $formated_date .= $date_array['mday'] . "/"; 

 $formated_date .= 

 $date_array['mon'] . "/";

 $formated_date .= $date_array['year']; 

 print $formated_date; 

 print date("m/d/y G.i:s<br>", time()); 

 print "Today is ";

 print date("j of F Y, \a\\t g.i a", time()); 

 echo "<br><h3>Math functions</h3>";

 echo abs(6.7)."<br>";

 echo abs(-6.7)."<br>";

 echo abs(-3)."<br>";

 echo abs(3);

 echo ceil(0.60)."<br>";

 echo ceil(0.40)."<br>";

 echo ceil(5)."<br>";

 echo ceil(5.1)."<br>";

 echo ceil(-5.1)."<br>";

 echo ceil(-5.9);

 echo exp(0)."<br>";

 echo exp(1)."<br>";

 echo exp(10)."<br>";

 echo exp(4.8);

 echo floor(0.60)."<br>";

 echo floor(0.40)."<br>";

 echo floor(5)."<br>";

 echo floor(5.1)."<br>";

 echo floor(-5.1)."<br>";

 echo floor(-5.9)."<br>";

 echo rand(10,100)."<br>";

 echo min(2,4,6,8,10)."<br>";

 echo max(2,4,6,8,10)."<br>";

 echo pow(2,4)."<br>";

 echo round(0.60)."<br>";

?>

 Example-2


<?php

         $string1="Hello Friend ";

         $string2="Welcome";

         echo "<br>";

        echo $string1 . " " . $string2;

        echo "<br>";

        echo strlen($string1);

        echo "<br>";

        echo strpos("Hello world!","world"); 

        echo "<br>";

         echo strtoupper($string1);

        echo "<br>";

        echo substr_replace($string1, "T",6,1);

        echo "<br>";

        echo substr($string1,0,5);

        echo "<br>";

        echo substr_count($string1,"e");

?> 


 Example-3

<?php     
$to_email = 'vainshbharatit@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
$headers = 'vainsh_bharat@yahoo.com;
mail($to_email,$subject,$message,$headers);
?>


 Example-4

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

 Example-5

<?php
// Start the session
session_start();
?>

<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>


 Example-6

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["favcolor"] = "yellow";
print_r($_SESSION);
?>

</body>
</html>


Example-7

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// remove all session variables
session_unset();

// destroy the session
session_destroy();
?>

</body>
</html>

PHP Advanced-1

 Example-1

<?php

echo"First 10 Fibonacci no.";

$i=1;

$j=1;

echo"$i , $j ,";

for($k=2;$k<50;$k++)

{

$k=$i+$j;

$i=$j;

$j=$k;

echo" $k ,";

}

?>


 Example-2

<!DOCTYPE html>

<html>

<body>


<form name="f1" action="upload.php" method="post" enctype="multipart/form-data">

  Select image to upload:

  <input type="file" name="fileToUpload" id="fileToUpload">

  <input type="submit" value="Upload Image" name="submit">

</form>


</body>

</html>


 Example-3


<?php

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

  if($check !== false) {

    echo "File is an image - " . $check["mime"] . ".";

    $uploadOk = 1;

  } else {

    echo "File is not an image.";

    $uploadOk = 0;

  }

}


// Check if file already exists

if (file_exists($target_file)) {

  echo "Sorry, file already exists.";

  $uploadOk = 0;

}


// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

  echo "Sorry, your file is too large.";

  $uploadOk = 0;

}


// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

  $uploadOk = 0;

}


// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

  echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file

} else {

  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

  } else {

    echo "Sorry, there was an error uploading your file.";

  }

}

?>


Example-4

<?php
function calcAverage()
{
    // initialize value to be used in calculation
    $total = 0;

    // find out how many arguments were given
    $arguments = func_num_args();     

    // loop to process each argument separately
    for ($i = 0; $i < $arguments; $i++) {
        // add the value in the current argument to the total
        $total += func_get_arg($i);
    }

    // after adding all arguments, calculate the average
    $average = $total / $arguments;

    // return the average
    return $average;
}

// invoke the function with 5 arguments

echo calcAverage(44, 55, 66, 77, 88);

// invoke the function with 8 arguments
echo calcAverage(12, 34, 56, 78, 90, 9, 8, 7);



Example-5

<html>

<body>

<?php

if(isset($_POST["submit"]))

{

$value = $_POST['val1'];

}

else

{

$value = "";

}

?>


<form name="f1" action="#" method="post">


Enter Age<input type="text" name="val1" required/>


<input type="submit" name="submit">

</form>



<?php

if($value)

{

if($value>=18)

    echo "You are Eligible for Vote"; }

else

    echo "You are not Eligible for Vote"; }

}

?>

</body>

</html>


Example-6

<html>
<body>

<form name="f1" action="verify.php" method="post">
<select name="book">
    <option></option>
    <option>ADA</option>
    <option>WT</option>
</select>

Quantity
<input type="text" name="q1">
<input type="submit">
</form>

</body>
</html>


Example-7

<html>
<body>
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("h:m:s")."<br>";
?>
<?php require();>
</body>
</html>


PHP Basic-2

 Example-1


<!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">

  <?php

if(isset($_POST['submit']))

{

        $num=$_POST['num1'];

        define('NUM',$num);


        for($i=1;$i<=10;$i++)

        {

            echo $i*NUM;

            echo '</br>';

        }

}

  ?>

<form name="f1" method="post" action="#">

Enter Number<input type="text" name="num1" size="25" required>

<input type="submit" name="submit"/>

</form>

</body>

</html>


 Example-2

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

$x=8;
if($x>5)
{
    echo "$x is greater than 5</br>";
}
if($x<10)
{
    echo "$x is also less than 10</br>";

}

  ?>

 Example-3

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">
  <?php
      $a=80;
      echo "your grade is:";
      switch($a)
      {
          case $a>=90:
            echo "A</br>";
          break;

          case $a>=80:
            echo "B</br>";
          break;

          case $a>=70:
            echo "C</br>";
          break;

          default
          echo "you have fail";
         break;
      }
  ?>
</body>
</html>


 Example-4

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

      $a=10;

      while($a>=0)
      {
          echo"no is".$a."\n";
          $a--;

      }
     

  ?>

  <?php
          echo "hello world".$a;
          print ""

  ?>


 Example-5

<?php
          if(isset($_POST['submit']))
          {
            
           $num1=$_POST['val1'];

           if($num1%2==0)
              {
                  echo "Even No.";
              }
              else
              {
                  echo "Odd No.";
              }  
            }
          
   ?>

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <form name="f1" action="#" method="post">

Enter No.<input type="text" name="val1" size="20">

<input type="submit" name="submit" value="check">
        </form>


   </body>
   </html>
   

 Example-6

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>

  <body bgcolor="white">

  <?php
$a=0;
$b=0;

for($i=0;$i<=5;$i++)
{
    $a+=10;
    $b+=5;
}

echo"at end of the loop a= $a and b=$b</br>";
 


$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value </br>";
}


function myname($name)
{
    echo "your name is".$name."</br>";    

}

echo "Hi!";
myname("james");


function mul($x,$y)
{
    $total=$x*$y;
    return $total;

}

echo "the value of=".mul(7,3);
echo "</br>";
function add_five(&$value) {
    $value += 5;
  }
  
  $num = 2;
  add_five($num);
  echo $num;
  
  echo "</br>";

  function hello()
  {

    echo "hi";

  }

  $check="hello";

  $check();

  $sub=array("wd","ADA");
  echo $sub[0]."and" .$sub[1]."are good subjects";


  $char=array(name=>"suresh",age=>22);
    echo $char[age];

    $char1=array(
          array(firstname=>"ravi",lastname=>"patel"),
          array(firstname=>"mayur",lastname=>"xyz")
        );

        //echo $char1[firstname];

        foreach($char1 as $val)
        {
            foreach($val as $key=>$final_val)
            {
                echo "$key:$final_val</br>";

            }
        }

        $cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
echo"</br>";

$check=array("free","program","apple");
sort($check);
echo $check;
//print($check);

foreach($check as $var)
{
    echo "$var</br>";

}

//array_slice()

$first=array("a","b","c","d","e");
$second=array_slice($first,1,2);
foreach($second as $var)
{
    echo "$var</br>";

}

//array_merge()

$first=array("a","b","c","d","e");
$second=array(1,2,3);
$third=array_merge($first,$second);
foreach($third as $var)
{
    echo "$var";

}

?>
</body>
</html>


 Example-6

<html>
<body>
<?php
if(isset($_POST["submit"]))
{
$value = $_POST['val_1'];
}
else
{
$value = "";
}
?>
<form method="POST" action="">
Enter your age <br>
<input type="text" name="val_1" value="<?php echo $value; ?>"> <br />
<input type="submit" name="submit" value= "click here">
</form>
<?php
if($value)
{
if($value>=18)
{ echo "You are Eligible for Vote"; }
else
{ echo "You are not Eligible for Vote"; }
}
?>
</body>
</html>


 Example-7

<html>
<head>
<title>STAR</title>
</head>
<body>
<h2>PYRAMIND</h2>
<?php
if(isset($_POST[“Submit”]))
{
$val = $_POST[“val”];
$symbol = $_POST[“symbol”];
}
else if(isset($_POST[“clear”]))
{
$val = “”;
$symbol = “”;
}
else
{
$val = “”;
$symbol = “”;
}?>
<form method=”POST”>
Enter the loop Number <input type=”text” name=”val” value="<?php echo”$val”; ?>" />
</br>
Enter the Symbol <input type=”text” name=”symbol” value="<?php echo”$symbol”; ?>"/>
</br>
<input type=”submit” name=”Submit”>
<input type=”submit” name=”clear” value=”Reset” >

</form>
<?php
if($val and $symbol)
{
if(is_numeric($val))
{ for($i=1;$i<=$val;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo “&nbsp “.$symbol;
}
echo “<br />”;
}
}

else
{
echo “Enter loop in Digits”;
}
}
?>
</body>
</html>


PHP Basic-1

 Example 1: 

 <!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">


  <?php

        echo "hello world</br>";

        print "hi!how are you?";

          ?>

</body>

</html>



 Example 2:

<html>

<head>

<script type="text/javascript">

$(document).ready(function(e){

 $('#btnSubmit').click(function(){ 

 var email = $('#txtEmail').val();

 if ($.trim(email).length == 0) {

 alert('Please Enter Valid Email Address');

 return false;

 }

 if (validateEmail(email)) {

 alert('Valid Email Address');

 return false;

 }

 else {

 alert('Invalid Email Address');

 return false;

 }});

});

</script>

</head> <body>

Email Address: <input type="text" id="txtEmail"/><br/>

<input type="submit" id="btnSubmit" Value="Submit" />

</body>

</html> 


 Example 3:

<html>

<body>

<form name="myform" action="welcome.php" method="post">

Name: <input type="text" name="name"><br>

E-mail: <input type="text" name="email"><br>

<input type="submit" name="submit">

</form>

</body>

</html>


 Example 4:

<?php

if(isset($_POST['submit']))

{

$num1=$_POST['num1'];

$num2=$_POST['num2'];


$result=$num1+$num2;


echo "Sum of " .$num1. " and ".$num2. " is " .$result;

}

?>

<!DOCTYPE html>

<html>

<head>

<title>Add two number</title>

</head>

<body>

<table>

<form name="add" action="#" method="post">

<tr>

<td>Number 1 :</td>

<td><input type="text" name="num1" required></td>

</tr>

<tr>

<td>Number 2 :</td>

<td><input type="text" name="num2" required></td>

</tr>

<tr>

<td></td>

<td><input type="submit" value="Add" name="submit" /></td>

</tr>

</form>

</table>

</body>

</html>


  Example 5:

<!DOCTYPE html>

<html>

  <head>

    <title></title>

  </head>

  <body bgcolor="white">

  <?php

        echo "hello world</br>";

        print "hi!how are you?";

        $text1="hello";

        $text2=10;

        echo $text1;

        echo $text2;

        echo $text1 ." " .$text2;

        echo $text1;

        print($text1);

        $bikes=array("yamaha","royal","honda","hero");

        var_dump($bikes);

        echo  "array element 1:$bikes[0] $bikes[1]$bikes[2] </br>";

        $x = "Hello world!";

        $x = null;

        var_dump($x);

echo "</br>";

      $x = 5985;

var_dump($x);

$cars = array("Volvo","BMW","Toyota");

var_dump($cars);

echo "</br>";

$text="hello world";

$text=null;

echo $text;

var_dump($text);

echo "</br>";

        $text2="xyz\"this is string\"pqr";

        echo $text2;

        echo "</br>";

        echo strlen("php programming");

        echo "</br>";

        echo str_word_count("php programming language"); 

        echo "</br>";

        echo strrev("JAMES"); 

        echo strpos("Hello world!", "world"); 

        echo strpos("developer news","news");

       $x = 5985;

        var_dump(is_int($x));

        $x = 1.9e411;

        var_dump($x);

        $x = acos(8);

        var_dump($x);

        echo "</br>";

// Cast float to int

$x = 23465.768;

$int_cast = (int)$x;

echo $int_cast;

echo "<br>";

// Cast string to int

$x = "23465.768";

$int_cast = (int)$x;

echo $int_cast;

echo "<br>";

echo(pi()); // returns 3.1415926535898

echo "<br>";

echo "<br>";

echo(rand(1000, 10000));

define("cars", [

  "Alfa Romeo",

  "BMW",

  "Toyota"

]);

echo cars[0];

    ?>

</body>

</html>


  Example 6:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php

    $a=45;
    $b=35;

    $temp=$a;
    $a=$b;
    $b=$temp;

    echo "after the swapping :</br></br>";
    echo "a=".$a. "b=".$b;

  ?>

  Example 7:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="white">

  <?php


    $a=45;
    $b=35;
    $c=20;

     
    if($a>$b and $a>$c)
    {
        echo "Max No. is $a";
    }
    elseif($b>$c)
    {
        echo "Max No. is $b";

    }
    else
    {
      echo "Max No. is $c";

    }

   ?>
   </body>
   </html>