Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Introduction to programming in C Programming Assignments July-2026 Swayam

 

Introduction to Programming in C - July 2026

 Please scroll down for latest Programs   ðŸ‘‡

Code compiled and tested successfully!



Week 3: Assignment 3: Question 1

Due date on 2026-08-14, 23:59 IST

Q.

Complete the function `int find_factorial(int k)` to find the factorial of the positive number `k`.


The factorial of a positive integer `k`, denoted by `k!`, is the product of all positive integers less than or equal to `k`.


k! = k × (k − 1) × ... × 1


Input

The first line of input is a positive integer `N`.

The next line contains `N` positive integers.

Output

For each `k` given as input, print `k!`.

Note: The code stub given takes care of reading the input, passing it to the function `find_factorial` and printing the value returned by the function.
You only need to complete the function `find_factorial`, which takes a single positive integer as input and returns its factorial.

Note: You can assume that each input is less than 10.

Sample input

3

4

3

5

Sample Output

24  6  120


Explanation

The first input (3) denotes there are three input's for k.
4! = 1 * 2 * 3 * 4 = 24
3! = 1 * 2 * 3  = 6
4! = 1 * 2 * 3 * 4  *5 = 120


Complete Program

C
#include <stdio.h>

int find_factorial(int k) {
    // --- START OF SOLUTION CODE ---
    int fact = 1;
    for (int i = 1; i <= k; i++) {
        fact *= i;
    }
    return fact;
    // --- END OF SOLUTION CODE ---
}

int main() {
    int n, k;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        printf("%d", find_factorial(k));
        if (i < n - 1) printf(" ");
    }
    
    return 0;
}

Code Snippet to Paste in the Editor

C
    // --- START OF SOLUTION CODE ---
    int fact = 1;
    for (int i = 1; i <= k; i++) {
        fact *= i;
    }
    return fact;
    // --- END OF SOLUTION CODE ---


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

3 4 3 5

24 6 120
24 6 120
-
Test Case 2

1 8

40320
40320
-




Week 3: Assignment 3: Question 2

Due date on 2026-08-14, 23:59 IST

Q.

Complete the function `int parking_fee(int hours)` to compute the parking fee based on the following rules:


- The first 2 hours are charged at ₹20 per hour.

- Every additional hour is charged at ₹30 per hour.

- If `hours` is 0, the parking fee is ₹0.


Input


A single integer `hours`, representing the number of hours a vehicle was parked.


Output


Print the parking fee, calculated according to the formula.

Sample Input

5

Sample Output

130

Explanation

The vehicle is parked for 5 hours:
First 2 hours: 2 × ₹20 = ₹40
Remaining 3 hours: 3 × ₹30 = ₹90
Total parking fee = ₹40 + ₹90 = ₹130.

Sample Input

2

Sample Output

40

Explanation

First 2 hours: 2 × ₹20 = ₹40

Complete Program

C
#include <stdio.h>

int parking_fee(int hours)
{
    // --- START OF SOLUTION CODE ---
    if (hours <= 2) {
        return hours * 20;
    } else {
        return 40 + (hours - 2) * 30;
    }
    // --- END OF SOLUTION CODE ---
}

int main()
{
    int hours;
    scanf("%d", &hours);
    printf("%d", parking_fee(hours));
    return 0;
}

Code Snippet to Paste in the Editor

C
    // --- START OF SOLUTION CODE ---
    if (hours <= 2) {
        return hours * 20;
    } else {
        return 40 + (hours - 2) * 30;
    }
    // --- END OF SOLUTION CODE ---



You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5

130
130
-
Test Case 2

2

40
40
-



Week 3: Assignment 3: Question 3

Due date on 2026-08-14, 23:59 IST

Q.

Caesar Cipher 


Write a C program that takes a single lowercase English letter and a positive integer k as input, and shifts the letter k positions forward in the alphabet using the Caesar cipher technique.


If the shift goes beyond 'z', it should wrap around to the beginning of the alphabet.


You may assume the input is always a valid lowercase letter ('a' to 'z') and 0 ≤ k ≤ 25. 


Input


The first line contains a single lowercase letter.


The second line contains an integer k.


Output


Print the lowercase letter obtained after shifting the input letter forward by k positions.


Sample Input


x

5


Sample Output


c


Explanation


Starting from x, shifting forward by 5 positions gives:


x → y → z → a → b → c


Hence, the output is c.

Sample Input


d

7


Sample Output


k

Explanation

Starting from d, shifting forward by 7 positions gives:


d → e → f → g → h → i → j → k


Hence, the output is k.

 

Complete Program

C
// --- START OF SOLUTION CODE ---
#include <stdio.h>

int main() {
    char ch;
    int k;
    scanf(" %c", &ch);
    scanf("%d", &k);
    printf("%c", (ch - 'a' + k) % 26 + 'a');
    return 0;
}
// --- END OF SOLUTION CODE ---

Code Snippet to Paste in the Editor

C
// --- START OF SOLUTION CODE ---
#include <stdio.h>

