💻
NPTEL • Introduction to Programming in C

Study Glance Blog

NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025

C PROGRAMMING • WEEK 4

NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025

NPTEL • Introduction to Programming in C

Week 4 : Assignments - NPTEL >> Introduction to Programming In C - 2025


Week 4 : Assignment 4 - Question 1

Write a C program to search for the first occurrence of given element in an array.

Solution :


#include<stdio.h>
int search(int n,int a[],int k)
{
  int flag=-1;
  for(int i = 0;i < n;i++)
  {  if(a[i]==k){
     flag=i;
    break;
  }}
  return flag;
}
int main()
{
  int n,k,a[20],i,r;
  scanf("%d",&n);
  for(i = 0;i < n;i++)
  {
    scanf("%d",&a[i]);
  }
  scanf("%d",&k);
  r=search(n,a,k);
    printf("%d",r);
}


Week 4 : Assignment 4 - Question 2

Write a C program to search for number of occurrences of given element in an array.

Solution :


#include<stdio.h>
int search(int n,int a[],int k)
{
  int count=0;
  for(int i = 0;i < n;i++)
  {  if(a[i]==k)
     count++;
  }
  return count;
}
int main()
{
  int n,k,a[20],i,r;
  scanf("%d",&n);
  for(i = 0;i < n;i++)
  {
    scanf("%d",&a[i]);
  }
  scanf("%d",&k);
  r=search(n,a,k);
    printf("%d",r);
}


Week 4 : Assignment 4 - Question 3

Given two strings(character arrays), write a C Program to check if one of them is an anagram of the other.

Solution :


#include<stdio.h>
int isanagrams(char str1[], char str2[], int size) {
    int count1[26] = {0};
    int count2[26] = {0};
    int i;
    for (i = 0; i < size; i++) {
        count1[str1[i] - 'A']++;
    }
    for (i = 0; i < size; i++) {
        count2[str2[i] - 'A']++;
    }
    for (i = 0; i < 26; i++) {
        if (count1[i] != count2[i]) {
            return 0;
        }
    }
    return 1;
}

int main() {
    int n,i;
    char str1[21], str2[21];
    scanf("%d", &n);
	scanf("%s",str1);
	scanf("%s",str2);
    printf("%d", isanagrams(str1, str2, n));
    return 0;
}