0% found this document useful (0 votes)
6 views

Final Lab Programs CP-2 .Docx

The document provides a series of C programming exercises focusing on control structures such as IF statements, IF-ELSE statements, SWITCH statements, and looping constructs. It includes example programs for various tasks like checking character types, calculating areas, determining factorials, and manipulating strings. Each program is accompanied by sample outputs demonstrating their functionality.

Uploaded by

madhush27.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Final Lab Programs CP-2 .Docx

The document provides a series of C programming exercises focusing on control structures such as IF statements, IF-ELSE statements, SWITCH statements, and looping constructs. It includes example programs for various tasks like checking character types, calculating areas, determining factorials, and manipulating strings. Each program is accompanied by sample outputs demonstrating their functionality.

Uploaded by

madhush27.m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NitteMeenakshi Institute of Technology

(AN AUTONOMOUS INSTITUTION AFFILIATED TO


VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELGAUM)
PB No. 6429, Yelahanka, Bangalore 560-064, Karnataka
Telephone: 080- 22167800, 22167860, Fax: 080 - 22167805

Unit 3
IF STATEMENT

PROGRAM 1: Write a C program to print whether the key pressed is alphabet or a


number

#include<stdio.h>
void main(){
char ch;
printf("\n Press alphabet key or a Number Key:");
scanf("%c", &ch);

if(ch>='a' && ch<='z')


printf("\nUser entered %c is a lower case alphabet\n", ch);

if(ch>='A' && ch<='Z')


printf("\nUser entered %c is a upper case alphabet\n", ch);

if(ch>='1' && ch<='9')


printf("\nUser entered %c is a Number\n", ch);
}
Output:

Press alphabet key or a Number Key: g


User entered g is a lower case alphabet

Press alphabet key or a Number Key: P


User entered P is a upper case alphabet

Press a alphabet key or a Number Key: 8


User entered 8 is a Number

IF-ELSE STATEMENT
PROGRAM 2:
Write a C Program to find the area of a given shape
#include<stdio.h>
void main(){
int r, l, b;
char ch;
float area;
printf("\n Enter C for circle or R for rectangle or S for Square\n");
scanf("%c", &ch);

if(ch=='C'){
printf("\n Enter radius of circle:");
scanf("%d", &r);
area= 3.14*r*r;
printf("\n Area of a circle is %f", area);
}

else if (ch =='R'){


printf("\n enter length of rectangle:");
scanf("%d", &l);
printf("\n enter breadth of rectangle:");
scanf("%d", &b);
area= l*b;
printf("\n Area of a rectangle is %f", area);
}

else if (ch =='S'){


printf("\n enter side of square:");
scanf("%d", &l);
area= l*l;
printf("\n Area of a rectangle is %f", area);
}

else printf("\nenter a valid option\n");


}
OUTPUT:
1. Enter C for circle or R for rectangle or S for Square: C
Enter radius of circle:2
Area of a circle is 12.56
2. Enter C for circle or R for rectangle or S for Square: R
enter length of rectangle:2
enter breadth of rectangle:3
Area of a rectangle is 6.0000
SWITCH STATEMENTS
PROGRAM 3:
Write a C program to enter a number from 1-7 and display the corresponding day of the
week

#include<stdio.h>
void main(){
int day;
printf("\n Enter any number from 1 to 7:");
scanf("%d", &day);
switch(day)
{
case 1: printf("\nSunday");
break;
case 2: printf("\nMonday");
break;
case 3: printf("\nTuesday");
break;
case 4: printf("\nWednesday");
break;
case 5: printf("\nThursday");
break;
case 6: printf("\nFriday");
break;
case 7: printf("\nSaturday");
break;
default: printf("\n Number not in range");
break;
}
}

OUTPUT:
Enter any number from 1 to 7: 7
Saturday
Enter any number from 1 to 7:4
Wednesday

DO-WHILE STATEMENT
PROGRAM 4:
Write C Program to calculate the factorial of a number( 5!=5*4*3*2*1) using a do-while
loop.
#include<stdio.h>
void main()
{
int n,i=1,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);
//loop to calculate the factorial of a number
do
{
f=f*i;
i++;
}while(i<=n);

printf("\n The Factorial of %d is %d" ,n,f);


}

OUTPUT:
1.
Enter the number: 5
The factorial of 5 is 120

2. Enter the number: 6


The factorial of 5 is 720

LOOPING CONSTRUCTS (for, while, do-while)