int main() {
    char ch;
    int k;
    scanf(" %c", &ch);
    scanf("%d", &k);
    printf("%c", (ch - 'a' + k) % 26 + 'a');
    return 0;
}
// --- END OF SOLUTION CODE ---


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

x 5

c
c
-
Test Case 2

d 7

k
k
-



Week 4: Assignment 4: Question 1

Due date on 2026-08-20, 23:59 IST

Your last recorded submission was on 2026-08-20, 15:42 IST.

Q.

Generic Search Using Pointers


Complete the C function that searches for the first occurrence of key in the array.

int *find(int arr[], int n, int key);

If the key is found, the function should return a pointer to the first occurrence of the key.

If the key is not found, the function should return NULL.

This function will be used by the main() function to perform the below task:

High-Level Description of the Template Program


The provided program performs the following steps:

Reads an integer n. Reads n integers into an array. Reads an integer key.

Calls find(arr, n, key).


If find() returns NULL, prints:   Key not found.


Otherwise, the program uses the returned pointer to print:

1. Determine the index of the first occurrence,

2. Print the value found,

3. Print the index of the value, and

4. Print all elements from the found element to the end of the array.


The code for this part is given to you. 

You only need to implement the find() function correctly.


Sample Input 1

8

3 5 3 2 5 8 2 1

2


Sample Output 1

Value found: 2

Index: 3

Elements from the first occurrence:

2 5 8 2 1

Sample Input 2

6

10 20 30 40 50 60

35

Sample Output 2

Key not found.

Complete Program

C
#include <stdio.h>

// --- START OF SOLUTION CODE ---
int *find(int arr[], int n, int key) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == key) {
            return &arr[i];
        }
    }
    return NULL;
}
// --- END OF SOLUTION CODE ---

