Tutorial 1
Tutorial 1
Tutorial – 1
1. Write a C program to print “Hello World” on the output screen.
#include<stdio.h>
#include<conio.h>
void main()
{
//This program is to demonstrate print function
clrscr();
printf(“Hello World”);
getch();
}
Output:
Code:-
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$\n\n\n");
Console.WriteLine("Name: Dhvani J Bhesaniya\n");
Console.WriteLine("DOB: 06/12/2002\n");
Console.WriteLine("Address: 3/2 Gaiyatri nagar , bustand road
Gondal -3360311\n");
Console.WriteLine("City: Gondal\n");
Console.WriteLine("Pincode: 360311\n");
Console.WriteLine("State: Gujarat\n");
Console.WriteLine("Country: India\n");
Console.WriteLine("Email: [email protected]\n");
Console.WriteLine("\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$");
}
}
}
Output:-
Code:-
using System;
namespace dotnet_sample
{
class Program
{
static void Main(string[] args)
{
int n;
n = int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
Console.WriteLine(n + " is an Even nember.");
}
else
{
Output:-
4 : Rearrange the given code to correct the program. The resultant program will
be to input a number and print whether the given number is odd or even.
Given code:-
namespace ConsoleApplication1
{
{
static void Main(string[] args)
{
int x;
Console.WriteLine("Enter Number : ");
x = Convert.ToInt32(str);
Console.WriteLine("Number is Even");
else
Console.Read();
Updated code:-
using System;
namespace ConsoleApplication1
{
class Program{
static void Main(string[] args)
{
int x;
Console.WriteLine("Enter Number : ");
Console.Read();
string str = Console.ReadLine();
x = Convert.ToInt32(str);
if (x % 2 == 0)
Console.WriteLine("Number is Odd");
else
{
Console.WriteLine("Number is Even");
}
}
}
}
Output:-
5 : Write output of the program. Also write comment for each line for the following
code.
Code:-
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n,fact=1;
Console.WriteLine("Enter Number : ");
Console.Read();
string str = Console.ReadLine();
n = Convert.ToInt32(str);
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
Console.WriteLine("Factorial : {0}",fact);
}
}
}
Output:-
9 Write a Program to convert given name in toggle case.
INPUT : JoHn F kEnNedy
OUTPUT: jOhN f KeNneDY
Code:
using System;
class HelloWorld {
static void Main() {
string no,n="",chr="";
Console.WriteLine("Enter name :");
no = Console.ReadLine();
Console.WriteLine(n);
}
}
Output :
10) Write a Program which accepts mobile no as a string from the user and converts the last 5 digits
into X.
Code:
using System;
namespace console2
{
class Program
{
static void Main(string[] args)
{
int n;
String no;
Console.WriteLine("Enter a mobile no :");
no = Console.ReadLine();
no = no.Remove(5, 5) + "XXXXX";
Console.WriteLine(no);
}
}
}
Output :
11 Write a Program which accepts name and gender from the user. Here, gender may have only 1
character, M or F.
Based on the gender prefix the name Mr. & Ms.
->
using System;
class HelloWorld {
static void Main() {
String name;
char a;
if (a == 'M' || a == 'm')
{
name = "Mr. " + name;
}
else
{
name = "Ms." + name;
}
Console.WriteLine("Name - "+name);
Console.WriteLine("Gender - "+a);
}
}
Output:
12 Write a Program which accepts name from the user and prints the same
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
string b;
Console.WriteLine(b);
Console.ReadKey();
}
}
}
Output
using System;
public class ArmstrongExample
{
public static void Main(string[] args)
{
int n1 = 0, n2 = 1, n3, i, number;
Console.Write("Enter the number of elements: ");
number = int.Parse(Console.ReadLine());
Console.Write(n1 + " " + n2 + " ");
for (i = 2; i < number; ++i)
{
n3 = n1 + n2;
Console.Write(n3 + " ");
n1 = n2;
n2 = n3;
}
}
Output
14 ) Write a Program which accepts no from the user and print the same in words.
using System;
public class ArmstrongExample
{
public static void Main(string[] args)
{
int n, num = 0;
while (n != 0)
{
num = (num * 10) + (n % 10);
n /= 10;
}
while (num != 0)
{
switch (num % 10)
{
case 0:
Console.Write("zero ");
break;
case 1:
Console.Write("one ");
break;
case 2:
Console.Write("two ");
break;
case 3:
Console.Write("three ");
break;
case 4:
Console.Write("four ");
}
Console.ReadLine();
}
}
Output
using System;
public class ArmstrongExample
{
public static void Main(string[] args)
{
int n, r, sum = 0, temp;
Console.Write("Enter the Number= ");
n = int.Parse(Console.ReadLine());
temp = n;
while (n > 0)
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;
}
if (temp == sum)
Console.Write("Armstrong Number.");
else
Console.Write("Not Armstrong Number.");
}
}
Output