Programming Homework 3


Example 1

#include <stdio.h>
#include <string.h>
int main()
{
  char a,b,c,d;
  scanf("%c %c\n",&a,&b);
  c = getchar();
  d = getchar();
  printf("I am a: %c\n", a);
  printf("I am b: %c\n", b);
  printf("I am c: %c\n", c);
  printf("I am d: %d\n", d);
  printf("%c%c%c%c\n",a,b,c,d);

}

Example 2

#include <stdio.h>
#include <string.h>
int main()
{
  int a=1,b=2;
  for(;a<8;a++)
  {
    b+=a;
    a+=2;
  }
  printf("%d,%d\n",a,b);
  return 0;
}

Example 3

#include <stdio.h>
#include <string.h>
int main()
{
int i,j,m=1;
  for(i=1;i<3;i++)
  {
    for(j=3;j>0;j--)
    {
      if(i+j>3)
        break;
      m*=i*j;
    }
   }
  printf("m=%d\n",m);

  return 0;
}

Example 4

#include <stdio.h>
#include <string.h>
int main()
{
  int b[3][3]={0,1,2,0,1,2,0,1,2},i,j,t=1;
  for(i=1;i<3;i++)
    for(j=1;j<=1;j++)
      t+=b[i][b[j][i]];
    printf("%d\n",t);

  return 0;
}

Example 5

#include <stdio.h>
#include <string.h>
int main()
{
  char s[100] = "Time is a train, making the future the past";
  int i,j;
  printf("%d",strlen(s));
  for(i=j=0;s[i]!='\0';i++)
  if(s[i]!=' ')
  {
    s[j]=s[i];
    j++;
  }
  s[j]= '\0';
  printf("%s\n",s);

  return 0;
}

Example 7

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[10]="abcd!",s2[]="\n123\\d";
  printf("%s\n",s2);
  printf("%d %d\n",strlen(s1),strlen(s2));

  return 0;
}

Example 8

#define SUB(a) (a)-(a)

#include <stdio.h>
#include <string.h>
int main()
{
  int a=2,b=3,c=5,d;
  d=SUB(a+b)*c;
  printf("%d\n",d);

  return 0;
}

Example 9

#include <stdio.h>
#include <string.h>
int main()
{
  int a=2,b=a,c=2;
  printf("%d\n",a/b&c);

  return 0;
}

Example 10

#include <stdio.h>
#include <string.h>
int main()
{
  int x=276;
  printf("%d-%d-%d\n", x%10, x/10%10, x/100);

  return 0;
}

Example 11

#include <stdio.h>
#include <string.h>
void fun(char *str)
{
  char temp;
  int n,i;
  n=strlen(str);
  temp=str[n-1];
  for(i=n-1;i>0;i--)
    str[i]=str[i-1];
  str[0]=temp;
}

int main()
{
  char s[50];
  scanf("%s",s);
  fun(s);
  printf("%s\n",s);

  return 0;
}

Example 13

#include <stdio.h>
#include <string.h>
int main()
{
  int a=1,b=2,c=3,d=0;
  if(a==1)
  if(b!=2)
    if(c==3) d=1;
    else d=2;
  else
    if(c!=3) d=3;
    else d=4;
  else d=5;
  printf("%d\n",d);

  return 0;
}

Example 14

#include <stdio.h>
#include <string.h>
int main()
{
  int m,n;
  scanf("%d %d",&m,&n);
  while(m!=n) {
    while(m>n)
      m=m-n;
    while(m<n)
      n=n-m;
  }
  printf("%d\n",m);

  return 0;
}

Example 15

#include <stdio.h>
#include <string.h>
int main()
{
  int i,j;
  int a[][3]={1,2,3,4,5,6,7,8,9};
  for(i=1;i<3;i++)
    for(j=1;j<3;j++)
      printf("%d",a[i][j]);
  return 0;
}

Example 16

#include <stdio.h>
#include <string.h>

int f(int t[],int n)
{
  if (n>0)
    return t[n-1]+f(t,n-1);
  else
    return 0;
}


int main()
{
  int a[4]={1,2,3,4},s;
  s=f(a,4);
  printf("%d\n",s);

  return 0;
}

Example 6

#include <stdio.h>
#include <string.h>
int main()
{
  int a[5]={1,2,3,4,5},b[5]={0,2,1,3,0},i,s=0;
  for(i=1;i<3;i++)
    s=s+a[b[i]];
  printf("%d\n",s);
  return 0;
}

Example 12

#include <stdio.h>
#include <string.h>

int main()
{
  int a[3][3]={1,2,3,4,5,6,7,8,9};
  int b[3]={0},i;
  for(i=1;i<3;i++)
    b[i]=a[i][2]+a[2][i];
  for(i=1;i<3;i++)
    printf("%d",b[i]);

  return 0;
}

Joker - Why So Serious?
Published under (CC) BY-NC-SA in categories Tech  tagged with Software