C Programming Lab
C Programming Lab
C Programming Lab
Program
Output
Keywords in C
A list of 32 keywords in the c language is given below:
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:
#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2, sum;
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:
#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:
#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
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;
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 :
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;
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;
//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:
Program:
#include <stdio.h>
#include<conio.h>
void main () {
int 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:
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