int main() {
    int n, key;

    scanf("%d", &n);

    int arr[100];

    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    scanf("%d", &key);

    int *ptr = find(arr, n, key);

    if (ptr == NULL) {
        printf("Key not found.");
    } else {
        printf("Value found: %d\n", *ptr);
        printf("Index: %ld\n", ptr - arr);

        printf("Elements from the first occurrence:\n");

        while (ptr < arr + n) {
            printf("%d ", *ptr);
            ptr++;
        }
    }

    return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

8 3 5 3 2 5 8 2 1 2

Value found: 2\n
Index: 3\n
Elements from the first occurrence:\n
2 5 8 2 1
Value found: 2\n
Index: 3\n
Elements from the first occurrence:\n
2 5 8 2 1 
Presentation Error
Test Case 2

6 10 20 30 40 50 60 35

Key not found.
Key not found.
-
Week 4: Assignment 4: Question 2

Due date on 2026-08-20, 23:59 IST

Your last recorded submission was on 2026-08-20, 15:43 IST.

Q.

Remove Duplicate Elements in Unsorted Array

Write a C program to read n integers into an array and remove all duplicate elements while preserving the order of their first occurrence. Print the resulting array.


Input Format

The first line contains an integer n, the number of elements in the array.

The second line contains n space-separated integers.


Output Format

Print the array after removing duplicate elements.


Constraints

1 ≤ n ≤ 100

Array elements are integers, within range of int data type. 

Important Note: Please ignore the following comment, if it appears (In all assignments). 

Passed after ignoring Presentation Error

It is due to the presence of an extra space or newline, and does not affect your scores in any manner. 


Sample Input

8

3 5 3 2 5 8 2 1

Sample Output 

3 5 2 8 1


Explanation:

The input array is 3 5 3 2 5 8 2 1.
The first two elements, 3 and 5, are kept because it has not appeared before.
The next element is 3, which has already appeared, so it is removed.
The element 2 has not appeared before, so it is kept.
The next element, 5, is a duplicate and is removed.
The element 8 is kept because it appears for the first time.
The next element, 2, is a duplicate and is removed.
Finally, 1 has not appeared before, so it is kept.

Therefore, after removing all duplicate elements while preserving the order of their first occurrence, the resulting array is 3 5 2 8 1.

Complete Program

C
#include <stdio.h>

int main() {
    int n;
    if (scanf("%d", &n) != 1) return 0;
    int arr[100];
    // Read the array elements
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // --- START OF SOLUTION CODE ---
    for (int i = 0; i < n; i++) {
        int isDuplicate = 0;
        for (int j = 0; j < i; j++) {
            if (arr[i] == arr[j]) {
                isDuplicate = 1;
                break;
            }
        }
        if (!isDuplicate) {
            printf("%d ", arr[i]);
        }
    }
    // --- END OF SOLUTION CODE ---

    return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

8 3 5 3 2 5 8 2 1

3 5 2 8 1
3 5 2 8 1 
Presentation Error
Test Case 2

11 4 4 4 5 6 5 7 8 7 9 10

4 5 6 7 8 9 10
4 5 6 7 8 9 10 
Presentation Error


Week 4: Assignment 4: Question 3

Due date on 2026-08-20, 23:59 IST

Your last recorded submission was on 2026-08-20, 15:27 IST.

Q.

Check if Two Strings are Anagrams


Write a C program that determines whether two strings are anagrams of each other.


Two strings are anagrams if they contain exactly the same letters with the same frequencies, but possibly in a different order.


For example:


LISTEN and SILENT are anagrams.

KNEE and KEEN are anagrams.

APPLE and PAPEL are anagrams.

Input


The first line contains an integer n, the size of the strings (number of characters).

The second line contains the first string.

The third line contains the second string.


Output


Print  1 if the two strings are anagrams.

Print  0 otherwise.


Notes: 1 ≤ n ≤ 20

Both strings consist only of uppercase English letters (A–Z).

Both strings the same number of characters.

Sample Input 1

6

LISTEN

SILENT

Sample Output 1

1


Explanation

LISTEN and SILENT are anagrams of each other, each contains the letters {E,I,L,N,S,T} exactly once.


Sample Input 1

3

CAT

CAR

Sample Output 1

0

Explanation

CAT and CAR are not anagrams. CAT doesn't have the letter 'R'.

Complete Program

C
#include <stdio.h>

int main() {
    int size;
    char s1[20], s2[20];
    int isanagram = 1;

    // Reading the input
    if (scanf("%d", &size) != 1) return 0;
    scanf("%s", s1);
    scanf("%s", s2);

    // Flag variable to detect character is present in s2
    int flag;

    // --- START OF SOLUTION CODE ---
    // Loop through each element in s1
    for (int i = 0; i < size; i++) {
        flag = 0; // Set flag to 0
        for (int j = 0; j < size; j++) {
            if (s2[j] == s1[i]) { // s1[i] found in s2
                s2[j] = '0'; // Set s2[j] to '0', so that it's not counted again
                flag = 1;
                break;
            }
        }
        if (flag == 0) { // If match is not found, it's not an anagram
            isanagram = 0;
        }
    }
    // --- END OF SOLUTION CODE ---

    printf("%d", isanagram); // Printing the result
    return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

6 LISTEN SILENT

1
1
-
Test Case 2

3 CAT CAR

0
0
-



Week 5: Assignment 5: Question 1

Due date on 2026-08-27, 23:59 IST

Your last recorded submission was on 2026-08-20, 15:30 IST.

Q.

Collatz Sequence


The Collatz function is defined for a positive integer n as follows.

f(n)={3n+1,if n oddn/2,if n is even

We consider the repeated application of the Collatz function starting with a given integer n, as follows: 

f(n),f(f(n)),f(f(f(n))),


It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260.

For example, if n=7, the sequence is

    f(7) = 22,

    f(f(7)) = f(22) = 11,

    f(11) = 34,

    f(34) = 17,

    f(17) = 52,

    f(52) = 26,

    f(26) = 13,

    f(13) = 40,

    f(40) = 20,

    f(20) = 10,

    f(10) = 5,

    f(5) = 16,

    f(16) = 8,

    f(8) = 4,

    f(4) = 2,

    f(2) = 1.

    

Thus if you start from n=7, you need to apply f 16 times in order to first get 1.


In this question, you will be given a positive number less than 32,000. You have to output how many times f has to be applied repeatedly in order to first reach 1.

Input

A single positive integer n

Output

Print a single integer indicating the number of applications of f required to reach 1.

Sample Input

7

Sample Output

16

Sample Input

20

Sample Output

7

Complete Program

C
#include <stdio.h>

// --- START OF SOLUTION CODE ---
int collatz_repeat(int n) {
    if (n == 1) {
        return 0;
    } else {
        if (n % 2 == 1) {
            return 1 + collatz_repeat(3 * n + 1);
        } else {
            return 1 + collatz_repeat(n / 2);
        }
    }
}
// --- END OF SOLUTION CODE ---

int main() {
    int n;
    if (scanf("%d", &n) != 1) return 0;
    printf("%d", collatz_repeat(n));
    return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

7

16
16
-
Test Case 2

20

7
7
-


Week 5: Assignment 5: Question 2

Due date on 2026-08-27, 23:59 IST

Your last recorded submission was on 2026-08-20, 15:37 IST.

Q.

Generate all binary strings of a given length

Complete the genBinary function in the recursive C program below that generates all binary strings of length N.

Input
A positive integer 0 < N < 10

Output
Print all binary (0/1) sequences of length N, one per line.

The sequences must be printed in natural lexicographic order.

Sample Input

3

Sample Output

000
001
010
011
100
101
110
111

Sample Input

2

Sample Output

00
01
10
11

Complete Program

C
#include <stdio.h>
#include <stdlib.h>

// --- START OF SOLUTION CODE ---
void genBinary(char *s, int i, int N) {
    if (i == N) {
        s[N] = '\0';
        printf("%s\n", s);
        return;
    }
    s[i] = '0';
    genBinary(s, i + 1, N);
    s[i] = '1';
    genBinary(s, i + 1, N);
}
// --- END OF SOLUTION CODE ---

int main(void) {
    char A[8];
    int n;
    if (scanf("%d", &n) != 1) return 0;

    genBinary(A, 0, n);

    return 0;
}


You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

3

000\n
001\n
010\n
011\n
100\n
101\n
110\n
111
000\n
001\n
010\n
011\n
100\n
101\n
110\n
111\n
Presentation Error
Test Case 2

2

00\n
01\n
10\n
11
00\n
01\n
10\n
11\n
Presentation Error


Week 5: Assignment 5: Question 3

Due date on 2026-08-27, 23:59 IST

Your last recorded submission was on 2026-08-20, 15:40 IST.

Q.

Merge Two Sorted Arrays

Write a C program to merge two sorted arrays into a single sorted array.

INPUT

The first line contains two integers N and M, the sizes of the two arrays.

The second line contains N integers in increasing order.

The third line contains M integers in increasing order.

OUTPUT

Print the merged array in increasing order.

SAMPLE INPUT

5 4
1 4 7 10 15
2 3 8 12

SAMPLE OUTPUT

1 2 3 4 7 8 10 12 15

Complete Program

C
#include <stdio.h>

int main() {
    int n, m;
    if (scanf("%d %d", &n, &m) != 2) return 0;
    
    int a[n], b[m], c[n + m];
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (int i = 0; i < m; i++)
        scanf("%d", &b[i]);

    int i = 0, j = 0, k = 0;

    // --- START OF SOLUTION CODE ---
    while (i < n && j < m) {
        if (a[i] <= b[j]) {
            c[k++] = a[i++];
        } else {
            c[k++] = b[j++];
        }
    }
    while (i < n) {
        c[k++] = a[i++];
    }
    while (j < m) {
        c[k++] = b[j++];
    }
    for (int x = 0; x < n + m; x++) {
        printf("%d ", c[x]);
    }
    // --- END OF SOLUTION CODE ---

    return 0;
}
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5 4 1 4 7 10 15 2 3 8 12

1 2 3 4 7 8 10 12 15
1 2 3 4 7 8 10 12 15 
Presentation Error
Test Case 2

4 5 1 3 5 7 2 4 6 8 10

1 2 3 4 5 6 7 8 10
1 2 3 4 5 6 7 8 10 
Presentation Error



Week 6: Assignment 6: Question 1

Due date on 2026-09-03, 23:59 IST

Your last recorded submission was on 2026-08-23, 09:10 IST.

Q.

Check Symmetric Matrix

A square matrix is said to be symmetric if it is equal to its transpose.

In other words, a matrix A of size n x n is symmetric if and only if:

A[i][j]=A[j][i]for all 0i,j<n.


Write a C program to check whether a given square matrix is symmetric or not.

Complete the function

int isSymmetric(int A[][n], int n)

that checks if A is a symmetric matrix and returns 1 if symmetric, 0 otherwise.

Input Format

  • The first line contains an integer n, the size of the square matrix.
  • The next n lines each contain n integers, representing the elements in each row of the matrix.

Output Format

  • Print 1 if the matrix is symmetric.
  • Otherwise, print 0.

Note. You can assume that n < 100.

Sample Input

3
1 2 3
2 4 5
3 5 6

Sample Output

1

Explanation 

The matrix is symmetric.
A[0][1] = A[1][0] = 2, A[0][2] = A[2][0] = 3, A[1][2] = A[2][1] = 5.

Sample Input

4
1 2 3 4
2 5 6 7
3 6 8 9
4 7 0 1


Sample Output

0

Explanation 

A[2][3]=9, but A[3][2] = 0

Complete Program

C
#include <stdio.h>

/* Complete the function 
check if an n x n matrix A is symmetric
Return 1 if symmetric
Return 0 if not symmetric 
*/
int isSymmetric(int n, int A[n][n]) {
    // --- START OF SOLUTION CODE ---
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (A[i][j] != A[j][i]) {
                return 0;
            }
        }
    }
    return 1;
    // --- END OF SOLUTION CODE ---
}

int main() {
    int n;
    
    // Read the size of the square matrix
    if (scanf("%d", &n) != 1) {
        return 0;
    }
    int A[n][n];
    // Read the matrix elements
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &A[i][j]);
        }
    }

    printf("%d", isSymmetric(n, A));

    return 0;
}

