Final Lab Programs CP-2 .Docx
Final Lab Programs CP-2 .Docx
Unit 3
IF STATEMENT
#include<stdio.h>
void main(){
char ch;
printf("\n Press alphabet key or a Number Key:");
scanf("%c", &ch);
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);
}
#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);
OUTPUT:
1.
Enter the number: 5
The factorial of 5 is 120
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.
#include <stdio.h>
int main()
int n;
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 main()
int n;
return 0;
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;
{ scanf("%c", &ch);
input[n]=ch;
ch1 = input[0];
ch2 = input[2];
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
{ i++;
len++;
// Now swap - first, last - second and last but one ...so on
{ 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;
#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
{ i++;
len++;
// Now check - first, last - second and last but one ...so on
if (inpstr[i] != inpstr[len-1-i]) // array of non-null char has len elements, so last is at len-1
break;
if (pali)
else
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;
{ scanf("%c", &ch);
word[n]=ch;
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
printf("%c", word[n]);
return 0;