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

CPDSweek1Final

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

CPDSweek1Final

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

Ex.No.

1 Practice of C programming using statements, expressions,


Date: 20-8-24 decision making and iterative statements

Write a C program

a) To perform addition and subtraction on 2 integers


b) To show the use of bitwise operators
c) To find the size of the integer, float and character
d) To calculate the area of the circle
e) To swap two numbers
f) To calculate students’ result based on three subjects
g) To determine whether a person is eligible to vote
h) To enter a character and then determine whether it is a vowel or not.
i) To print numbers from 1 to n using while loop
j) To print the following pattern using for loop
*
**
***
****
Output:

Flowchart
1a. Write a C program to perform addition and subtraction on 2 integers

AIM: To write a C program to perform addition and subtraction on 2 integers


ALGORITHM:
Step 1: Start
Step 2: Declare variables: int a, b, c, d
Step 3: Print "Enter 2 no's:"
Step 4: Read two integers a and b from the user
Step 5: Calculate sum: c = a + b
Step 6: Calculate difference: d = a - b
Step 7: Print "The sum is : " followed by the value of c
Step 8: Print "The difference is: " followed by the value of d
Step 9: End

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
scanf("%d%d",&a,&b);
c=a+b;
d=a-b;
printf("Sum is %d Difference is %d",c,d);
getch();
}
Output:

Flowchart
1b. Write a C program to show the use of bitwise operators
AIM: To write a C program to show the use of bitwise operators
ALGORITHM:
Step 1: Start
Step 2: Read number num1 & num2
Step 3: Perform bitwise AND(&) and store result in var1
Step 4: Perform bitwise OR(|) and store result in var2
Step 4: Perform bitwise XOR (^)and store result in var3
Step 4: Perform bitwise left shift (<<) and right shift (>>) and store result in var4
Step 5: Print the values of var1,var2,var3 and var4 Step
6: Stop

PROGRAM:
#include<stdio.h>
void main()
{
int a,b;
scanf("Enter Two Numbers :%d%d",&a,&b);
printf("\nThe Answer for AND is %d",a&b);
printf("\nThe Answer for OR is %d",a|b);
printf("\nThe Answer for XOR is %d",a^b);
printf("\nThe Answer for Right Shift is %d",a>>b);
printf("\nThe Answer for Left Shift is %d",a<<b);
getch();
}
Output:

Flowchart
1c. Write a C program to find the size of the integer, float and character

AIM: To write a C program to find the size of the integer, float and character
ALGORITHM:
Step 1: Start
Step 2: Read integer, float and character in variables a ,b and c respectively
Step 3: Find the size of a, b & c using sizeof( ) operator and store it in variables d, e & f
respectively
Step 4: Print the values of d, e and f
Step 5: Stop

PROGRAM:
#include<stdio.h>
void main()
{
int a;
float b;
char c;
scanf("%d%f%s",&a,&b,&c);
printf("Size of Integer is %d",sizeof(a));
printf("Size of Float is %d",sizeof(b));
printf("Size of Character is %d",sizeof(c));
getch();
}
Output:

Flowchart
1d. Write a C program to calculate the area of the circle

AIM: To write a C program to calculate the area of the circle


ALGORITHM:
Step 1: Start
Step 2: Read radius
Step 3: Calculate area = 3.14*radius*radius
Step 4: Print area
Step 5:Stop

PROGRAM:
#include<stdio.h>
void main()
{
float r,area;
scanf("%f",&r);
area=3.14*r*r;
printf("%f",area);
getch();
}
Output:

Flowchart
1e. Write a C program to swap two numbers

AIM: To write a C program to swap two numbers


ALGORITHM:
Step 1: Start
Step 2: Read 2 values in a & b
Step 3: Declare the third variable c
Step 4: Copy value of Var1 to c
Step 5: Copy value of Var2 to Var1
Step 6: Copy value of Temp to Var2
Step 7: Print the values of var1 & var2
Step 8: Stop.