Code Snippet to Paste in the Editor

C
// --- START OF SOLUTION CODE ---
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (A[i][j] != A[j][i]) {
                return 0;
            }
        }
    }
    return 1;
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

3 1 2 3 2 4 5 3 5 6

1
1
-
Test Case 2

4 1 2 3 4 2 5 6 7 3 6 8 9 4 7 0 1

0
0
-


Week 6: Assignment 6: Question 2

Due date on 2026-09-03, 23:59 IST

Your last recorded submission was on 2026-08-23, 10:08 IST.

Q.

Sudoku Validator

Write a C program to validate a completed 9 × 9 Sudoku grid using a two-dimensional array.

A Sudoku solution is valid if:

  • Each row contains the digits 1 through 9 exactly once.
  • Each column contains the digits 1 through 9 exactly once.
  • Each 3 × 3 subgrid contains the digits 1 through 9 exactly once.

For example:

5 3 4 | 6 7 8 | 9 1 2

6 7 2 | 1 9 5 | 3 4 8

1 9 8 | 3 4 2 | 5 6 7

------+-------+------

8 5 9 | 7 6 1 | 4 2 3

4 2 6 | 8 5 3 | 7 9 1

7 1 3 | 9 2 4 | 8 5 6

------+-------+------

9 6 1 | 5 3 7 | 2 8 4

