f i give input like 1,1,4,5,6,1,2,5,4,1,2, then output
should be like : 1-4, 2-2, 4-2, 5-1, 6-1 which means 1
occurs 4 times, 2 occurs 2 times like so.
Answers were Sorted based on User's Feedback
Answer / shadab
using System;
class abc
{
public static void Main()
{
Console.WriteLine("Enter your number with
comma Like this 1,1,4,5,6,1,2,5,4,1,2 ");
string str=Console.ReadLine();
char []a=str.ToCharArray();
int
ctr1=0,ctr2=0,ctr3=0,ctr4=0,ctr5=0,ctr6=0,ctr7=0,ctr8=0,ctr9
=0,ctr0=0;
for(int i=0;i<a.Length;i=i+2)
{
if(Convert.ToInt32(a[i].ToString())
==1)
{
ctr1++;
}
if(Convert.ToInt32(a[i].ToString())
==2)
{
ctr2++;
}
if(Convert.ToInt32(a[i].ToString())
==3)
{
ctr3++;
}
if(Convert.ToInt32(a[i].ToString())
==4)
{
ctr4++;
}
if(Convert.ToInt32(a[i].ToString())
==5)
{
ctr5++;
}
if(Convert.ToInt32(a[i].ToString())
==6)
{
ctr6++;
}
if(Convert.ToInt32(a[i].ToString())
==7)
{
ctr7++;
}
if(Convert.ToInt32(a[i].ToString())
==8)
{
ctr8++;
}
if(Convert.ToInt32(a[i].ToString())
==9)
{
ctr9++;
}
if(Convert.ToInt32(a[i].ToString())
==0)
{
ctr0++;
}
}
Console.WriteLine("1 Occurs "+ctr1);
Console.WriteLine("2 Occurs "+ctr2);
Console.WriteLine("3 Occurs "+ctr3);
Console.WriteLine("4 Occurs "+ctr4);
Console.WriteLine("5 Occurs "+ctr5);
Console.WriteLine("6 Occurs "+ctr6);
Console.WriteLine("7 Occurs "+ctr7);
Console.WriteLine("8 Occurs "+ctr8);
Console.WriteLine("9 Occurs "+ctr9);
Console.WriteLine("0 Occurs "+ctr0);
}
}
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / nag
int[] dup = {1,1,4,5,6,1,2,5,4,1,2};
int[] kk = new int[10];
foreach(int k in dup)
{
kk[k] = kk[k] + 1;
}
for (int i = 0; i < 10; i++)
{
if (kk[i] != 0)
Console.Write(i.ToString() + " - " + kk
[i].ToString() + ", ");
}
Console.ReadLine();
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / amitabh dubey
In C# 3.0 with Linq here how you will do
int[] intArray = { 1, 1, 4, 5, 6, 1, 2, 5, 4, 1, 2 };
var groups = from g in intArray
orderby g
group g by g;
StringBuilder stringBuilder = new StringBuilder();
foreach (var g in groups)
stringBuilder.Append(g.Key + "-" + g.Count() + ",");
Console.WriteLine(stringBuilder.ToString());
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / ganthi ganesh
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GanthiGanesh
{
class Program
{
static void Main(string[] args)
{
int one = 0, two = 0, thr = 0, fou = 0, fiv = 0,
six = 0, sev = 0, eig = 0, nin = 0, zer = 0;
Console.WriteLine("\nEnter here : ");
string ip = Console.ReadLine();
for (int i = 0; i < ip.Length - 1; )
{
char c1 = Convert.ToChar(ip[i]);
i++;
char c2 = Convert.ToChar(ip[i]);
if ((c1 == '0' || c1 == '1' || c1 == '2' ||
c1 == '3' || c1 == '4' || c1 == '5' || c1 == '6' || c1 ==
'7' || c1 == '8' || c1 == '9') && (c2 == ','))
{
i++;
}
else
{
Console.WriteLine("\nFormat error...
Press enter to exit...");
goto a;
}
}
for (int i = 0; i < ip.Length; i++)
{
char c = Convert.ToChar(ip[i]);
if (c == '1')
one++;
else if (c == '2')
two++;
else if (c == '3')
thr++;
else if (c == '4')
fou++;
else if (c == '5')
fiv++;
else if (c == '6')
six++;
else if (c == '7')
sev++;
else if (c == '8')
eig++;
else if (c == '9')
nin++;
else
zer++;
i++;
}
Console.WriteLine("\n");
if (one != 0)
Console.WriteLine("1-" + one.ToString());
if (two != 0)
Console.WriteLine("2-" + two.ToString());
if (thr != 0)
Console.WriteLine("3-" + thr.ToString());
if (fou != 0)
Console.WriteLine("4-" + fou.ToString());
if (fiv != 0)
Console.WriteLine("5-" + fiv.ToString());
if (six != 0)
Console.WriteLine("6-" + six.ToString());
if (sev != 0)
Console.WriteLine("7-" + sev.ToString());
if (eig != 0)
Console.WriteLine("8-" + eig.ToString());
if (nin != 0)
Console.WriteLine("9-" + nin.ToString());
if (zer != 0)
Console.WriteLine("0-" + zer.ToString());
a:
Console.Read();
}
}
}
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / rajesh kardile
In C# 3.0 with Linq here how you will do
int[] intArray = { 1, 1, 4, 5, 6, 1, 2, 5, 4, 1, 2 };
var groups = from g in intArray
orderby g
group g by g;
StringBuilder stringBuilder = new StringBuilder();
foreach (var g in groups)
stringBuilder.Append(g.Key + "-" + g.Count() + ",");
Console.WriteLine(stringBuilder.ToString());
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / ganthi ganesh
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GanthiGanesh
{
class Program
{
static void Main(string[] args)
{
int one = 0, two = 0, thr = 0, fou = 0, fiv = 0,
six = 0, sev = 0, eig = 0, nin = 0, zer = 0;
Console.WriteLine("\nEnter here : ");
string ip = Console.ReadLine();
for (int i = 0; i < ip.Length - 1; )
{
char c1 = Convert.ToChar(ip[i]);
i++;
char c2 = Convert.ToChar(ip[i]);
if ((c1 == '0' || c1 == '1' || c1 == '2' ||
c1 == '3' || c1 == '4' || c1 == '5' || c1 == '6' || c1 ==
'7' || c1 == '8' || c1 == '9') && (c2 == ','))
{
i++;
}
else
{
Console.WriteLine("\nFormat error...
Press enter to exit...");
goto a;
}
}
for (int i = 0; i < ip.Length; i++)
{
char c = Convert.ToChar(ip[i]);
if (c == '1')
one++;
else if (c == '2')
two++;
else if (c == '3')
thr++;
else if (c == '4')
fou++;
else if (c == '5')
fiv++;
else if (c == '6')
six++;
else if (c == '7')
sev++;
else if (c == '8')
eig++;
else if (c == 9)
nin++;
else
zer++;
i++;
}
Console.WriteLine("\n");
if (one != 0)
Console.WriteLine("1-" + one.ToString());
if (two != 0)
Console.WriteLine("2-" + two.ToString());
if (thr != 0)
Console.WriteLine("3-" + thr.ToString());
if (fou != 0)
Console.WriteLine("4-" + fou.ToString());
if (fiv != 0)
Console.WriteLine("5-" + fiv.ToString());
if (six != 0)
Console.WriteLine("6-" + six.ToString());
if (sev != 0)
Console.WriteLine("7-" + sev.ToString());
if (eig != 0)
Console.WriteLine("8-" + eig.ToString());
if (nin != 0)
Console.WriteLine("9-" + nin.ToString());
if (zer != 0)
Console.WriteLine("0-" + zer.ToString());
a:
Console.Read();
}
}
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / kirubasankar
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
int a[20],t;
clrscr();
for(i=0;i<5;i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
for(j=i+1;j<=5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
cout<<a[i];
}
getch();
}
Is This Answer Correct ? | 0 Yes | 2 No |
What do you understand by the terms datareader object and dataset object?
can we access main() using objects?
Explain the difference between the system.array.copyto() and system.array.clone()?
How will U encapsulate button trigger event into text_box event of Pressing Enter key?
What are All kind of access specifiers for a class and for methods
When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)?
What is c sharp used for?
What?s a multicast delegate?
What is difference between comparable and comparator?
Hello Friends..am Mohammed riyash..final year BCA in Jamal Mohamed College 2009. Trichy. My doubt is while connecting SQL2005 Built in Database withing the Visual Studio 2005 Framework.. I am getting the error.. That " An Error occurred and it may be due to , under the default settings of the Server, the connection cannot be established.." But the code works in MS Access for me.. Both in VB.Net and C#.Net am getting the same error.. Any genius please Message me to 9994558822 or mail me.. riyash.ips@gmail.com
Which class comes after the SortedList class?
3. Use layered architecture for coding. s.no name description 1 abc xxxxxxxxx 2 abc xxxxxxxxx 3 4 5 6 7 8 Select all Clear all Add Delete Name Description Save close