C Programming Sample Codes

Here are some of my program codes:

Computing Average Grades in C

#include

#define p printf

#define s scanf

main()

{

int a,b,c,d,sum,average;

clrscr();

p("enter 1st grade: ");

s("%d",&a);

p("enter 2nd grade: ");

s("%d",&b);

p("enter 3rd grade: ");

s("%d",&c);

p("enter 4th grade: ");

s("%d",&d);

sum=a+b+c+d;

average=sum/4;

p("Total Grade is: %d",average);

getch();

}

Here is the output:

Enter 1st grade:(users input)

Enter 2nd grade:(users input)

Enter 3rd grade:(users input)

Enter 4th grade:(users input)

Total Grade is (program output)Average

C Programming Sample Codes

In this Program the user will enter a choice if a,b,c or d every choice have a corresponding

Message that will appear

Here are some program codes:

#include

#define p printf

main()

{

char choice;

clrscr();

p("What is your Gender?\n");

p("A.Male\n");

p("B.Female\n");

p("C.Lesbian\n");

p("D.Gay\n");

p("E.Idont know\n");

p("Enter a letter:");

scanf("%c",&choice);

if(choice=='A'||choice=='a')

{

p("your the man!");

}

else if(choice=='B'||choice=='b')

{

p("you are so cute!");

}

else if(choice=='C'||choice=='c')

{

p("ohm");

}

else if(choice=='D'||choice=='d')

{

p("no comment");

}

else

{

p("Invalid choice!");

}

getch();

}

Program Output:

What is your Gender?

A.Male

B.Female

C.Lesbian

D.Gay

E.Idont know

Enter a letter(user input)

(program output corresponding letter)

Try this program and see what output of it:

#include

#include

#define p printf

main()

{

int size,ctr;

char word [15];

clrscr();

p("Enter a word:");

scanf("%s",word);

size=strlen(word);

word[2]=toupper(word[2]);

word[15]=toupper(word[15]);

word[size/2]=toupper(word[size/2]);

p("%s",word);

getch();

}