2 8 7 | 4 1 9 | 6 3 5

3 4 5 | 2 8 6 | 1 7 9


Input

9 lines, each containing 9 integers (1–9) separated by spaces.

Note: There are n
o row/column separators, dashes, or pipe characters - just raw numbers.

Output

The program should print:

Valid Sudoku

if the grid is valid, and

Invalid Sudoku

otherwise.

Important Note : Output Checking is Case-Sensitive. Please print the output exactly as specified.

Sample Input 1

5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

Sample Output 1

Valid Sudoku

Sample Input 2

5 3 5 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9

Sample Output 2

Invalid Sudoku

Explanation

5 is repeated twice in row 1

Complete Program

C
#include <stdio.h>

int main() {
    int board[9][9];
    int i, j, k, r, c;
    int num;
    // Read the 9x9 grid
    for (i = 0; i < 9; i++) {
        for (j = 0; j < 9; j++) {
            scanf("%d", &board[i][j]);
        }
    }
    /* Complete the code here */
    // --- START OF SOLUTION CODE ---
    int valid = 1;

    // 1. Check Rows
    for (i = 0; i < 9; i++) {
        int seen[10] = {0};
        for (j = 0; j < 9; j++) {
            num = board[i][j];
            if (num < 1 || num > 9 || seen[num]) {
                valid = 0;
                break;
            }
            seen[num] = 1;
        }
        if (!valid) break;
    }

    // 2. Check Columns
    if (valid) {
        for (j = 0; j < 9; j++) {
            int seen[10] = {0};
            for (i = 0; i < 9; i++) {
                num = board[i][j];
                if (num < 1 || num > 9 || seen[num]) {
                    valid = 0;
                    break;
                }
                seen[num] = 1;
            }
            if (!valid) break;
        }
    }

    // 3. Check 3x3 Subgrids
    if (valid) {
        for (r = 0; r < 9; r += 3) {
            for (c = 0; c < 9; c += 3) {
                int seen[10] = {0};
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        num = board[r + i][c + j];
                        if (num < 1 || num > 9 || seen[num]) {
                            valid = 0;
                            break;
                        }
                        seen[num] = 1;
                    }
                    if (!valid) break;
                }
                if (!valid) break;
            }
            if (!valid) break;
        }
    }

    if (valid) {
        printf("Valid Sudoku\n");
    } else {
        printf("Invalid Sudoku\n");
    }
    // --- END OF SOLUTION CODE ---

    return 0;
}

Code Snippet to Paste in the Editor

C
// --- START OF SOLUTION CODE ---
    int valid = 1;

    // 1. Check Rows
    for (i = 0; i < 9; i++) {
        int seen[10] = {0};
        for (j = 0; j < 9; j++) {
            num = board[i][j];
            if (num < 1 || num > 9 || seen[num]) {
                valid = 0;
                break;
            }
            seen[num] = 1;
        }
        if (!valid) break;
    }

    // 2. Check Columns
    if (valid) {
        for (j = 0; j < 9; j++) {
            int seen[10] = {0};
            for (i = 0; i < 9; i++) {
                num = board[i][j];
                if (num < 1 || num > 9 || seen[num]) {
                    valid = 0;
                    break;
                }
                seen[num] = 1;
            }
            if (!valid) break;
        }
    }

    // 3. Check 3x3 Subgrids
    if (valid) {
        for (r = 0; r < 9; r += 3) {
            for (c = 0; c < 9; c += 3) {
                int seen[10] = {0};
                for (i = 0; i < 3; i++) {
                    for (j = 0; j < 3; j++) {
                        num = board[r + i][c + j];
                        if (num < 1 || num > 9 || seen[num]) {
                            valid = 0;
                            break;
                        }
                        seen[num] = 1;
                    }
                    if (!valid) break;
                }
                if (!valid) break;
            }
            if (!valid) break;
        }
    }

    if (valid) {
        printf("Valid Sudoku\n");
    } else {
        printf("Invalid Sudoku\n");
    }
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9

Valid Sudoku
Valid Sudoku\n
Presentation Error
Test Case 2

5 3 5 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9

Invalid Sudoku
Invalid Sudoku\n
Presentation Error



Week 6: Assignment 6: Question 3

Due date on 2026-09-03, 23:59 IST

Your last recorded submission was on 2026-08-23, 10:24 IST.

Q.

Tic-Tac-Toe: Find the Winner

