sample code


SAMPLE CODING OF SOME EXAMPLE



QUESTION 1.    Write a program where you enter a letter but show the next letter what you input.

Problem identification: If you press 'A' then you get 'B' and if you press 1 then you get 2

CODE:

# include <stdio.h>
int main()
{
char Val;
printf("Enter a character\n");
Val = getch();
printf(".................%c",Val + 1);
return 0;
}


OUTPUT:

DISCUSSION:  In this program we are showing that the behavior of getch() function. In this program when we enter a character then we get the next character what we entered. This happend because getch() is a function which is used to accept a character form the user and simply we initialize Val = getch(), that means the user enter a character which is store in the variable 'Val' and we show Val + 1 then increase the value of 'Val' by 1 which is increase the ASCII value of Val as Val is a character type variable.


QUESTION 2.    Write a program where you press a small letter but you get a capital letter and id you press a capital letter the you get a small letter.

Problem identification: If you enter  'a' then you get 'A' and if you enter 'B' then you get 'b'. That means if you enter small letter then you get capital letter and if you enter capital letter then you get small letter.

CODE:
# include <stdio.h>
int main()
{
char Val;
printf("Enter a letter:   \n");
Val = getch();

if(Val>90)
Val = Val - 32;
else
Val = Val + 32;
printf("        %c",Val);
return 0;
}



OUTPUT:


DISCUSSION: This is another example of a program if a user input a small letter then gets the capital letter and if the user press a capital letter then gets the capital letter. ASCII value of small letters is start from 'a' = 97 so if we just subtract 32 from val we get 97 - 32 = 65, the value of capital A = 65.


QUESTION 3.   Write a program where you enter a string and gets the ASCII value of the string except space. Show space where space is.


Problem identification: User enter a string and we want the ASCII value of every characters.

CODE:

# include <stdio.h>
int main()
{
char Val[30];
int i=0;
printf("Enter your staring:   \n");
gets(Val);

while(Val[i]!='\0')
{
if(Val[i]==32)
printf(" ");
else if(Val[i]!='\0')
printf("%d",Val[i]);
i++;

}
return 0;
}


OUTPUT:






DISCUSSION: In this program we are showing the ASCII value of the given string except space between the words. This is a vary simple program where we create a loop which iterate NULL/ \0 times, when Val[i] = NULL then terminate the loop. In this program we represent the character as integer value(%d) and we get this result.




QUESTION 4.   Write a program where we use a statement without using the semicolon(;) or terminate operator.

Problem identification: Display "America" with out using semicolon(;).


CODE:

# include <stdio.h>
int main()
{
if(printf("America"))

return 0;
}

OUTPUT:

DISCUSSION: This example is a very important because every statement should have finish with semicolon but if we use the statement in a if condition the we don't need to do that.




























Translate