9Google AdSense

Switch case examples in C

//1. Chceck input number is 1,2 or 3
#include<stdio.h>

int main()
{

      int n;

      printf("Enter a Number:");
     scanf("%d",&n);

   
     switch( n )
     {
        case 1 : printf( "Your number is 1\n" );
                   break;
        case 2 : printf( "Your number is 2\n" );
                   break;
        case 3 : printf( "Your number is 3\n" );
                   break;
        default  : printf( "Try Again\n" );
                   break;
     }

return 0;
}
==============================================================




===============================================================
//2.Check user input letter is vowel or not
#include<stdio.h>

int main()
{

      char ch;

      printf("Enter the Vowel (In Capital Letter):");
     scanf("%c",&ch);

   
     switch( ch )
     {
        case 'A' : printf( "Your Character Is A\n" );
                   break;
        case 'E' : printf( "Your Character Is E\n" );
                   break;
        case 'I' : printf( "Your Character Is I\n" );
                   break;
        case 'O' : printf( "Your Character Is O\n" );
                   break;
        case 'U' : printf( "Your Character Is U\n" );
                   break;
        default  : printf( "Your Character is Not Vowel.Otherwise Not a Capital Letter\n" );
                   break;
     }

return 0;
}

===============================================================
//3.check week name
#include<stdio.h>

int main()
{

      int day;

      printf("Enter a number:");
     scanf("%d",&day);

     switch (day)
  {
   case 1 : printf("Sunday\n");
            break;
   case 2 : printf("Monday\n");
            break;
   case 3 : printf("Tuesday\n");
            break;
   case 4 : printf("Wednesday\n");
            break;
   case 5 : printf("Thursday\n");
            break;
   case 6 : printf("Friday\n");
            break;
   case 7 : printf("Saturday\n");
            break;
  default : cout << "Not an allowable day number";
            break;
  }

return 0;
}

===============================================================
//check color
#include <stdio.h>

int main() {
    int color = 1;
    printf("Please choose a color(1: red,2: green,3: blue):\n");
    scanf("%d", &color);

    switch (color) {
        case 1:
            printf("you chose red color\n");
            break;
        case 2:
            printf("you chose green color\n");
            break;
        case 3:
            printf("you chose blue color\n");
            break;
        default:
            printf("you did not choose any color\n");
    }
    return 0;
}
=============================================================
//5.check student grade

#include <stdio.h>

main()
{
     int  Grade = 'B';

     switch( Grade )
     {
        case 'A' : printf( "Excellent\n" );
                   break;
        case 'B' : printf( "Good\n" );
                   break;
        case 'C' : printf( "OK\n" );
                   break;
        case 'D' : printf( "Mmmmm....\n" );
                   break;
        case 'F' : printf( "You must do better than this\n" );
                   break;
        default  : printf( "What is your grade anyway?\n" );
                   break;
     }
}

=============================================================

while example in C

#include <stdio.h>
#include <stdlib.h>

//print "Hello" n times

int main()
{
    int number,i=1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("Hello\n");
       i = i+1;
    }

    return 0;
}
==========================



==========================

#include <stdio.h>
#include <stdlib.h>

//print value of a variable n times

int main()
{
    int number,i=1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("value of i is %d\n", i );
       i = i+1;
    }

    return 0;
}


========================

#include <stdio.h>
#include <stdlib.h>

//Calculate sum of n number

int main()
{
    int number,sum=0,i = 1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("%d,",i);
       sum = sum + i;
       i = i+1;
    }

    printf("\nSum :%d\n",sum);

    return 0;
}

==========================

#include <stdio.h>
#include <stdlib.h>

//Calculate sum of n odd number

int main()
{
    int number,sum=0,i = 1;

    scanf("%d",&number);

    while ( i <= number )
    {
       printf("%d,",i);
       sum = sum + i;
       i = i+2;
    }

    printf("\nSum :%d\n",sum);

    return 0;
}

==========================
#include <stdio.h>
#include <stdlib.h>

//Calculate sum of n even number

int main()
{
    int number,sum=0,i = 2;

    scanf("%d",&number);

    printf("Calculate sum of even number between 2 to %d\n",number);

    while ( i <= number )
    {
       printf("%d,",i);
       sum = sum + i;
       i = i+2;
    }

    printf("\nSum :%d\n",sum);

    return 0;
}

If-Else Example

