Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Thursday, 2 March 2017

Elements of programming-6




If-else if ladder.

Checking more than one else if condition is known as if-else if ladder. First a condition is checked if it is true then a block of statements are executed. If it is false an another condition is checked with the help else if construct. If it also false then another condition is checked. Checking more than one else if construct is known as else if ladder.


Example:

#include <stdio.h>

int main()
{
    int dayno;
    printf("enter day no:");
    scanf("%d",&dayno);
    if(dayno==1)
    {
        printf("sunday");
    }
   else  if(dayno==2)
    {
        printf("monday");
    }
   else if(dayno==3)
    {
        printf("tuesday");
    }
   else if(dayno==4)
    {
        printf("wednesday");
    }
    else if(dayno==5)
    {
        printf("thursday");
    }
   else if(dayno==6)
    {
        printf("friday");
    }
   else if(dayno==7)
    {
        printf("saturday");
    }
    else
    {
        printf("invalid day number");
    }


    return 0;
}

                                       In the above program day number is get as input . the block statements which will be executed is depending upon the variable dayno we entered. If all the condition is false then  else part will be executed.

                                       Thanking you,
                                                                    Muthu karthikeyan,Madurai.
contact:919629329142
email:muthu.vaelai@gmail.com

Wednesday, 1 March 2017

Elements of programming -5.




Nested if statement.

First a condition is checked if it is true then another condition checked. If it is true another condition is checked. If it also true then a block of statement is executed. If the first condition is true, then else part will be executed. we can also check another condition in else part.

Example.

We are going to find big number among three numbers. The program for it.
#include <stdio.h> 
int main()
{
     int a,b,c;
     printf("enter a:");
     scanf("%d",&a);
     printf("enter b");
     scanf("%d",&b);
     printf("enter c");
     scanf("%d",&c);
     if(a>b)
     {
         if(a>c)
         {
             printf("a is big");

         }
         else
         {
             printf("c is big");

         }
     }
     else
     {
         if(b>c)
         {
             printf("b is big");
         }
         else
         {
             printf("c is big");
         }
     }
    return 0;
}
In the above program has been written in c language. It uses the method of nested if statement.  First a is greater than b is executed if it is true then it check for the second condition whether the second condition is true or not false. If it also true a is declared as big number. If the second condition is false then c is declared as big number .if the first condition is false then it comes to else part.  It also checks another condition. If it is true then b is declared as big number. If it is false then c is declared as big number.
                    Will continue
                                                                                Muthu karthikeyan,Madurai.
                                                     contact:919629329142
                                                          Email:muthu.vaelai@gmail.com.                                                                                                                    

Wednesday, 18 February 2015

Strings in c Language-2


A large set of c functions are provided with every c compiler.

Although it is a big list we shall discuss the functions Strlen(),strcpy(),strcat(),strcmp() ,because these are all the most used one.

strlren()
This function Counts the no of characters in a string.

Example

#include<stdio.h>
#include<conio.h>
#include<string.h>
void  main()
{
char name[]=”muthu”;
int l1,l2;
clrscr();
l1=strlen(name);
l2=strlen(“welcome”);
printf(“str=%s string length=%d\n”,name,l1);
printf(“str=%s string length=%d\n”,”welcome”,l2);
getch();
}

Output
Str=muthu  string length=5
Str=welcome,string length=7

Note we have included a header file string.h in the above program . to use a string function. This is a essential one.

strcpy:

This function will copy a string from one variable to another variable.

Example:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[]=”muthu”;
char name2;
clrscr();
strcpy(name2,name1);
printf(“name1=%s\n”,name1);
printf(“name2=%s\n”,name2);
getch();
}

Output:

name1=”muthu”
name2=”muthu”

strcat:

this function will append a string from one variable to another variable.

Example:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30]=”muthu”;
char name[15]=” karthikeyan”;
clrscr();
strcat(name1,name2);
printf(“name1=%s\n”,name1);
printf(“name2=%s\n”,name2);
getch();
}

Output:
name1=muthu karthikeyan
name2=muthu

strcmp:

this function will compare one string with other string to find out whether they are same or not. If two strings are identical strcmp function will return zero. If they are not  it will return the difference between  ASCII values of first non matching character.

Example:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[]=”muthu”;
char name2[]=”karthikeyan”
int i;
i=strcmp(name1,name2);
printf(“%d”,i);
getch();
}

Output:
2
This is the end of 2 part series string in c language:
please all visit my following blogs:







Tuesday, 10 February 2015

Strings in c language-1

In c & cpp language there is no separate data type for string. But we can use string as array of characters.A string constant is a one dimensional array of characters terminated by null (‘\0’) .

For example

Char a[]={‘w’,’e’,’l’,’c’,’o’,’m’,’e’,’\0’};

We know that character data type take only one byte of memory .In string each character take one byte of memory and last character is always null. The terminating null is important because only by null character a string knows that it is the end of the string.

What is the difference between character array and numerical array?. Can elements character array can be accessed as numerical array. The following sample programs will explain it

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
Char name[]=”muthu”;
int i=0;
clrscr();
while(i<=4)
{
printf(“%c”,name[i]);
i++;
}
getch();
}

Output:
Muthu.

In the above program we have initialized a character array and have printed out the elements of this array in while loop.

 The other way of using while loop to print the character array is using the end character null(‘\0’).

Example

#include<stdio.h>
#include<conio.h>
void main()
{
char name[]=”karthikeyan”;
Int i=0;
clrscr();
while (name[i]!=’\0’)
{
printf(“%c”,name[i])
i++;
}
getch();
}

Output:

Karthikeyan.

Another simple way of printing a string.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[]=”karthikeyan”;
clrscr();
printf(“%s”, name);
getch();
}

Output:
karthikeyan

Note that in the previous programs we used ‘ %c’ as format specifier. But in the above program we used ‘%s’ as format specifier.we also use %s in scanf statement.

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf(“enter your name”);
scanf(“%s”,name);
printf(“hai %s”,name)
getch();
}

Output:
Enter your name :
Muthu karthikeyan
hai Muthu

Note in the above program we give the input as a multi worded string “Muthu karthikeyan”.but the output is only muthu. Because scanf() is not capable getting input of multi worded string.The way of getting aroung this limitation is using gets() for getting input puts for getting output.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf(“enter name”);
gets(name)
puts(“hai”);
puts(name);
getch();
}

Output

Enter name

Muthu karthikeyan

Hai Muthu karthikeyan.



The  other thing to note in gets() and puts() is we don’t have to use format specifier. But gets() and puts() can be only used to getting input and printing output of only strings.
to read my blogs in tamil visit: