using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { string greetingText = "www.kutayzorlu.com/java2s/com"; for (int i = (int)'z'; i >= (int)'a'; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } for (int i = (int)'Z'; i >= (int)'A'; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } Console.WriteLine("Encoded: " + greetingText); } }
Determine if a number is prime. If it is not, then display its largest factor
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Determine if a number is prime. If it is not,
then display its largest factor.
*/
using System;
public class FindPrimes {
public static void Main() {
int num;
int i;
int factor;
bool isprime;
for(num = 2; num < 20; num++) { isprime = true; factor = 0; // see if num is evenly divisible for(i=2; i <= num/2; i++) { if((num % i) == 0) { // num is evenly divisible -- not prime isprime = false; factor = i; } } if(isprime) Console.WriteLine(num + " is prime."); else Console.WriteLine("Largest factor of " + num + " is " + factor); } } } [/csharp]
A negatively running for loop
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A negatively running for loop. using System; public class DecrFor { public static void Main() { int x; for(x = 100; x > -100; x -= 5) Console.WriteLine(x); } }
Determine smallest single-digit factor
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Determine smallest single-digit factor.
using System;
public class Ladder {
public static void Main() {
int num;
for(num = 2; num < 12; num++) { if((num % 2) == 0) Console.WriteLine("Smallest factor of " + num + " is 2."); else if((num % 3) == 0) Console.WriteLine("Smallest factor of " + num + " is 3."); else if((num % 5) == 0) Console.WriteLine("Smallest factor of " + num + " is 5."); else if((num % 7) == 0) Console.WriteLine("Smallest factor of " + num + " is 7."); else Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7."); } } } [/csharp]
The finished C# statement Help system that processes multiple requests
/* C# A Beginner's Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295 */ /* Project 3-3 The finished C# statement Help system that processes multiple requests. */ using System; public class Help3 { public static void Main() { char choice; for(;;) { do { Console.WriteLine("Help on:"); Console.WriteLine(" 1. if"); Console.WriteLine(" 2. switch"); Console.WriteLine(" 3. for"); Console.WriteLine(" 4. while"); Console.WriteLine(" 5. do-while"); Console.WriteLine(" 6. break"); Console.WriteLine(" 7. continue"); Console.WriteLine(" 8. goto "); Console.Write("Choose one (q to quit): "); do { choice = (char) Console.Read(); } while(choice == ' ' | choice == ' '); } while( choice < '1' | choice > '8' & choice != 'q'); if(choice == 'q') break; Console.WriteLine(" "); switch(choice) { case '1': Console.WriteLine("The if: "); Console.WriteLine("if(condition) statement;"); Console.WriteLine("else statement;"); break; case '2': Console.WriteLine("The switch: "); Console.WriteLine("switch(expression) {"); Console.WriteLine(" case constant:"); Console.WriteLine(" statement sequence"); Console.WriteLine(" break;"); Console.WriteLine(" // ..."); Console.WriteLine("}"); break; case '3': Console.WriteLine("The for: "); Console.Write("for(init; condition; iteration)"); Console.WriteLine(" statement;"); break; case '4': Console.WriteLine("The while: "); Console.WriteLine("while(condition) statement;"); break; case '5': Console.WriteLine("The do-while: "); Console.WriteLine("do {"); Console.WriteLine(" statement;"); Console.WriteLine("} while (condition);"); break; case '6': Console.WriteLine("The break: "); Console.WriteLine("break;"); break; case '7': Console.WriteLine("The continue: "); Console.WriteLine("continue;"); break; case '8': Console.WriteLine("The goto: "); Console.WriteLine("goto label;"); break; } Console.WriteLine(); } } }
For loop for array
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
public class Arrays2Chapter_3___Structs__Enums__Arrays_and_Classes
{
static void Main(string[] args)
{
//multidimensional array
int[,] MyIntArray = new int[5, 5];
for (int x = 0, y = 0; x < 5; x++, y++) { MyIntArray[x, y] = 0; } } } } [/csharp]
Simplest for
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_4___Program_Control
{
public class Fors
{
static void Main(string[] args)
{
for (int a = 0; a < 10; a++)
{
Console.WriteLine(a);
}
}
}
}
[/csharp]