PROGRAM 5:
Write a program that will calculate the sum of every third integer ,
beginning with i=2(i.e, 2+5+8+11+...) for all values of i that are less than 100.
Write in three different ways.

//C program using a do-while loop


#include<stdio.h>
void main(){
int i=2;
int sum =0;
do{
sum += i; //(sum = sum +i)
i=i+3;
}while(i<100);
printf("\n sum of n natural numbers: %d\n\n", sum);
}

//C program using a while loop


#include<stdio.h>
void main(){
int i=2;
int sum =0;
while(i<100){
sum += i; //(sum = sum +i)
i+=3; //i=i+3
}
printf("\n sum of n natural numbers: %d\n\n", sum);
}

//C Program using a for loop


#include<stdio.h>
void main(){
int i;
int sum =0;
for(i=2; i<100; i=i+3){
sum += i; //(sum = sum +i)
}
printf("\n sum of n natural numbers: %d\n\n", sum);
}

Prob 6 WAP to find largest and smallest numbers in given N numbers

#include <stdio.h>

int a[10] = {22,14,28,25,27,21,18,25,26,16};

int main()

int smallest = a[0]; // initial value

int largest = a[0]; // initial value

int n;

for (n=0; n < 10; n++)

{ if (a[n] < smallest ) smallest = a[n];


if (a[n] > largest) largest = a[n];

printf ("Smallest number = %d, Largest number = %d", smallest, largest);

return 0;

Prob 7 WAP to store marks scored by N students in a subject and find number of students who
scored higher than or equal to 12 ( Max score 30)

#include <stdio.h>

int marks[10] = {22,12,28,25,27,21,12,25,26,16}; // N is 10

int main()

{ int count =0;

int n;

for (n=0; n < 10; n++)

if (marks[n] >= 12) count++;

printf ("No of students with score 12 and above is %d", count);

return 0;

Prob 8 WAP to read e-m (three characters) as character array

Expand e-m and print table of all characters and corresponding ASCII values

#include <stdio.h>

int main()

{ char input[3];
int n=0;

char ch,ch1,ch2;

//Read three characters

for (n=0; n < 3; n++)

{ scanf("%c", &ch);

input[n]=ch;

//three characters in array, Expand and print

ch1 = input[0];

ch2 = input[2];

while ( ch1 <= ch2)

printf("%c %d\n", ch1, ch1); // Print charcter and ASCII value

ch1++;

return 0;

Prob 9 WAP to reverse a string in place (second string should not be used)

#include <stdio.h>

int main()

char inpstr[30];

int len=0;
int i = 0;

char temp;

scanf("%s", inpstr); // note & not required since in C array name is an address

// Find length - number of non-null characters

while (inpstr[i] != '\0')

{ i++;

len++;

// Now swap - first, last - second and last but one ...so on

for (i=0; i < len/2 ; i++)

{ temp = inpstr[i];

inpstr[i] = inpstr[len-1-i]; // array of non-null char has len elements, so last is at len-1

inpstr[len-1-i] = temp;

printf("%s", inpstr);

return 0;

Prob 10. WAP to Check if given string is a palindrome or not

#include <stdio.h>

int main()

char inpstr[30];

int len=0;

int i = 0;
int pali = 1; // pali is a flag, assume input string is palindrome

scanf("%s", inpstr); // note & not required since in C array name is an address

// Find length - number of non-null characters

while (inpstr[i] != '\0')

{ i++;

len++;

// Now check - first, last - second and last but one ...so on

for (i=0; i < len/2 ; i++)

if (inpstr[i] != inpstr[len-1-i]) // array of non-null char has len elements, so last is at len-1

{pali = 0; // our assumption wrong

break;

if (pali)

printf("input string is a palindrome");

else

printf("inpput string is not palindrome");

return 0;

Prob 11. WAP to read first word (5 characters) of a sentence as character array

If first character is not upper case , make it upper case and print word

#include <stdio.h>

int main()
{ char word[5];

int n=0;

char ch;

//Read five characters

for (n=0; n < 5; n++)

{ scanf("%c", &ch);

word[n]=ch;

// Check first charcter

if (word[0] >= 'a' && word[0] <= 'z') // lower case Check

word[0] = word[0] - ('a' - 'A'); // lower case ASCII value > aupper case ASCCI by a fixed
number

for (n=0; n<5; n++)

printf("%c", word[n]);

return 0;

You might also like