#include <stdio.h>
#include <stdlib.h>

//checking student passed or failed in exam
int main()
{
    int mark;
    scanf("%d",&mark);
    if(mark>=50)
        printf("You are passed");
    else
        printf("You are failed");

    return 0;
}

=====================================
#include <stdio.h>
#include <stdlib.h>

//checking student grade
int main()
{
    int mark;
    scanf("%d",&mark);
    if(mark>=80)
        printf("Grade : A+");
    else if(mark>=70)
        printf("Grade: A");
    else if(mark>=60)
        printf("Grade: A-");
    else if(mark>=50)
        printf("Grade: B");
    else if(mark>=40)
        printf("Grade: B-");
    else
        printf("Grade: Fail");

    return 0;
}

======================================

#include <stdio.h>
#include <stdlib.h>

//check a number is even or odd

int main()
{
    int number;
    scanf("%d",&number);
    if(number == 0)
        printf("Given number is zero");
    else if(number%2==0)
        printf("%d is a even number",number);
    else
        printf("%d is a odd number",number);
    return 0;
}

==========================================



==========================================

#include <stdio.h>
#include <stdlib.h>

//check a number is positive or negative

int main()
{
    int number;
    scanf("%d",&number);
    if(number == 0)
        printf("Given number is zero");
    else if(number>0)
        printf("%d is a positive number",number);
    else
        printf("%d is a negative number",number);
    return 0;
}

===========================================
#include <stdio.h>
#include <stdlib.h>

//voting age check

int main()
{
    int age;
    printf("Enter Your Age :");
    scanf("%d",&age);
    if(age>=18)
    {
         printf("you can vote");
    }
    else
    {
         printf("you are not Eligible for voting");
    }
    return 0;
}

For Loop Example

#include <stdio.h>
#include <stdlib.h>

//print a statement n times using for

int main()
{
    int number,sum=0,i;

    scanf("%d",&number);

    for(i = 1;i<=number;i++)
    {
         printf("Welcome\n");
    }


    return 0;
}

======================================

#include <stdio.h>
#include <stdlib.h>


//print a variable value n times

int main()
{
    int number,sum=0,i;

    scanf("%d",&number);

    for(i = 1;i<=number;i++)
    {
         printf("Value of i :%d\n",i);
    }


    return 0;
}
=======================================



=======================================

#include <stdio.h>
#include <stdlib.h>


//print  loop variable value

int main()
{
    int number,sum=0,i;

    scanf("%d",&number);

    for(i=number;i>=1;i=i-1)
    {
         printf("value of i is %d\n",i);
    }


    return 0;
}

=======================================

#include <stdio.h>
#include <stdlib.h>


//Calculate sum of n number

int main()
{
    int number,sum=0,i;

    scanf("%d",&number);

    for(i = 1;i<=number;i++)
    {
        sum = sum + i;
    }

    printf("Sum :%d\n",sum);

    return 0;
}


=========================================

#include <stdio.h>
#include <stdlib.h>


//Calculate sum of n even number

int main()
{
    int number,sum=0,i;

    scanf("%d",&number);

    for(i = 2;i<=number;i = i+2)
    {

        printf("%d,",i);
        sum = sum + i;
    }

    printf("\nTotal Sum:%d\n",sum);

    return 0;
}

=========================================

#include <stdio.h>
#include <stdlib.h>


//Calculate sum of n odd number

int main()
{
    int number,sum=0,i;

    scanf("%d",&number);

    for(i = 1;i<=number;i = i+2)
    {

        printf("%d,",i);
        sum = sum + i;
    }

    printf("\nTotal Sum:%d\n",sum);

    return 0;
}




Write a program to find the sum of first n natural numbers where n is entered by user (For-Loop)

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int number, sum = 0, i;
    printf("Enter a number: ");
    scanf("%d",&number);

    for(i=1;i<=number;i=i+1)
    {
        printf("%d(sum)",sum);
        sum = sum + i;
        printf(" + %d(i) = %d(sum)\n",i,sum);

    }
    printf("Sum: %d",sum);


    return 0;
}


Output:

check whether a number entered by user is even or odd in C using If-Else

#include <stdio.h>
#include <stdlib.h>

//Write a C program to check whether a number entered by user is even or odd
int main()
{
    int number;
    printf("Enter a number :");
    scanf("%d",&number);

    if(number%2==0)
        printf("%d is a even number",number);
    else
        printf("%d is a odd number",number);


    return 0;
}