Write a C program to determine the winner of a Tic-Tac-Toe game. The game board is represented using a 3 × 3 two-dimensional character array. Each cell contains either X or O.

Task

Given a completed Tic-Tac-Toe board, determine whether Player X or Player O has won.

There are two players X and O. You are given a 3*3 char matrix in which each cell is an X or an O

A player wins if their symbol occurs in all three cells of:

  • any row,
  • any column,
  • the main diagonal, or
  • the secondary diagonal.

If no player has three symbols in a row, column, or diagonal, there is no winner.


Complete the following function:

char findWinner(char board[3][3]);

The function should examine the given 3 × 3 Tic-Tac-Toe board and:

  • Return 'X' if Player X has three consecutive symbols in a row, column, or diagonal.
  • Return 'O' if Player O has three consecutive symbols in a row, column, or diagonal.
  • Return 'N' if neither player has won.

Input

The input consists of 3 lines, each containing 3 characters separated by spaces. Each character is either X or O.

Output

If Player X wins, print:

Player X wins

If Player O wins, print:

Player O wins

If neither player wins, print:

No winner

Sample Input 1

O X X
X X O
X O O

Sample Output 1

Player X wins

Explanation

The secondary diagonal contains three X symbols, so Player X wins.

Sample Input 2

X O X
X O O
O X X

Sample Output 2

No winner

Sample Input 3

X O X
X O X
O O X

Sample Output 3

Player O wins

Explanation

The second column contains three O symbols, so Player O wins.

Constraints

  • Use a 3 × 3 two-dimensional array to store the board.
  • Use loops to check the rows and columns.
  • The board is guaranteed to contain only X and O.

Complete Program

C
#include <stdio.h>

char findWinner(char board[3][3])
{
    // --- START OF SOLUTION CODE ---
    for (int i = 0; i < 3; i++) {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
            if (board[i][0] == 'X' || board[i][0] == 'O')
                return board[i][0];
        }
    }

    for (int j = 0; j < 3; j++) {
        if (board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
            if (board[0][j] == 'X' || board[0][j] == 'O')
                return board[0][j];
        }
    }

    if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        if (board[0][0] == 'X' || board[0][0] == 'O')
            return board[0][0];
    }

    if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        if (board[0][2] == 'X' || board[0][2] == 'O')
            return board[0][2];
    }

    return 'N';
    // --- END OF SOLUTION CODE ---
}

int main()
{
    char board[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            scanf(" %c", &board[i][j]);
    char winner = findWinner(board);

    if (winner == 'X')
        printf("Player X wins\n");
    else if (winner == 'O')
        printf("Player O wins\n");
    else
        printf("No winner\n");

    return 0;
}

Code Snippet to Paste in the Editor

C
// --- START OF SOLUTION CODE ---
    for (int i = 0; i < 3; i++) {
        if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
            if (board[i][0] == 'X' || board[i][0] == 'O')
                return board[i][0];
        }
    }

    for (int j = 0; j < 3; j++) {
        if (board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
            if (board[0][j] == 'X' || board[0][j] == 'O')
                return board[0][j];
        }
    }

    if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
        if (board[0][0] == 'X' || board[0][0] == 'O')
            return board[0][0];
    }

    if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
        if (board[0][2] == 'X' || board[0][2] == 'O')
            return board[0][2];
    }

    return 'N';
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 3/3 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

O X X X X O X O O

Player X wins
Player X wins
-
Test Case 2

X O X X O O O X X

No winner
No winner
-
Test Case 3

X O X X O X O O X

Player O wins
Player O wins
-



Week 7: Assignment 7: Question 1

Due date on 2026-09-10, 23:59 IST

Q.

3D Vector Operations Using Structures

Write a C program to perform basic operations on two three-dimensional integer vectors A and B using structures. The structure definition and function declarations are given below. Complete the function definitions as required.

Structure Definition

struct Vector3D {
    int x;
    int y;
    int z;
};

The following functions are to be completed:

struct Vector3D add(struct Vector3D a, struct Vector3D b);

struct Vector3D subtract(struct Vector3D a, struct Vector3D b);

int dotProduct(struct Vector3D a, struct Vector3D b);

struct Vector3D crossProduct(struct Vector3D a, struct Vector3D b);

The function add() must return the sum of the two vectors, subtract() must return their difference, dotProduct() must return their dot product, and crossProduct() must return their cross product.

Vector Addition and Subtraction

For two vectors A = (ax, ay, az) and B = (bx, by, bz):

A + B = (ax + bx, ay + by, az + bz)

A - B = (ax - bx, ay - by, az - bz)

For example, if A = (1, 2, 3) and B = (4, 5, 6):

A + B = (1 + 4, 2 + 5, 3 + 6)
      = (5, 7, 9)

A - B = (1 - 4, 2 - 5, 3 - 6)
      = (-3, -3, -3)

Dot Product

The dot product of two vectors produces a single integer. It is calculated by multiplying corresponding components and adding the results:

