You are on page 1of 23

Program 1

Exercise on Structure of C program and Simple C program Execution

Structure of the C Program


1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs

Program

/* Sum of two numbers */


#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, sum;
printf("Enter two numbers to be added ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("%d + %d = %d", a, b, sum);
getch();
}

Output

Enter two numbers to be added 25 35


25+35=60
Program 2 Exercise On Keywords and Identifiers

Keywords in C
A list of 32 keywords in the c language is given below:

auto break case char const continue default do


double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while

C Identifiers
program

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int A=20;
printf("Value of a is : %d",a);
printf("\value of A is :%d",A);
getch();
}

Output
Value of a is : 10
Value of A is :20

The above output shows that the values of both the variables, 'a' and 'A' are different.
Therefore, we conclude that the identifiers are case sensitive.
Program 3 Exercise on operators and Expressions

Program:

#include <stdio.h>
#include <conio.h>
Void main()
{
int a, b,c,d;
a = 18;
b = 6;
c = 3;
d=(a+b)*c;
printf(“the value of expression is %d\n:”, d);
d=(a&&b)
printf(“(a&&b) is %d\n”,d);
printf(“the increment of a is %d”,++a);
c+=b;
printf(“c+=a value is %d”,c);
getch();
}

Output:
The value of expression is : 72
The increment of a is : 19
C+=a value is 9
Program 4 Exercise on Constants and Variables
Program:
Constant

#include <stdio.h>
#include <conio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'

int main()
{
printf("Integer Constant: %d\n", val);
printf("Floating point Constant: %f\n", floatVal);
printf("Character Constant: %c\n", charVal);

getch();
}

Output:

Integer Constant: 10
Floating point Constant: 4.500000
Character Constant: G

Variable
#include <stdio.h>
#include<conio.h>
int main()
{
int b = 17, c = 18;
char a1 = 'J';
printf("Character value : %c\n", a1);
printf("Integer value : %d\t%d\n", b, c);
getch();
}
Output:
Character value : J
Integer value : 17 18

Program 5 Exercise on Formatted Input and Output

Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2, sum;

printf("Enter two integers: ");

scanf("%d %d", &num1, &num2);

sum = num1 + num2;

printf("Sum of %d and %d is: %d", num1, num2, sum);


getch();
}

Output:
Enter two integers : 25 35
Sum of 25 and 35 is: 60
Program 6 : Exercise on Input and output of
characters

Program:
Character input

#include <stdio.h>
#include<conio.h>
int main(void)
{
char ch;
printf("Enter any character: ");
ch = getchar();
printf("Entered character: %c\n", ch);
printf("ASCII value: %d\n", ch);
getch();
}

Output
Enter any character: A
Entered character: A
ASCII value: 65

Character Output

#include <stdio.h>
#include<conio.h>
void main
{
char ch;
printf("Enter any character: ");
ch = getchar();
printf("Entered character: ");
putchar(ch);
getch ( );
}
Output
Enter any character: A
Entered character: A

Program 7 : Exercise on simple if statement


Syntax:
if(expression)
{
//code to be executed
}

Program:

#include<stdio.h>
#include<conio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
getch();
}

Output:
Enter a number 4
4 is even number

Program 8 : Exercise on if else statement


Syntax :
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}

Program:

#include <stdio.h>
#include<conio.h>
Void main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
getch();
}

Output:
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote

Program 9 : Exercise on if else ladder statement


Syntax:

if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}

Program :

#include<stdio.h>
#include<conio.h>
Void main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
getch( );
}

Output :

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
Program 10 : Exercise on switch statement
Syntax:
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional */

default : /* Optional */
statement(s);
}

Program:

#include <stdio.h>
#include<conio.h>
int main ( )
{
char grade = 'C';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
printf("good\n" );
break;
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
getch ( );
}

Output:

Well done
Program 11 : Exercise on While statement
Syntax:
while(condition)
{
statements;
variable increment or decrement;
}

Program:
Program to print table for the given number using while loop in C
#include<stdio.h>

#include<conio.h>

void main()

int i=1,number=0,b=9;

printf("Enter a number: ");

scanf("%d",&number);

while(i<=10){

printf("%d \n",(number*i));

i++;

getch( );
}

Output :
Enter a number: 50

50

100

150

200

250

300

350

400

450

500
Program 12 : Exercise on for statement

Syntax:
for(initialization; condition; increment/decrement)
{
statement-block;
}

Program :

*C program to print all natural numbers from 1 to n


#include <stdio.h>
#include<conio.h>
void main()
{
int i, n;
printf("Enter any number: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d : \n", n);

for(i=1; i<=n; i++)


{
printf("%d\n", i);
}
getch( );
}

Output:
Enter any number 5
Natural numbers from 1 to 5 :
1
2
3
4
5
Program 13 : Exercise on do while statement

Syntax:
do {
statement(s);
} while( condition );

Program :
Program to print table for the given number using do while loop
#include<stdio.h>

#include<conio.h>

void main()

int i=1,number=0;

printf("Enter a number: ");

scanf("%d",&number);

do{

printf("%d \n",(number*i));

i++;

}while(i<=10);

getch( );

}
Output:

Enter a number: 5
5
10
15
20
25
30
35

40

45

50
Program 14: Exercise on one dimensional array
Syntax:
data_type array_name[array_size];

program

#include<stdio.h>

#include<conio.h>

void main(){

int i=0;

int marks[5]={20,30,40,50,60};//declaration and initialization of array

//traversal of array

for(i=0;i<5;i++){

printf("%d \n",marks[i]);

getch();

Output:

20 30 40 50 60
Program 14: Exercise on two dimensional array
Syntax:

Data type arrayName [ x ][ y ];

Program:

#include <stdio.h>

#include<conio.h>

void main () {

/* an array with 5 rows and 2 columns*/

int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

int i, j;

/* output each array element's value */

for ( i = 0; i < 5; i++ ) {

for ( j = 0; j < 2; j++ ) {

printf("a[%d][%d] = %d\n", i,j, a[i][j] );

getch( );

}
Output:
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
Program 15: Exercise on Structure

Syntax:

struct MyStructure { // Structure declaration


int myNum; // Member (int variable)
char myLetter; // Member (char variable)
};

program :

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
int main()
{
struct Student s1;
s1.age = 18;
strcpy(s1.name, "Viraaj");
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
getch( );
}
Output :
Name of Student 1: Viraaj
Age of Student 1: 18

You might also like