Output:



Checking a number is postive or negative in C using If-Else

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;
    //number = 50;

    scanf("%d",&number);

    if(number == 0)
        printf("Given number is %d",number);
    else if(number>0)
        printf("%d is a Positive number",number);
    else
        printf("%d is a Negative number",number);

    return 0;
}

Output:



ACM Problem Solution 10696 - f91

problem

#include<stdio.h>

long int result(long int n)
{
  if(n<=100)
    return(result(result(n+11)));
  else if(n>=101)
    return(n-10);

}
int main()
{
long int n,x;
while(scanf("%ld",&n)==1)
{
    if(n==0)
        break;
    x=result(n);
    printf("f91(%ld) = %ld\n",n,x);

}
return 0;

}

ACM Problem Solution : 10035 Primary Arithmetic


problem
#include <stdio.h>
#include<string.h>>
int main() {
char a[100],b[100];
int lena,lenb,x,y,num1,num2,cry,sum,i,j;
while(scanf("%s%s",&a,&b)==2)
{
    if(a[0]=='0'&&b[0]=='0')
        break;
    lena=strlen(a);
    lenb=strlen(b);
    x=0;
    cry=0;
    for(i=lena-1,j=lenb-1;i>=0||j>=0;j--,i--)
    {
     if(i>=0)
     num1=a[i]-48;
    else num1=0;
    if(j>=0)
     num2=b[j]-48;
     else num2=0;
     sum=num1+num2+x;
        if(sum>9)
        {
        cry++;
        x=sum/10;

        }
        else x=0;
    }
    if(cry==0)
        printf("No carry operation.\n");
    else if(cry==1)
    printf("%d carry operation.\n",cry);

    else
    printf("%d carry operations.\n",cry);

}

return 0;

}

Find topological sort of directed graph


#include<stdio.h>

int vertex,indegree1[50],strtop[50];
int graph[50][50];
void topo(int vertex);

int main()
{


    int i,j,edge,p,x,y,kk,ff,rr;
    printf("Enter the Number of Vertex : ");
    scanf("%d",&vertex);
    printf("\n");
    printf("Enter the Number of Edge : ");
    scanf("%d",&edge);
    printf("\n");
    for(ff=1;ff<=vertex;ff++)
    for(rr=1;rr<=vertex;rr++)
        graph[ff][rr]=0;
    for(i=1;i<=edge;i++)
    {
    scanf("%d%d",&x,&y);
    graph[x][y]=1;

    }
    for(kk=1;kk<=vertex;kk++)
    {    indegree1[kk]=0;
    strtop[kk]=0;
    }
    printf("\n");
    for(i=1;i<=vertex;i++)
      for(j=1;j<=vertex;j++)
          if(graph[j][i]==1)
            indegree1[i]++;
    topo(vertex);
    return 0;
}
void topo(int vertex)
{
    int l,p,xx,node,top=0,d=0,flag=0,pp;
    for(l=1;l<=vertex;l++)
    {
        if(indegree1[l]==0)
        {
              strtop[++top]=l;
              flag=1;
        }

    }
    if(flag==0)
    {    printf("Topological sort is not possible\n");

    }
    else
    {

        while(d!=vertex)
        {
        node=strtop[++d];
        for(xx=1;xx<=vertex;xx++)
            if(graph[node][xx]==1)
            {
                indegree1[xx]=indegree1[xx]-1;
                if(indegree1[xx]==0)
                    strtop[++top]=xx;
            }


        }
        printf("\nAfter topological sort : \n");
        for(pp=1;pp<=vertex;pp++)
            printf("%d ",strtop[pp]);
        printf("\n");

    }

}

ACM Problem Solution : Ordering Tasks 10305

#include<stdio.h>

int vertex,indegree1[110],strtop[110];
int graph[110][110];
void topo(int vertex);

