CPDSweek1Final
CPDSweek1Final
Write a C program
Flowchart
1a. Write a C program to perform addition and subtraction on 2 integers
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
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
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
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
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
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