Print values divisible by 7 using One Dimensional Array in C


This program is written in C programming language and it does the following:

  • It first declares some integer variables i, j, n and an array a
  • The program then prompts the user to enter a limit 'n' which determines the size of an integer array 'a'
  • Then, it prompts the user to enter 'n' values which are stored in the array 'a'
  • Next, it prints the given values by using a for loop to iterate through the array
  • Then it uses another for loop to check each element of the array 'a' and check if the element is divisible by 7 or not by using the modulus operator(%)
  • If the element is divisible by 7 it prints the element using printf statement and increments the value of j
  • Finally, it prints the total number of elements divisible by 7 using printf statement
  • The program ends by returning 0 as the value of the main function.

Source Code

#include<stdio.h>
int main()
{
  int i,j=0,n,a[10];
  printf("\nEnter the Limit:");
  scanf("%d",&n);
  printf("\nEnter the Values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
     printf("\na[%d]=%d",i,a[i]);
  }
  printf("\nNumbers divisible by 7:");
  for(i=0;i<n;i++)
  {
      if(a[i]%7==0)
      {
         printf("\na[%d]=%d",i,a[i]);
         j++;
      }
  }
  printf("\nTotal numbers divisible by 7=%d",j);
  return 0;
}
To download raw file Click Here

Output

Enter the Limit:5
Enter the Values:1
7
28
4
35
Given values are:
a[0]=1
a[1]=7
a[2]=28
a[3]=4
a[4]=35
Numbers divisible by 7:
a[1]=7
a[2]=28
a[4]=35
Total numbers divisible by 7 =3

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C