Write a program that allows to enter 10 integers in an Array, and to calculate the number of occurrences of an element N in this Array. Where N is entered by the user.
******** In Algorithm ********
Algorithm Variables Tab[10],N,nb_occ,i: integers
nb_occ ← 0 for i from 1 to 10 read(Tab[i]) next write("Enter N :") read(N) for i from 1 to 10 if( Tab[i] = N ) then nb_occ ← nb_occ +1 next next write("Number of occurences of ",N," = ",nb_occ) end Result ==> 7 6 3 7 5 6 9 0 7 1 Enter N: 7 Number of occurrences of 7 = 3
******** In C ********
#include <stdio.h> int main(){ int Tab[10],nb_occ=0,i,N; for( i=0; i < 10; i++){ printf("Enter an integer :"); scanf("%d",&Tab[i]); } printf("Enter N :"); scanf("%d",&N); for( i=0; i < 10; i++){ if ( Tab[i] == N ) nb_occ ++ ; } printf("Number of occurences of %d is %d",N,nb_occ); return 0; }
******** In C++ ********
#include <iostream>
using namespace std;
int main(){
int Tab[10],nb_occ=0,i,nb_rech;
for( i=0; i < 10; i++){
cout<<"Enter an integer :";
cin>>Tab[i];
}
cout<<"Entrer N :");
cin>>nb_rech;
for( i=0; i < 10; i++){
if (Tab[i] == nb_rech)
nb_occ ++ ;
}
cout<<"Number of occurences of "<<N<<"is"<<nb_occ;
return 0;
}
******** In JAVA ********
import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
int[] Tab=new int [10];
int nb_occ=0,i,nb_rech;
for( i=0; i < 10; i++){
System.out.print("Enter an integer:");
Tab[i] = sc.nextInt();
}
System.out.print("Entrer Le nombre recherche:");
nb_rech = sc.nextInt();
for( i=0; i < 10; i++){
if ( Tab[i] == nb_rech )
nb_occ ++ ;
}
System.out.print("Number of occurences of "+nb_rech+" est "+nb_occ);
}
}
******** In C# ********
public class Ex27{
public static void Main(string[] args)
{ int nb_occ=0,i,nb_rech;
int [] Tab = new int [10];
for( i=0; i < 10; i++){
Console.Write("Entrer un entier :");
Tab[i]=int.Parse(System.Console.ReadLine());
}
Console.Write("Entrer Le nombre rechercheé:");
nb_rech = int.Parse(System.Console.ReadLine());
for( i=0; i < 10; i++){
if ( Tab[i] == nb_rech )
nb_occ ++ ;
}
Console.Write("Nombre doccurences de "+nb_rech+" est "+nb_occ);
}
}
******** In JAVA ********
import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
int[] Tab=new int [10];
int nb_occ=0,i,nb_rech;
for( i=0; i < 10; i++){
System.out.print("Enter an integer:");
Tab[i] = sc.nextInt();
}
System.out.print("Entrer Le nombre recherche:");
nb_rech = sc.nextInt();
for( i=0; i < 10; i++){
if ( Tab[i] == nb_rech )
nb_occ ++ ;
}
System.out.print("Number of occurences of "+nb_rech+" est "+nb_occ);
}
}
******** In C# ********
public static void Main(string[] args)
{ int nb_occ=0,i,nb_rech;
int [] Tab = new int [10];
for( i=0; i < 10; i++){
Console.Write("Entrer un entier :");
Tab[i]=int.Parse(System.Console.ReadLine());
}
Console.Write("Entrer Le nombre rechercheé:");
nb_rech = int.Parse(System.Console.ReadLine());
for( i=0; i < 10; i++){
if ( Tab[i] == nb_rech )
nb_occ ++ ;
}
Console.Write("Nombre doccurences de "+nb_rech+" est "+nb_occ);
}
}