0% found this document useful (0 votes)
3 views5 pages

Code

The document provides a structured guide for programming in Turbo C, covering essential topics such as loops, arrays, string manipulation, functions, and structures. It emphasizes the importance of comments, input validation, and specific coding patterns for common problems like palindromes and matrix operations. Additionally, it includes a quick revision checklist to help reinforce key concepts and syntax accuracy.

Uploaded by

v.viiswinburne
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)
3 views5 pages

Code

The document provides a structured guide for programming in Turbo C, covering essential topics such as loops, arrays, string manipulation, functions, and structures. It emphasizes the importance of comments, input validation, and specific coding patterns for common problems like palindromes and matrix operations. Additionally, it includes a quick revision checklist to help reinforce key concepts and syntax accuracy.

Uploaded by

v.viiswinburne
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

1.

The "Turbo C" Template: Every program must look like this to pass the compiler check and keep
the output clean:

2. Comments: Add // Logic to calculate... above your loops. Examiners give partial marks for
comments even if the code fails.

3. Input Validation: If the question asks for a positive integer, use a loop to ensure n > 0.

--------------------------------------------------------------------------------

MODULE 1: The Logic of Digits (Loops)

Key Concept: Use % 10 to get the last digit and / 10 to remove it. Likely Questions: Palindrome
Number, Armstrong Number, Sum of Digits.

The Master Code (Memorize This): This single logic block solves Armstrong, Reverse, Sum of Digits,
and Palindrome.

int n, r, sum = 0, temp;

printf("Enter number: ");

scanf("%d", &n);

temp = n; // Save original number

while(n > 0) {

r = n % 10; // Extract last digit

sum = sum * 10 + r; // Logic for REVERSE. Change for others:

// For Armstrong: sum = sum + (r*r*r);

// For Sum of Digits: sum = sum + r;

n = n / 10; // Remove last digit

if(temp == sum) printf("Palindrome/Armstrong");

else printf("Not Palindrome/Armstrong");

Note: For Prime Numbers, remember to loop from i=2 to n/2 and check if(n % i == 0).

--------------------------------------------------------------------------------

MODULE 2: Arrays & Matrices (2D Arrays)

Key Concept: Nested loops. The outer loop i controls rows, inner loop j controls columns. Likely
Questions: Matrix Multiplication, Matrix Addition, Transpose.

The "Boss" Problem: Matrix Multiplication If you can memorize Multiplication, you can do
Addition/Subtraction easily.
int a[7], b[7], c[7], i, j, k;

// ... assume input is taken for a and b ...

// Logic for Multiplication

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

for(j=0; j<3; j++) {

c[i][j] = 0; // Initialize result cell

for(k=0; k<3; k++) {

c[i][j] = c[i][j] + a[i][k] * b[k][j]; // The Formula

// Print using nested loops and \t for formatting [8, 9]

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

printf("\n"); // New line for new row

for(j=0; j<3; j++)

printf("%d\t", c[i][j]);

--------------------------------------------------------------------------------

MODULE 3: String Manipulation

Key Concept: Strings end with a null character \0. Likely Questions: Palindrome String, Count
Vowels, Reverse String.

Critical Turbo C Tip: Use gets(str) to read a string with spaces. Do not use scanf("%s", ...) as it stops at
the first space.

The "Palindrome String" Pattern (Manual Method): This is safer than using library functions if the
exam asks for "logic without string.h".

char str;

int len = 0, i, flag = 1;

printf("Enter string: ");

gets(str);

// 1. Find Length manually


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

len++;

// 2. Compare Start vs End

for(i=0; i < len/2; i++) {

if(str[i] != str[len - i - 1]) {

flag = 0; // Mismatch found

break;

if(flag == 1) printf("Palindrome");

else printf("Not Palindrome");

--------------------------------------------------------------------------------

MODULE 4: Functions & Pointers

Key Concept: Call by Reference is the most frequent "hard" question. You must modify variables
in main from inside a function using pointers.

The "Swap Two Numbers" Code:

// Function Definition

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

int temp;

temp = *x; // Get value at address x

*x = *y; // Put value at y into address x

*y = temp; // Put temp into address y

void main() {

int a = 10, b = 20;

clrscr();

swap(&a, &b); // PASS ADDRESSES


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

getch();

Recursion: If asked for Factorial or Fibonacci, use this logic:

int fact(int n) {

if (n == 0) return 1; // Base case

else return n * fact(n-1); // Recursive step

--------------------------------------------------------------------------------

MODULE 5: Structures

Key Concept: Grouping different data types (Name, Roll, Marks). Likely Questions: Student Record
System, Sorting Students by Marks.

The Structure Template:

struct Student {

int roll;

char name[16];

float marks;

};

void main() {

struct Student s[1]; // Array of structures

int i;

// Input

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

printf("Enter Roll, Name, Marks: ");

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

// Display

printf("Roll\tName\tMarks\n");
for(i=0; i<3; i++) {

printf("%d\t%s\t%.2f\n", s[i].roll, s[i].name, s[i].marks);

getch();

--------------------------------------------------------------------------------

QUICK REVISION CHECKLIST (Last Hour)

1. Patterns: Practice the "Pyramid" loop (nested i and j loops where j depends on i).

2. Syntax: Remember to put ; after struct Name { ... };.

3. Arrays: Array indices start at 0, so a loop goes from 0 to n-1.

4. Sorting: Review Bubble Sort logic. It uses nested loops and a temp swap variable (similar to the
Swap logic above).

Focus on syntax accuracy and indenting your code; that usually accounts for 20-30% of the marks.

--- @janakgohil_

You might also like