A · B = (ax * bx) + (ay * by) + (az * bz)

For example, for A = (1, 2, 3) and B = (4, 5, 6):

A · B = (1 × 4) + (2 × 5) + (3 × 6)
      = 4 + 10 + 18
      = 32

Cross Product

The cross product of two 3D vectors produces a new 3D vector. It is given by:

A × B = (
    (ay * bz) - (az * by),
    (az * bx) - (ax * bz),
    (ax * by) - (ay * bx)
)

For example, for A = (1, 2, 3) and B = (4, 5, 6):

A × B = (
    (2 × 6) - (3 × 5),
    (3 × 4) - (1 × 6),
    (1 × 5) - (2 × 4)
)

      = (-3, 6, -3)

Thus, the key difference is that Dot product returns a single integer, while all other operations return a 3D vector.

Input

The first line contains three integers representing the components xy, and z of vector A. The second line contains three integers representing the components of vector B.

Output

Print the results of the four operations in the following order:

  1. The result of A + B.
  2. The result of A - B.
  3. The dot product A · B.
  4. The result of A × B.

For vector results, print the three components separated by spaces.

Sample Input

1 2 3
4 5 6

Sample Output

5 7 9
-3 -3 -3
32
-3 6 -3

Complete Program

If you want to run it locally, here is the full code. Notice that main() is defined first, exactly as in the boilerplate. The function prototypes are declared above main() so that the compiler knows about them when main() calls them.

c
#include <stdio.h>

struct Vector3D {
    int x;
    int y;
    int z;
};

// Function declarations (required because main is defined before the implementations)
struct Vector3D add(struct Vector3D a, struct Vector3D b);
struct Vector3D subtract(struct Vector3D a, struct Vector3D b);
int dotProduct(struct Vector3D a, struct Vector3D b);
struct Vector3D crossProduct(struct Vector3D a, struct Vector3D b);

// --- BOILERPLATE main() (EXACTLY AS GIVEN, DO NOT CHANGE) ---
int main() {
    struct Vector3D a, b;
    struct Vector3D sum, difference, cross;
    int dot;
    scanf("%d %d %d", &a.x, &a.y, &a.z);
    scanf("%d %d %d", &b.x, &b.y, &b.z);
    sum = add(a, b);
    difference = subtract(a, b);
    dot = dotProduct(a, b);
    cross = crossProduct(a, b);
    printf("%d %d %d\n", sum.x, sum.y, sum.z);
    printf("%d %d %d\n", difference.x, difference.y, difference.z);
    printf("%d\n", dot);
    printf("%d %d %d", cross.x, cross.y, cross.z);
    return 0;
}
// --- END BOILERPLATE main() ---

/* Complete the following functions */
// --- START OF SOLUTION CODE (Paste this block into the editor) ---
struct Vector3D add(struct Vector3D a, struct Vector3D b) {
    struct Vector3D result;
    result.x = a.x + b.x;
    result.y = a.y + b.y;
    result.z = a.z + b.z;
    return result;
}

struct Vector3D subtract(struct Vector3D a, struct Vector3D b) {
    struct Vector3D result;
    result.x = a.x - b.x;
    result.y = a.y - b.y;
    result.z = a.z - b.z;
    return result;
}

