Home
Diploma Practice C Online People
Certificate Contact Us

Solutions for assignments on arrays and loops

as01.c

#include <stdio.h>

int main()
{
  int nums[5];
  int ii;

  printf("Enter five integer numbers\n");
  for(ii=0; ii<5; ii++)
    scanf("%d",&nums[ii]);

  printf("You have entered the following numbers\n");
  for(ii=0; ii<5; ii++)
    printf("%d\n",nums[ii]);
}

as02.c

#include <stdio.h>

int main()
{
  int nums[5];
  int ii;
  int small,large,total,avg;

  printf("Enter five integer numbers\n");
  //First read 5 numbers from the user 
  for(ii=0; ii<5; ii++)
    scanf("%d",&nums[ii]);

  small = nums[0];
  large = nums[0];
  total = nums[0];

  for(ii=1; ii<5; ii++)
  {
    if(nums[ii] < small)
      small = nums[ii];
    if(nums[ii] > large)
      large = nums[ii];
    total = total + nums[ii];
  }
  avg = total / 5;
  printf("small   = %d\n",small);
  printf("large   = %d\n",large);
  printf("total   = %d\n",total);
  printf("average = %d\n",avg);
}

as03.c

#include <stdio.h>

int main()
{
  char marks[6];
  int ii;

  printf("Enter marks of six subjects\n");
  //First read 6 subject marks. Note the use of %hhd format for 8 bit integers
  for(ii=0; ii<6; ii++)
    scanf("%hhd",&marks[ii]);

  //Break from the for loop, if any subject got less than 35 marks
  for(ii=0; ii<6; ii++)
  {
    if(marks[ii] < 35)
      break;
  }

  //If we completed for loop without break, student has passed else failed
  if(ii==6)
    printf("Student Passed\n");
  else
    printf("Student Failed\n");
}

as04.c

#include <stdio.h>

int main()
{
  char str[128];
  int ii;

  printf("Enter a line of text with multiple words \n");

  scanf("%s",str);
  // scanf("%[^\n]",str);
  // gets(str);
  // fgets(str,128,stdin);

  ii=0;
  while(str[ii])
  {
    ii++;
  }
  printf("Length of the text line: %d\n",ii);
}

as05.c

#include <stdio.h>

int main()
{
  char str[128];
  int ii;
  int spaces=0;
  

  printf("Enter a line of text with both upper and lower case letters\n");
  fgets(str,128,stdin);

  ii=0;
  while(str[ii])
  {
    if(str[ii] == ' ')
      spaces++;
    ii++;
  }
  printf("Number of spaces in the entered text are: %d\n",spaces);
}

as06.c

#include <stdio.h>

int main()
{
  char str[128];
  int ii;
  int capitalCnt=0;

  printf("Enter a line of text with both upper and lower case letters\n");
  fgets(str,128,stdin);

  ii=0;
  while(str[ii])
  {
    if((str[ii] >= 'A') && (str[ii] <= 'Z'))
      capitalCnt++;
    ii++;
  }

  printf("Number of capital letters in the entered text are: %d\n",capitalCnt);
}

as07.c

#include <stdio.h>

int main()
{
  char str[128];
  int ii;
  int wordCnt=0;
  
  printf("Enter a line of text with multiple words\n");
  gets(str);
  //fgets(str,128,stdin);

  ii=0;

  while(1)
  {
    while(str[ii]==' ')
      ii++;

    if(str[ii]==0)
      break;
    wordCnt++;
    while((str[ii]!=' ') && (str[ii]!=0))
     ii++;
    if(str[ii]==0)
      break;
  }
  printf("Number of words in the entered text are: %d\n",wordCnt);
}

as08.c

#include <stdio.h>

int main()
{
  char lineStr[128];
  int ii;

  printf("Enter a line of text with both upper and lower case letters\n");
  fgets(lineStr,128,stdin);
  ii=0;
  while(lineStr[ii])
  {
    if((lineStr[ii] >= 'A') && (lineStr[ii] <= 'Z'))
      lineStr[ii]  += 32;
    else if((lineStr[ii] >= 'a') && (lineStr[ii] <= 'z'))
      lineStr[ii]  -= 32;
    ii++;
  }
  printf("Following is the line after conversion\n");
  printf("%s",lineStr);
}

as09.c

#include <stdio.h>

int main()
{
  char str1[80];
  char str2[80];
  int ii;

  printf("Enter a line of text\n");
  fgets(str1,80,stdin);

  ii=0;
  while(str1[ii])
  {
    str2[ii] = str1[ii];
    ii++;
  }
  str2[ii] = 0;

  printf("Following is the second\n");
  printf("%s",str2);
}

as10.c

#include <stdio.h>

int main()
{
  char str[80];
  char words[5][20];
  int ii,jj,wcnt=0;

  printf("Enter a line of text conntaining exactly 5  words\n");
  gets(str);

  ii=0;

  while(1)
  {
    while(str[ii]==' ')
      ii++;

    if(str[ii]==0)
      break;

    jj=0;
    while((str[ii]!=' ') && (str[ii]!=0))
    {
      words[wcnt][jj] = str[ii];
      ii++;
      jj++;
    }
    words[wcnt][jj] = 0;
    wcnt++;

    if(str[ii]==0)
      break;
  }
  printf("Following are the words\n");
  for(ii=0; ii<5; ii++)
    printf("Word %d is %s\n",ii+1, words[ii]);
}


#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com