int main()
{


    int i,j,edge,p,x,y,kk,ff,rr,pp;
    while(scanf("%d%d",&vertex,&edge)==2)
    {
    if((vertex==0)&&(edge==0))
        break;
    for(ff=1;ff<=vertex;ff++)
        for(rr=1;rr<=vertex;rr++)
            graph[ff][rr]=0;
    for(i=1;i<=edge;i++)
    {
        scanf("%d%d",&x,&y);
        graph[x][y]=1;
    }
    for(kk=1;kk<=vertex;kk++)
    {    indegree1[kk]=0;
        strtop[kk]=0;
    }
    for(i=1;i<=vertex;i++)
        for(j=1;j<=vertex;j++)
            if(graph[j][i]==1)
                indegree1[i]++;
    topo(vertex);
    for(pp=1;pp<=vertex;pp++)
        printf("%d ",strtop[pp]);
    printf("\n");
    }
    return 0;
}
void topo(int vertex)
{
    int l,p,xx,node,top=0,d=0;
    for(l=1;l<=vertex;l++)
        if(indegree1[l]==0)
              strtop[++top]=l;
    while(d!=vertex)
    {
        node=strtop[++d];
        for(xx=1;xx<=vertex;xx++)
            if(graph[node][xx]==1)
            {
                indegree1[xx]=indegree1[xx]-1;
                if(indegree1[xx]==0)
                    strtop[++top]=xx;
            }


    }


}

Find Indegree and Outdegree of all vertex of a directed graph.

#include<stdio.h>

int main()
{
    int graph[50][50];
    int i,j,edge,vertex,p,x,y,indegree1[90]={0},outdegree1[90]={0},a,q,w;
    printf("Enter the Number of Vertex : ");
    scanf("%d",&vertex);
    printf("\n");
    printf("Enter the Number of Edge : ");
    scanf("%d",&edge);
    printf("\n");
    for(w=1;w<=vertex;w++)
        for(q=1;q<=vertex;q++)
            graph[w][q]=0;
    for(i=1;i<=edge;i++)
    {
    scanf("%d%d",&x,&y);
    graph[x][y]=1;
    }
    printf("\n");
    for(i=1;i<=vertex;i++)
    {
         for(j=1;j<=vertex;j++)
         {
              if(graph[i][j]==1)
              {
                    outdegree1[i]++;
                    indegree1[j]++;
              }
         }

    }
    printf("Indegree\n");
    for(p=1;p<=vertex;p++)
    {
        printf("%d : %d\n",p,indegree1[p]);
    }
    printf("Outdegree\n");
    for(a=1;a<=vertex;a++)
    {
        printf("%d : %d\n",a,outdegree1[a]);
    }

    return 0;
}


Input/Output

 

Adjacency List (Represent Graph on computer)

#include<stdio.h>

int main()
{
    int graph[50][50];
    int i,j,edge,vertex,p,x,y,w,q;
    printf("Enter the Number of Vertex : ");
    scanf("%d",&vertex);
    printf("\n");
    printf("Enter the Number of Edge : ");
    scanf("%d",&edge);
    printf("\n");
 for(w=1;w<=vertex;w++)
        for(q=1;q<=vertex;q++)
            graph[w][q]=0;
    for(i=1;i<=edge;i++)
    {
        scanf("%d%d",&x,&y);
        graph[x][y]=1;
    }
    printf("\n");
    for(i=1;i<=vertex;i++)
    {
         printf("[%d] ",i);
         for(j=1;j<=vertex;j++)
         {
              if(graph[i][j]==1)
                printf("=> %d ",j);
         }
         printf("\n");
    }
    return 0;
}

Adjacency Matrix (Represent Graph on computer)

#include<stdio.h>

int main()
{
    int graph[50][50];
    int i,j,edge,vertex,p,x,y,q,w;
    printf("Enter the Number of Vertex : ");
    scanf("%d",&vertex);
    printf("\n");
 for(w=1;w<=vertex;w++)
        for(q=1;q<=vertex;q++)
            graph[w][q]=0;
    printf("Enter the Number of Edge : ");
    scanf("%d",&edge);
    printf("\n");

    for(i=1;i<=edge;i++)
    {
        scanf("%d%d",&x,&y);
        graph[x][y]=1;
    }

    printf("\n");
    printf("  ");
    for(p=1;p<=vertex;p++)
           printf("%d\t",p);
    printf("\n");
    for(i=1;i<=vertex;i++)
    {
         printf("%d ",i);
         for(j=1;j<=vertex;j++)
         {
             printf("%d\t",graph[i][j]);
         }
         printf("\n");
    }
    return 0;
}

Input
 Output

ACM Problem Solution : Slogan Learning of Princess 12592