int dotProduct(struct Vector3D a, struct Vector3D b) {
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

struct Vector3D crossProduct(struct Vector3D a, struct Vector3D b) {
    struct Vector3D result;
    result.x = a.y * b.z - a.z * b.y;
    result.y = a.z * b.x - a.x * b.z;
    result.z = a.x * b.y - a.y * b.x;
    return result;
}
// --- END OF SOLUTION CODE ---

Code Snippet to Paste in the Editor

Copy only the following block and paste it exactly where the comment /* Complete the following functions */ appears in your editor. Do not paste the #includestruct Vector3D, or main() — those are already provided by the platform.

c
// --- START OF SOLUTION CODE ---
struct Vector3D add(struct Vector3D a, struct Vector3D b) {
    struct Vector3D result;
    result.x = a.x + b.x;
    result.y = a.y + b.y;
    result.z = a.z + b.z;
    return result;
}

struct Vector3D subtract(struct Vector3D a, struct Vector3D b) {
    struct Vector3D result;
    result.x = a.x - b.x;
    result.y = a.y - b.y;
    result.z = a.z - b.z;
    return result;
}

int dotProduct(struct Vector3D a, struct Vector3D b) {
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

struct Vector3D crossProduct(struct Vector3D a, struct Vector3D b) {
    struct Vector3D result;
    result.x = a.y * b.z - a.z * b.y;
    result.y = a.z * b.x - a.x * b.z;
    result.z = a.x * b.y - a.y * b.x;
    return result;
}
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

1 2 3 4 5 6

5 7 9\n
-3 -3 -3\n
32\n
-3 6 -3
5 7 9\n
-3 -3 -3\n
32\n
-3 6 -3
-
Test Case 2

2 -3 5 -4 6 1

-2 3 6\n
6 -9 4\n
-21\n
-33 -22 0
-2 3 6\n
6 -9 4\n
-21\n
-33 -22 0
-
Week 7: Assignment 7: Question 2

Due date on 2026-09-10, 23:59 IST

Your last recorded submission was on 2026-08-30, 09:52 IST.

Q.

Student Database with Structures

Write a C program to create and sort a database of students using structures. The template code is provided. You must complete both the struct Student definition and the compare() function.

Each student record contains:

  • Name: a string with no spaces.
  • Physics: an integer in [0, 100].
  • Chemistry: an integer in [0, 100].
  • Mathematics: an integer in [0, 100].

The program uses qsort() to sort the array of students. Complete the compare() function so that students are sorted according to the following rules:

  1. Higher Physics marks come first.
  2. If Physics marks are equal, higher Chemistry marks come first.
  3. If both Physics and Chemistry marks are equal, higher Mathematics marks come first.

The compare() function should return a negative value if a should come before b, a positive value if a should come after b.

Input

The first line contains an integer n (1 ≤ n ≤ 100). The next n lines contain the student's name followed by their Physics, Chemistry, and Mathematics marks.

Output

Print the sorted student database, one student per line, in the following format:

name physics chemistry mathematics

Note:

Ignore the comment "Passed after ignoring Presentation Error".

Constraints

  • 1 ≤ n ≤ 100
  • All marks are integers in [0, 100].
  • Names contain no spaces.
  • All Mathematics marks are distinct.

Sample Input

5
alice 90 85 92
bob 90 88 80
charlie 95 70 78
diana 90 85 99
ed 95 65 81

Sample Output

charlie 95 70 78
ed 95 65 81
bob 90 88 80
diana 90 85 99
alice 90 85 92

Explanation

The students are first ordered by Physics marks in descending order. Among students with the same Physics marks, Chemistry marks are compared. If both Physics and Chemistry marks are equal, Mathematics marks are used as the final criterion, also in descending order.

For example, charlie and ed both have Physics marks of 95. Since charlie has higher Chemistry marks (70 compared with 65), charlie appears before ed.

Complete Program

c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Student {
    // --- START OF SOLUTION CODE ---
    char name[100];
    int physics;
    int chemistry;
    int maths;          // Must match the field name used in main()
    // --- END OF SOLUTION CODE ---
};

int compare(const void *a, const void *b) {
    // --- START OF SOLUTION CODE ---
    const struct Student *s1 = (const struct Student *)a;
    const struct Student *s2 = (const struct Student *)b;

    // Higher Physics marks come first (descending)
    if (s1->physics != s2->physics)
        return s2->physics - s1->physics;

    // If Physics ties, higher Chemistry marks come first (descending)
    if (s1->chemistry != s2->chemistry)
        return s2->chemistry - s1->chemistry;

    // If both tie, higher Maths marks come first (descending)
    return s2->maths - s1->maths;
    // --- END OF SOLUTION CODE ---
}

int main() {
    int n;
    scanf("%d", &n);
    struct Student students[n];

    for (int i = 0; i < n; i++) {
        scanf("%s %d %d %d", students[i].name,
              &students[i].physics,
              &students[i].chemistry,
              &students[i].maths);
    }

    qsort(students, n, sizeof(struct Student), compare);

    for (int i = 0; i < n; i++) {
        printf("%s %d %d %d\n", students[i].name,
               students[i].physics,
               students[i].chemistry,
               students[i].maths);
    }

    return 0;
}

Code Snippet to Paste in the Editor

Copy exactly this block into the editor at the placeholders /* Complete the struct definition here. */ and /* Complete the code here */:

c
// --- START OF SOLUTION CODE ---
    char name[100];
    int physics;
    int chemistry;
    int maths;

int compare(const void *a, const void *b) {
    const struct Student *s1 = (const struct Student *)a;
    const struct Student *s2 = (const struct Student *)b;

    if (s1->physics != s2->physics)
        return s2->physics - s1->physics;

    if (s1->chemistry != s2->chemistry)
        return s2->chemistry - s1->chemistry;

    return s2->maths - s1->maths;
}
// --- END OF SOLUTION CODE ---
You may submit any number of times before the due date. The final submission will be considered for grading.

This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5 alice 90 85 92 bob 90 88 80 charlie 95 70 78 diana 90 85 99 ed 95 65 81

charlie 95 70 78\n
ed 95 65 81\n
bob 90 88 80\n
diana 90 85 99\n
alice 90 85 92
charlie 95 70 78\n
ed 95 65 81\n
bob 90 88 80\n
diana 90 85 99\n
alice 90 85 92\n
Presentation Error
Test Case 2

4 alice 80 70 90 bob 90 60 80 charlie 80 85 70 david 90 75 60

david 90 75 60\n
bob 90 60 80\n
charlie 80 85 70\n
alice 80 70 90
david 90 75 60\n
bob 90 60 80\n
charlie 80 85 70\n
alice 80 70 90\n
Presentation Error
















































No comments:

Post a Comment

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.