0% found this document useful (0 votes)
2 views42 pages

cp

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 42

Q.1. WAP in C that asks user for Name, Age and then display a welcome message.

Use scanf for input


and printf for output

#include<stdio.h>

int main (){

char name[50];

int age;

printf("Enter your name:");

scanf("%s",name);

printf("Enter your age:");

scanf("%d",&age);

printf("Welcome,%s!You are %d years old.\n",name,age);

return 0;

}
Q.2. WAP in C to perform all basic arithmetic operations based on the input from the user.

#include<stdio.h>

void main()

int num1,num2;

int sum,diff,mul,div,mod,quo;

printf("Enter first number:");

scanf("%d",&num1);

printf("Enter second number;");

scanf("%d",&num2);

sum=num1+num2;

diff=num1-num2;

mul=num1*num2;

mod=num1%num2;

quo=num1/num2;

printf("Sum=%d\n",sum);

printf("Difference=%\n",diff);

printf("Multiply=%d\n",mul);

printf("Modulus=%d\n",mod);

printf("Quotient=%d\n",quo);

}
Q.3. Write a program to check if a number is positive , negative or zero in C.

#include<stdio.h>

void main()

int num;

printf("Input a Number:");

scanf("%d",&num);

if(num >= 0)

printf("%d is a positive Number \n",num);

else

printf("%d is a negative Number \n",num);

}
Q.5. WAP in C to perform bit-level operations using all 6 bitwise operators.

#include<stdio.h>

int main(){

int num1,num2;

printf("Enter first integer: ");

scanf("%d", &num1);

printf("Enter second integer: ");

scanf("%d", &num2);

printf("Bitwise AND (&) of %d and %d = %d\n", num1, num2, num1 & num2);

printf("Bitwise OR (|) of %d and %d = %d\n", num1, num2, num1 | num2);

printf("Bitwise XOR (^) of %d and %d = %d\n", num1, num2, num1 ^ num2);

printf("Bitwise NOT (~) of %d = %d\n", num1, ~num1);

printf("Bitwise NOT (~) of %d = %d\n", num2, ~num2);

printf("Left Shift (<<) of %d by 1 = %d\n", num1, num1 << 1);

printf("Left Shift (<<) of %d by 1 = %d\n", num2, num2 << 1);

printf("Right Shift (>>) of %d by 1 = %d\n", num1, num1 >> 1);

printf("Right Shift (>>) of %d by 1 = %d\n", num2, num2 >> 1);

return 0;

}
Q.6. WAP in C to print numbers from 1 to 15 using while loop and 15 to 30 using Do- While loop.

#include <stdio.h>

