Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

BAHRIA UNIVERSITY (KARACHI CAMPUS)

ASSGINMENT # 2 (Group Assignment) - FALL 2018

Computer Programming (CSC-112)


Class: BSE 1 A Name: Aakif Iqbal Enrollment No:02-131182-046
Course Instructor: Engr. Adnan ur Rehman
Lab Instructor: Engr. Misbah Perveen Submission Deadline (BSE 1 A): 20 December, 2018
Max Marks: 10

1. Write a program in C# that take string input, and print its number of characters

Solution :

string str;
int NoofCharac;
Console.Write("Enter string: ");
str = Console.ReadLine();
NoofCharac = str.Length;
Console.WriteLine("Total number of characters in the string entered is: {0}",
NoofCharac);
Output:

2. Write a program in C# using do - while loop, that take a number, and increase it 2 by 2, and
ends before 30.

Solution:

int num;
Console.Write("Enter a number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("number entered is:{0}", num);
do
{
num = num + 2;
Console.WriteLine("Number entered increased by 2 is:{0}", num);
} while (num<28);// so it can be ended before number is equal to 30

Output:

3. Write a JAVA program that uses looping to print the following table of values:

N 10*N 100*N 1000*N

1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000
Solution:
int num = 0;
int result1 = 0;
int result2 = 0;
int result3 = 0;
Console.Write("ENTER THE N VALUE : ");
num = Convert.ToInt32(Console.ReadLine());
Console.Write("\nN");
Console.Write("\t\t10*N");
Console.Write("\t\t100*N");
Console.Write("\t\t1000*N");
for (int n = 1; n <= num; n++)
{

result1 = n * 10;
result2 = n * 100;
result3 = n * 1000;
Console.WriteLine("\n\n\n{0}\t\t{1}\t\t{2}\t\t{3}", n, result1, result2,
result3);

}
Output:

4. Write a program in JAVA that take a sentence as input and show the number of "a" used
in the sentence.
Solution:
string sentnce;
int length, count;
Console.Write("Input the Sentence : ");
sentnce = Console.ReadLine();
count = 0;
length = sentnce.Length;
for (int i = 0; i < length; i++)
{

if (sentnce[i] == 'a')
{
count++;
}

}
Console.Write("\nThe total number of A in the string is : {0}\n", count);

Output:
5. Write a C# program that takes two-character strings as input from the user and displays
the number of characters in each string. Then concatenate both strings and displays the
resultant string also find the number of characters in it. Then get a character to search in
the new string and another character for replacement. If the character to be searched is
found in the new string, replace it with the character to be replaced. Display the updated
string.

Solution:
string str1, str2,comstring,newstr;
char schar, repchar;
bool selement;
int NoofCharachter;
Console.Write("Enter string1: ");
str1 = Console.ReadLine();
Console.Write("Enter string2: ");
str2 = Console.ReadLine();
NoofCharachter=str1.Length;
Console.WriteLine("Number of characters in string 1 is: {0}",
NoofCharachter);
NoofCharachter = str2.Length;
Console.WriteLine("Number of characters in string 2 is: {0}",
NoofCharachter);
comstring = (str1+" "+str2);//spacing included to make it meaningful
Console.WriteLine("The string formed by concatenating the two strings is:
{0}", comstring);
NoofCharachter = comstring.Length;
Console.WriteLine("Number of characters in the new string is: {0}",
NoofCharachter);
Console.Write("Enter the charachter you want to find in the string: ");
schar=Convert.ToChar(Console.ReadLine());
Console.Write("Enter the Charachter you want to replace it with: ");
repchar = Convert.ToChar(Console.ReadLine());
selement = comstring.Contains(schar);
if (selement)
{
newstr=comstring.Replace(schar,repchar);
Console.WriteLine("The New string is: {0}", newstr);
}
else
{
Console.WriteLine("The character entered was not found");
}

Output:
6. Write a program that inputs a string, if it is in lower case, reverse the string and converts it
into its uppercase and vice versa.

Solution:
string str1;
char[] arr1;
int b, d;
b= 0;
char ch;
Console.Write("Enter the string: ");
str1 = Console.ReadLine();
b = str1.Length;
arr1 = str1.ToCharArray(0, b); // Converts string into char array.

Console.Write("After converting we now have the string as : ");


for (d = 0; d < b; d++)
{
ch = arr1[d];
if (Char.IsLower(ch))
Console.Write(Char.ToUpper(ch)); // Converts lowercase to uppercase.
else
Console.Write(Char.ToLower(ch)); // Converts uppercase to lowercase.
}
Console.Write("\n\n");

Output:
7. Write a code to take series of numbers as an input in an array from the user. Search any
given number by the user. If the number is found, then return index location of that
number else print “number not found”.

Solution :

int[] array = new int[100];


Console.Write("Enter the number of elements you want to add in the array? ");
string s = Console.ReadLine();
int z = Int32.Parse(s);
Console.WriteLine("======================");
Console.WriteLine("\n Enter the array elements: \n");
for (int i = 0; i < z; i++)
{
string s1 = Console.ReadLine();
array[i] = Int32.Parse(s1);
}
Console.WriteLine("======================");
Console.WriteLine("Enter the element you want to search: \n");
string s3 = Console.ReadLine();
int y = Int32.Parse(s3);
for (int i = 0; i < z; i++)
{
if (array[i] == y)
{
Console.WriteLine("======================");
Console.WriteLine("Element found!");
Console.WriteLine("Element {0} found at location {1}\n", y, i + 1);
//return;
Console.ReadLine();
}
}
Console.WriteLine("The element searched was not found");
Console.ReadLine();

Output:
8. Write a program that asks date of birth from user and return age in days.

Solution:

Console.Write("Enter your birthdate: ");


int a = int.Parse(Console.ReadLine());
Console.Write("Enter your birthmonth: ");
int b = int.Parse(Console.ReadLine());
Console.Write("Enter your birthyear: ");
int c = int.Parse(Console.ReadLine());
int cyear = 2018;
int cmonth = 12;
int cday = 30;
int Year, Month, Days;
Year = (cyear - c) * 365;
Month = (cmonth - b) * 30;
Days = (cday - a);
int ageindays = Year + Month + Days;
Console.WriteLine("age in days{0}: ", ageindays);
double ageinyears = ageindays / 365;
Console.WriteLine("age in years{0};", ageinyears);

Output:

You might also like