PROGRAM:
#include<stdio.h>
void main()
{
int a,b,c;
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("%d%d",a,b);
getch();
}
Output:

Flowchart
1f. Write a C program to calculate students’ result based on three subjects

AIM: To write a C program to calculate students’ result based on three subjects


ALGORITHM:
Step 1: Start
Step 2: Input the scores or grades for each of the three subjects (e.g., Maths, English,
Science
Step 3: If any one of the scores is below 35 print FAIL else print PASS
Step 4: Calculate the total score by summing up the scores obtained in all three subjects
Step 5: Calculate the average score by dividing the total score by 3 (the number of
subjects)
Step 6: Determine the grade based on the average score
Step 7: Display the average score and the corresponding grade
Step 8: Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main() {
int a,b,c;
float avg;
printf("Enter marks of three subjects :");
scanf("%d%d%d",&a,&b,&c);
if(a>35&&b>35&&c>35) {
printf("Pass");
avg=(a+b+c)/3;
printf("\nThe avg is %f\n",avg);
if(avg>90)
printf("A+");
else if(avg>70)
printf("A");
else if(avg>50)
printf("B");
else
printf("C");
}
else {
printf("Fail");
}
getch();
}
Output:

Flowchart
1g. Write a C program to determine whether a person is eligible to vote

AIM: To write a C program to determine whether a person is eligible to vote

ALGORITHM:
Step 1: Start
Step 2: Define the legal voting age in the country
Step 3: Input the person's age
Step 4: Check if the person is eligible to vote
Step 5: if age >= legal voting age, print ‘eligible’ else print ‘not eligible’ Step
6: Stop

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
printf("Enter age:");
scanf("%d",&age);
if(age>=18)
printf("Eligible to vote");
else
printf("Not Eligible to vote");
getch();
}
Output:

Flowchart
1h. Write a C program to enter a character and then vowel or not

AIM: To write a C program to enter a character and then to determine whether it is a


vowel or not
ALGORITHM:
Step 1: Start
Step 2: Read a character
Step 2: Convert the input character to lowercase to handle both uppercase and
lowercase vowels
Step 3: Check if the input character is a vowel using logical operators
Step 4: If yes print ‘Vowel’ else print ‘Consonants’ Step
5: Stop

PROGRAM:

#include <stdio.h>
int main()
{
char c,d;
printf("Enter the character");
scanf("%c",&c);
d=tolower(c);
if(d=='a'|| d=='e'||d=='i'||d=='o'||d=='u')
printf("\nVowels");
else
printf(“\nConsonants”);
getch();
}
OUTPUT:

Flowchart:
1i. Write a C program to print numbers from 1 to n using while loop
AIM: To write a C program to print numbers from 1 to n using while loop
ALGORITHM:
Step 1: Start
Step 2: Read the value of n from the user
Step 3: Initialize a variable to store the current number (start with 1)
Step 4: Use a while loop to print numbers from 1 to n
Step 5: Stop

PROGRAM:
#include<stdio.h>
void main(){
int n,i=1;
printf(“Enter the number:”);
scanf(“%d”,&n);
while(i<=n){
printf(“%d\t”);
i++;
}
}
OUTPUT:

Flowchart
1j. Write a C program to print the following pattern using for loop
AIM: To write a C program to print the given pattern using for loop
ALGORITHM:
Step 1: Start
Step 2: Input the number of rows (in this case, it's 4 rows)
Step 3: Use a for loop to iterate from 1 to the number of rows
Step 4: Inside the loop, use another for loop to iterate from 1 to the current row number
Step 5: Print an asterisk (*) for each iteration of the inner loop
Step 6: Print a newline character (\n) after each row is complete
Step 7: Stop

PROGRAM:
#include <stdio.h>
int main() {
int r,i,j;
printf("Enter no. of rows:");
scanf("%d",&r);
for(i=0;i<r;i++){
for(j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}

RESULT:
With the help of C Programming language, programs using statements, expressions,
decision making and iterative statements was verified and executed successfully in Turbo
C Compiler

You might also like