int main() {

int i = 1;

printf("Numbers from 1 to 15:\n");

while (i <= 15) {

printf("%d ", i);

i++;

printf("\n");

i = 15;

printf("Numbers from 15 to 30:\n");

do{

printf("%d ", i);

i++;

} while (i <= 30);

printf("\n");

return 0;

}
Q.7. WAP in C to swap two integer numbers using a third variable and also without using a third
variable.

#include <stdio.h>

int main() {

int a, b, temp;

printf("Enter the first number (a): ");

scanf("%d", &a);

printf("Enter the second number (b): ");

scanf("%d", &b);

printf("\nSwapping using a third variable:\n");

printf("Before swap: a = %d, b = %d\n", a, b);

temp = a;

a = b;

b = temp;

printf("After swap: a = %d, b = %d\n", a, b);

printf("\nResetting values for the second swap method.\n");

printf("Enter the first number (a): ");

scanf("%d", &a);

printf("Enter the second number (b): ");

scanf("%d", &b);

printf("\nSwapping without using a third variable:\n");

printf("Before swap: a = %d, b = %d\n", a, b);

a = a + b;

b = a - b;

a = a - b;

printf("After swap: a = %d, b = %d\n", a, b);


return 0;

}
Q.8 Write a program to check whether the given year is leap year or not.

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 400 == 0) {

printf("%d is a leap year.", year);

else if (year % 100 == 0) {

printf("%d is not a leap year.", year);

else if (year % 4 == 0) {

printf("%d is a leap year.", year);

else {

printf("%d is not a leap year.", year);

return 0;

}
Q.9. WAP in C to display the recommended actions depending on the colour of a traffic light. If
colour is red then action is stop. If colour is green action is go. If colour is orange action is caution.
Using Else if ladder.

#include <stdio.h>

int main() {

char signal;

printf("Enter the traffic light signal (R/Y/G): ");

scanf(" %c", &signal);

if (signal == 'R') {

printf("Stop! It's red signal.");

} else if (signal == 'Y') {

printf("Get ready to move. It's yellow signal.");

} else if (signal == 'G') {

printf("Go! It's green signal.");

} else {

printf("Invalid signal input.");

return 0;

}
.Q.10.WAP in C to display the day of week in words based on number input ( 1 for Sunday, 2 for
Monday, etc.) using switch statement.

#include <stdio.h>

int main() {

int day;

// Taking input from the user

printf("Enter a number (1 for Sunday, 2 for Monday, etc.): ");

scanf("%d", &day);

// Using switch to determine the day

switch(day) {

case 1:

printf("Sunday\n");

break;

case 2:

printf("Monday\n");

break;

case 3:

printf("Tuesday\n");

break;

case 4:

printf("Wednesday\n");

break;

case 5:

printf("Thursday\n");

break;

case 6:

printf("Friday\n");

break;
case 7:

printf("Saturday\n");

break;

default:

printf("Invalid input! Please enter a number between 1 and 7.\n");

return 0;

}
Q11.Program to print the following pattern in C

*
**
***
****
*****

#include <stdio.h>

int main() {

int rows, i, j;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; i++) {

for (j = 1; j <= i; j++) {

printf("*");

printf("\n");

return 0;

}
Q.12)WAP in C that will ask the user for a number and check whether the number is a perfect
number or not.

#include <stdio.h>

int main() {

int num, i, sum = 0;

printf("Enter a number: ");

scanf("%d", &num);

for (i = 1; i < num; i++) {

if (num % i == 0) {

sum += i;

if (sum == num) {

printf("%d is a perfect number.\n", num);

} else {

printf("%d is not a perfect number.\n", num);

return 0;

}
Q.13.) WAP in C to reverse a number. #include <stdio.h>

int main() {

int num, reversed = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &num);

while (num != 0) {

remainder = num % 10;

reversed = reversed * 10 + remainder;

num /= 10;

printf("Reversed number = %d\n", reversed);

return 0;

}
14) WAP in C to check whether a number is prime, armstrong, perfect number or not using
functions.

#include <stdio.h>

#include <math.h>

int isPrime(int num) {

if (num <= 1) {

return 0;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) {

return 0;

return 1;

int isArmstrong(int num) {

int originalNum = num, digits = 0, sum = 0;

while (originalNum > 0) {

digits++;

originalNum /= 10;

originalNum = num;

while (originalNum > 0) {

int digit = originalNum % 10;

sum += pow(digit, digits);

originalNum /= 10;
}

return sum == num;

int isPerfect(int num) {

int sum = 0;

for (int i = 1; i < num; i++) {

if (num % i == 0) {

sum += i;

return sum == num;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (isPrime(num)) {

printf("%d is a prime number.\n", num);

} else

if (isArmstrong(num)) {

printf("%d is an Armstrong number.\n", num);

} else if (isPerfect(num)) {

printf("%d is a perfect number.\n", num);


} else {

printf("%d is not a prime, Armstrong, or perfect number.\n", num);

return 0;

}
Q15. WAP in c to find maximum and minimum between two
numbers using functions.
#include<stdio.h>

//function to find the maximum of two numbers

int findMax(int a,int b){

return(a>b)?a:b;

//function to find the maximum of two number

int findMin(int a,int b){

return(a<b)?a:b;

int main(){

int num1,num2;

//ask the user to enter two numbers

printf("Enter two user:");

scanf("%d%d",&num1,&num2);

//Find the maximum ans minimum using the functions

int max = findMax(num1,num2);

int min = findMin(num1,num2);

//print the result

printf("Maximum:%d\n",max);

printf("Maximum:%d\n",min);

return 0;

#include<stdio.h>

int factorial(int n){

if(n==0){;
return 1;//Base case:factorial of 0 is 1

}else{

return n*factorial(n-1);//Recursive case

}
16)WAP in C to find factorial of any number using a recursive function.

#include <stdio.h>

// Function prototype

long long factorial(int n);

int main() {

int num;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial of a negative number doesn't exist.\n");

} else {

printf("Factorial of %d = %lld\n", num, factorial(num));

return 0;

// Recursive function to calculate factorial

long long factorial(int n) {

if (n == 0 || n == 1) {
return 1; // Base case

return n * factorial(n - 1); // Recursive case

}
17) WAP in C to generate nth Fibonacci term using a recursive function.

#include <stdio.h>

int fibonacci(int n) {

if (n <= 1) {

return n; // Base cases: F(0) = 0, F(1) = 1

} else {

return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case

int main() {

int n;

printf("Enter the value of n: ");

scanf("%d", &n);

if (n < 0) {

printf("Invalid input. Please enter a non-negative integer.\n");

} else {

int result = fibonacci(n);

printf("The %dth Fibonacci term is %d\n", n, result);

return 0;
}
18) WAP in C to sort the list of numbers stored in an array in ascending
order.
#include <stdio.h>

void main (){

int num[20];

int i, j, a, n;

printf("enter number of elements in an array");

scanf("%d", &n);

printf("Enter the elements");

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

scanf("%d", &num[i]);

for (i = 0; i < n; ++i){

for (j = i + 1; j < n; ++j){

if (num[i] > num[j]){

a = num[i];

num[i] = num[j];

num[j] = a;

printf("The numbers in ascending order is:");

for (i = 0; i < n; ++i){

printf("%d", num[i]);

}
19) WAP in C to multiply two matrices.

#include <stdio.h>

#define N 3 // Size of square matrices

int main() {

int matrix1[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int matrix2[N][N] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

int result[N][N];

int i, j, k;

// Multiply the two matrices

for (i = 0; i < N; i++) {

for (j = 0; j < N; j++) {

int sum = 0;

for (k = 0; k < N; k++) {

sum += matrix1[i][k] * matrix2[k][j];

result[i][j] = sum;

// Print the resulting matrix

printf("Result Matrix:\n");

for (i = 0; i < N; i++) {

for (j = 0; j < N; j++) {

printf("%d ", result[i][j]);


}

printf("\n");

return 0;

}
20) WAP in C to check if given string is palindrome or not.

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int i, len;

int flag = 0;

printf("Enter a string: ");

scanf("%s", str);

len = strlen(str);

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

if(str[i] != str[len - i - 1]) {

flag = 1;

break;

if(flag) {

printf("%s is not a palindrome.\n", str);

} else {

printf("%s is a palindrome.\n", str);

}
return 0;

}
21)Define a structure named Circle to represent a circle with a radius. WAP in C to
calculate the area and perimeter of two circles and display the results.

#include <stdio.h>

#define PI 3.14159

struct Circle

float radius;

};

int main()

struct Circle circle1, circle2;

// Get radii from user

printf("Enter the radius of the first circle: ");

scanf("%f", &circle1.radius);

printf("Enter the radius of the second circle: ");

scanf("%f", &circle2.radius);

// Calculate areas and perimeters

float area1 = PI * circle1.radius * circle1.radius;

float perimeter1 = 2 * PI * circle1.radius;

float area2 = PI * circle2.radius * circle2.radius;

float perimeter2 = 2 * PI * circle2.radius;

// Print results

printf("Area of the first circle: %.2f\n", area1);

printf("Perimeter of the first circle: %.2f\n", perimeter1);


printf("Area of the second circle: %.2f\n", area2);

printf("Perimeter of the second circle: %.2f\n", perimeter2);

return 0;

}
22) WAP in C to Create a File, Write in it, And Close the File.

#include <stdio.h>
#include <conio.h>

void main() {
char name[201];
int marks, n, i = 0;

FILE *fp = fopen("first.txt", "w");

if (fp == NULL) {
printf("File not opened.\n");
return;
}

printf("How many records do you want to insert? ");


scanf("%d", &n);

while (i < n) {
printf("Enter the Name and Marks: ");
scanf("%s %d", name, &marks);
fprintf(fp, "%s %d\n", name, marks);
i++;
}

fclose(fp);

fp = fopen("first.txt", "r");

if (fp == NULL) {
printf("File is not open.\n");
} else {
printf("STUDENT RECORDS\n");
printf("Name - Marks\n");
while (fscanf(fp, "%s %d", name, &marks) != EOF) {
printf("%s - %d\n", name, marks);
}
fclose(fp);
}
}
23) Write a program in C to calculate the length of a string using a pointer.

#include <stdio.h>

int main() {

char str[100];

char *ptr;

int length = 0;

printf("Enter a string: ");

fgets(str, 100, stdin);

// Pointer points to the first character of the string

ptr = str;

// Iterate through the string until the null character is encountered

while (*ptr != '\0') {

length++;

ptr++;

printf("Length of the string: %d\n", length);

return 0;

}
24) WAP to create a structure named "Employee" to store employee details such as employee
ID, name, and salary. Write a program to input data for three employees, find the highest
salary employee, and display their information.

#include <stdio.h>

struct Employee {

int id;

char name[50];

float salary;

};

int main() {

struct Employee emp[3], highest;

// Input data for 3 employees

for (int i = 0; i < 3; i++) {

printf("Enter details for employee %d:\n", i + 1);

printf("Enter ID: ");

scanf("%d", &emp[i].id);

printf("Enter name: ");

scanf("%s", emp[i].name);

printf("Enter salary: ");

scanf("%f", &emp[i].salary);

// Find the employee with the highest salary

highest = emp[0];

for (int i = 1; i < 3; i++) {

if (emp[i].salary > highest.salary) {


highest = emp[i];

// Display information of the highest salary employee

printf("\nEmployee with the highest salary:\n");

printf("ID: %d\n", highest.id);

printf("Name: %s\n", highest.name);

printf("Salary: %.2f\n", highest.salary);

return 0;

You might also like