Problem
-------------------------------------------------------
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
int main()
{
    int n,i,m,j,k,number;
    char ch;
    string ary[150],slogan;
    cin>>number;
    getchar();
    n=number*2;
    for(i=1;i<=n;i++)
        getline(cin,ary[i]);
    cin>>m;
    getchar();
    for(j=1;j<=m;j++)
    {
        getline(cin,slogan);
        for(k=1;k<=n;k++)
        {
            if(ary[k]==slogan)
            {
                cout<<ary[k+1]<<endl;
                break;
            }
        }
    }
    return 0;
}



ACM Problem Solution : How old are you? - 11219

View Code
View Problem
-------------------------------------------------------------------------------------

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
    int day,month,year,age,t,day1,month1,year1,i;
    char ch;
    scanf("%d",&t);
    i=0;
    while(t--)
    {
        i++;
        scanf("%d%c%d%c%d",&day,&ch,&month,&ch,&year);
        scanf("%d%c%d%c%d",&day1,&ch,&month1,&ch,&year1);
        age=year-year1;
        if((month1>month)||((month==month1)&&(day<day1)))
            age=age-1;
        if(age<0)
            cout<<"Case #"<<i<<":"<<" "<<"Invalid birth date"<<endl;
        else if(age>130)
            cout<<"Case #"<<i<<":"<<" "<<"Check birth date"<<endl;
        else
            cout<<"Case #"<<i<<":"<<" "<<age<<endl;
    }
    return 0;
} 


ACM Problem Solution : Generating Fast - 10098

View Problem
View Code
-------------------------------------------------------------------------------------------------
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

int main()
{
    int t,len;
    char ch;
    char str[10000];
    scanf("%d%c",&t,&ch);
    while(t--)
    {
        gets(str);
        len=strlen(str);
        sort(str,str+len);
        do
        {
            cout<<str<<endl;
        }while(next_permutation(str,str+len));
        cout<<endl;
    }
    return 0;
}

ACM Problem Solution : Pig-Latin - 492

#include<stdio.h>
#include<ctype.h>
int isvowel(char ch)
{
   int flag =0;
   if(ch=='a'|| ch=='A'||ch=='e'|| ch=='E'||ch=='i'|| 
         ch=='I'||ch=='u'|| ch=='U'||ch=='o'|| ch=='O')
       flag = 1;
   return flag;
} 
int main()
{
    char ch,save;
    int n;
    while(1)
    {
        n=scanf("%c",&ch);
        if(n!=1)
            break;
        if(isvowel(ch))
        {
            printf("%c",ch);
            while(1)
        {
                scanf("%c",&ch);
                if(!isalpha(ch))
                    break;
                printf("%c",ch);
            }    
            printf("ay%c",ch);
        }
        else if(isalpha(ch))
        {
              save=ch;
              while(1)
              {
                scanf("%c",&ch);
                if(!isalpha(ch))
                    break;
                printf("%c",ch);
              }
              printf("%cay%c",save,ch);
        }
        else
          printf("%c",ch);
    }
    return 0;
} 

ACM Problem Solution : ID Codes - 146


View Code
view Problem
----------------------------------------------------------------------------
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    char str[200];
    int len;
    while(gets(str)!=NULL)
    {
        if(str[0]=='#')
                 break;
        len=strlen(str);
       if(next_permutation(str,str+len)==1)
              printf("%s\n",str);
       else
             printf("No Successor\n");
   }
  return 0;
}

12554 - A Special "Happy Birthday" Song!!!

View Problem
View Code
---------------------------------------------------------------------
#include<stdio.h>
int main()
{
   int t,tt,k,p,ll,h,hh,se,st;
   char happy[20][100]={"Happy","birthday","to","you","Happy","birthday","to",
       "you","Happy","birthday","to","Rujia","Happy","birthday","to","you"};
   char inp[200][200],ch;
   scanf("%d%c",&t,&ch);
   for(k=0;k<t;k++)
          gets(inp[k]);
   if(t<16)
   {      for(p=0,ll=0;p<16;p++,ll++)
      {
        if(ll==t)
           ll=0;
         printf("%s: %s\n",inp[ll],happy[p]);
      }   }
   else
   {
      for(h=0,hh=0;h<t;h++,hh++)
      {       if(hh>15)
        hh=0;
        printf("%s: %s\n",inp[h],happy[hh]);
      }
      if(hh!=0)
      {
         for(st=hh,se=0;st<16;st++,se++)
         printf("%s: %s\n",inp[se],happy[st]);
      }
   }
   return 0;
}