C Practice Question Example
In C practice Question has more then 100+ Question with Code .and this programs are catograize with all diffrent topic in c .i hope you will enjoy our c practice Example with coding program.
Basic Program
- Write a program in c to take your name, father name, college name, and course name as input from the user then print it.
- Write a program in c take input of five subjects from the user and find out total marks and percentage.
- Write a program in c take two numbers and input through the keyboard into two locations C and D interchange the contents of C and D.
- Write a program in c to take two numbers from the user and swap them without temp.
- Write a program in c to take length and width as input from the user to find the Area and Perimeter of a Rectangle.
- Write a program in c take the input radius from the user and find the circumference of circle and the area of a circle.
- Write a program in c that takes input as two numbers from the user finds the sum of numbers finds the sum of numbers and square of that sum result and then also calculates the cube of square result.
- Write a program in c to take input as the temperature in degrees Celsius and convert it to Fahrenheit.
- Write a program in c to take input P, T, and R from the user and find out simple interests.
- Write a program to print whether two numbers are equal or not.
- Write a c program to check whether a number is divisible by 5 or not.
- Write a c program to input week number and print week day.
- Write a c program to input any alphabet and check whether it is vowel or consonant.
- Write a program to check number is negative, positive, or zero in c.
- Write a program in c to check whether the number is odd or even.
-
• For the first 50 units Rs. 0.50/unit
• For the next 100 units Rs. 0.75/unit
• For the next 100 units Rs. 1.20/unit
-
Write a c program to check you are eligible for vote or not/drive or not.
- Write a program in c take two numbers from user and find out largest number using conditional operator.
- Write a program in c take three numbers are input from user and find the greatest number among three.
- Write a program in c to check number is prime or not.
- Write a program the number is greater or smaller in c.
-
Write a cpp program to find Fibonacci Series up to n number of terms.
The Fibonacci sequence is like this,
0, 1, 1, 2, 3, 5, 8, 13, 21.....n term.
- Write a program in c to check you are eligible for vote or not/drive or not.
- Write a program in c to check whether a number is a palindrome or not.
- Write a program in c take input salary from user theen increase 10% of salary and print total salary of employee..
- Write a program in c to check whether an alphabet is vowel or a consonant using switch case.
- Write a program in c,accept 1 to 7 number and display their respective day name using switch case.
- Write a program in c Menu driven programm for all arithmetic operation by switch case.
- Write a program in c to enter a number then print their English word.
-
Write a c program to create simple calculator using switch case.
- Write a c program to check whether a number is positive ,negative or zero using switch case.
- Write a c program to check whether a number is even or odd using switch case.
- Write a program in c to print 1 to 10 numbers.
- Write a program in c to add 1 to 10 numbers.
- write a program in c take number as input from the user and generate table
- Write a program in c to find even numbers in 1 to 50.
- Write a program in c to find odd numbers in 1 to 50.
- Write a program in c to display a range of alphabets in reversed & alternative order.
-
Write a program in c to display the numbers in reverse order.
-
Write a program in c to display the pattern like right angle triangle with number.
-
Write a C program that makes a pattern such as below.
1 22 333 4444 55555
-
Write a C program that makes a pattern such as below.
55555 4444 333 22 1
-
Write a program in c that makes a pattern such as below.
1 2 3 4 5 6 7 8 9 10
-
Write a program in c that makes a pattern such as below.
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
-
Write a program in c that makes a pattern such as below.
* * * * * * * * * * * * * * *
- Write a c program to take input 10 number in two array and find out addition of two array .
- Write a c program to count total number of elements in an array.
- Write a c program to array of matrices.
- Take 10 integer inputs from user and store them in an array. Now, copy all the elements in another array but in reverse order.
- Write a c program to take input 10 number in array and find sum of all number and product.
- Write a c program to take input 5 number in array and find out square of all number.
- Write a program in c Take 10 integer as inputs from user in array and find out all even number and all odd number.
- Write a c program to take input 5 subject of marks in array and find out total marks and percentage.
- Write a c program to print all negative elements in an array.
- Write a c program to take 10 number as input from user in array and find out the largest element of array.
- Write a c program to take 10 number as input from user in array and find out the Smallest element of array.
- Write a c program to take 10 number as input from user in array and find out the Second largest element of array.
- Write a c program to take 10 number as input from user in array and find out the Second Smallest element of array.
- Write a c program to addition two matrices.
- Write a c program to check whether two matrices are equal or not.
- Write a bubble sort program in c.
- Write a linear search programming in c.
- Write a Insertion sorting program in c.
- Write a searching program in c.
- Write Binary Search program in c language.
- Write a c program to take 10 number as input from user in array and then take any number as input in another variable then search this number is exist or not.
- Write a c program to find length of a string.
- Write a c program to copy one string to another string.
- Write a c program to concatenate two strings.
- Write a c program to compare two strings.
- Write a c program to convert lowercase string to uppercase.
- Write a c program to convert uppercase string to lowercase.
- Write a c program to toggle case of character of a string.
- Write a c program to find reverse of a string.
- Write a c program to reverse order of words in a given string.
- Write a c program to find cube of any number using function.
- Write a c program to find diameter,circumference and area of circle using functions.
- Write a c program to check whether a number is even or odd using functions.
- Write a program in C to swap elements using call by reference.
- Write a c program to find factorial of any number using recursion.
- Write a c program to print all natural numbers between 1 to n using recursion.
- Create a structure called "Student" with members name, age, and total marks. Write a C program to input data for two students, display their information, and find the average of total marks.
- Define a structure named Time with members hours, minutes, and seconds. Write a C program to input two times, add them, and display the result in proper time format.
- Create a structure named Book to store book details like title, author, and price. Write a C program to input details for three books,an display Inforamtion.
- Define a structure named Circle to represent a circle with a radius. Write a C program to calculate the area circle and display the results.
- 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.
- Create a structure named "Date" with members day, month, and year. Write a C program to display information.
- Write a c program to add two numbers using pointers.
- Write a c program to swap two numbers using pointers.
- write a c program to find length of string using poinetrs
- write a c program to reverse an array using pointers.
- Write a c program to find reverse of a string using pointers.
Q. Write a program in c to input your name, father name, college name, and course name as input from the user then print it.
#include<stdio.h>
#include<conio.h>
int main(){
char name[50],f_name[50],c_name[50],co_name[20];
//take the input using scanf
printf("Enter your name:");
scanf("%s",&name);
printf("Enter your father name:");
scanf("%s",&f_name);
printf("Enter your college name:");
scanf("%s",&c_name);
printf("Enter your course name:");
scanf("%s",&co_name);
//print the data
printf("\n========Output============\n");
printf("\nNAME: %s ",name);
printf("\nFATHER NAME: %s",f_name);
printf("\nCOLLEGE NAME: %s",c_name);
printf("\nCOURSE NAME: %s",co_name);
getch();
return 0;
}
2.Write a program in c take two numbers as input from the user and find out the addition of two numbers.
//Write a program in c take two numbers as input from the user and find out the addition of two numbers.
#include<stdio.h>
#include<conio.h>
int main(){
int a,b,sum;
printf("ENTER THE FIRST NUMBER:");
scanf("%d",&a);
printf("ENTER THE SECOND NUMBER:");
scanf("%d",&b);
sum=a+b;
printf("THE SUM IS:%d",sum);
getch();
return 0;
}
3.Write a program in c take two numbers as input from the user and find out the subtraction of two numbers.
//Write a program in c take two numbers as input from the user and find out the subtraction of two numbers.
#include<stdio.h>
#include<conio.h>
int main(){
int a,b;
printf("ENTER THE NUMBER:");
scanf("%d",&a);
printf("ENTER THE SECOND NUMBER:");
scanf("%d",&b);
int sub=a-b;
printf("THE SUBTRACTON IS: %d",sub);
getch();
return 0;
}
4.Write a program in c take input of five subjects from the user and find out total marks and percentage.
//Write a program in c take input of five subjects from the user and find out total marks and percentage.
#include<stdio.h>
#include<conio.h>
int main(){
int eng,mar,math,sci,hin;
printf("ENTER THE MARKS OF FIVE SUBJECTS:");
scanf("%d %d %d %d %d",&eng,&mar,&math,&sci,&hin);
int total=eng+mar+math+sci+hin;
float per=(float)(total*100)/500;
printf("THE TOTAL IS : %d",total);
printf("\nTHE PERCENTAGE IS : %f",per);
getch();
return 0;
}
5.Write a program in c take two numbers and input through the keyboard into two locations C and D interchange the contents of C and D.
// C Program to Swap Two Numbers using a Temporary Variable
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
printf("Before swapping: a = %d, b = %d\n", a, b);
// Swapping values of a and b
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
6.Write a program in c to take two numbers from the user and swap them without temp.
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
7.Write a program in c to take length and width as input from the user to find the Area and Perimeter of a Rectangle.
// C program to demonstrate the
// area and perimeter of rectangle
#include <stdio.h>
int main()
{
int l,b;
printf("ENTER THE LENGTH:");
scanf("%d",&l);
printf("ENTER THE BREADTH:");
scanf("%d",&b);
int A, P;
A = l * b;
P = 2 * (l + b);
printf("Area of rectangle is : %d", A);
printf("\nPerimeter of rectangle is : %d", P);
return 0;
}
8.Write a program in c take the input radius from the user and find the circumference of circle and the area of a circle.
#include <stdio.h> // Standard input-output header file
// Define the constant value of Pi (p)
#define PI 3.14159
int main() {
double radius, area, circumference;
// Prompt the user to enter the radius of the circle
printf("Enter the radius of the circle: ");
// Read the user input and store it in the variable 'radius'
scanf("%lf", &radius);
// Calculate the area of the circle using the formula: Area = pr^2
area = PI * radius * radius;
// Calculate the circumference of the circle using the formula: Circumference = 2pr
circumference = 2 * PI * radius;
// Display the calculated area
printf("The area of the circle is: %lf\n", area);
// Display the calculated circumference
printf("The circumference of the circle is: %lf\n", circumference);
return 0; // Return 0 to indicate successful execution
}
9.Write a program in c that takes input as two numbers from the user finds the sum of numbers finds the sum of numbers and square of that sum result and then also calculates the cube of square result.
//Write a program in c that takes input as two numbers from the user finds the sum of numbers finds the sum of numbers and square of that sum result and then also calculates the cube of square result.
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,sum,square,cube;
printf("ENTER TWO NUMBERS:");
scanf("%d %d",&a,&b);
sum=a+b;
square=sum*sum;
cube=sum*sum*sum;
printf("THE SUM IS : %d",sum);
printf("\nTHE SQUARE OF THE RESULT IS : %d",square);
printf("\nTHE CUBE OF THE RESULT IS : %d",cube);
return 0;
}
10.Write a program in c to take input as the temperature in degrees Celsius and convert it to Fahrenheit.
#include<stdio.h>
int main()
{
float fahrenheit, celsius;
printf("ENTER TEMPERATURE IN CELCIUS:");
scanf("%f",&celsius);
fahrenheit =( (celsius*9)/5)+32;
printf("\n\n Temperature in fahrenheit is: %f",fahrenheit);
return (0);
}
11.Write a program in c to take input P, T, and R from the user and find out simple interests.
#include <stdio.h>
int main()
{
float principle, time, rate, SI;
/* Input principle, rate and time */
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
/* Calculate simple interest */
SI = (principle * time * rate) / 100;
/* Print the resultant value of SI */
printf("Simple Interest = %f", SI);
return 0;
}
12.Write a program to print whether two numbers are equal or not.
#include <stdio.h> // Include the standard input/output header file.
int main()
{
int int1, int2; // Declare two integer variables 'int1' and 'int2'.
printf("Input the values for Number1 and Number2 : "); // Prompt the user to input values for Number1 and Number2.
scanf("%d %d", &int1, &int2); // Read and store the user's input in 'int1' and 'int2'.
if (int1 == int2) // Check if Number1 is equal to Number2.
printf("Number1 and Number2 are equal\n"); // Print a message if Number1 and Number2 are equal.
else
printf("Number1 and Number2 are not equal\n"); // Print a message if Number1 and Number2 are not equal.
return 0;
}
13.Write a c program to check whether a number is divisible by 5 or not.
#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(num % 5 ==0)
{
printf("Number is divisible by 5");
}
else
{
printf("Number is not divisible by 5");
}
return 0;
}
14.Write a c program to input week number and print week day.
#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number (1-7): ");
scanf("%d", &week);
if(week == 1)
{
printf("Monday");
}
else if(week == 2)
{
printf("Tuesday");
}
else if(week == 3)
{
printf("Wednesday");
}
else if(week == 4)
{
printf("Thursday");
}
else if(week == 5)
{
printf("Friday");
}
else if(week == 6)
{
printf("Saturday");
}
else if(week == 7)
{
printf("Sunday");
}
else
{
printf("Invalid Input! Please enter week number between 1-7.");
}
return 0;
}
15.Write a c program to input any alphabet and check whether it is vowel or consonant.
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is a uppercase vowel
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if c is a vowel
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
16.Write a program to check number is negative, positive, or zero in c.
#include<stdio.h>
int main()
{
int num;
printf("ENTER NUMBER:");
scanf("%d",&num);
if(num == 0)
printf("The Number is Zero");
else if(num < 0)
printf("Negative");
else
printf("Positive");
return 0;
}
17.Write a program in c to check whether the number is odd or even.
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
18.Write a c program to input the customer ID, name, and electricity unit consumed by the user and calculate the total electricity bill according to the given condition.
• For the first 50 units Rs. 0.50/unit
• For the next 100 units Rs. 0.75/unit
• For the next 100 units Rs. 1.20/unit
.For units above 250 Rs. 1.50/unit
• An additional surcharge of 20% is added to the bill
#include <stdio.h>
int main() {
int customerID;
char customerName[50];
float unitsConsumed, bill, totalBill;
// Input customer details
printf("Enter Customer ID: ");
scanf("%d", &customerID);
printf("Enter Customer Name: ");
scanf(" %[^\n]s", customerName); // Read string with spaces
printf("Enter Units Consumed: ");
scanf("%f", &unitsConsumed);
// Calculate the bill based on the units consumed
if (unitsConsumed <= 50) {
bill = unitsConsumed * 0.50;
} else if (unitsConsumed <= 150) {
bill = (50 * 0.50) + ((unitsConsumed - 50) * 0.75);
} else if (unitsConsumed <= 250) {
bill = (50 * 0.50) + (100 * 0.75) + ((unitsConsumed - 150) * 1.20);
} else {
bill = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((unitsConsumed - 250) * 1.50);
}
// Add a surcharge of 20%
totalBill = bill + (0.20 * bill);
// Output the total bill
printf("\nCustomer ID: %d\n", customerID);
printf("Customer Name: %s\n", customerName);
printf("Units Consumed: %.2f\n", unitsConsumed);
printf("Total Electricity Bill: Rs. %.2f\n", totalBill);
return 0;
}
19.write a c program to enter marks of five subjects and calculate the Total, Percentage, and find Grade according to the following Conditions:.
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
#include <stdio.h>
int main() {
float marks[5];
float total = 0.0, percentage;
char grade;
// Input marks for five subjects
printf("Enter marks for five subjects:\n");
for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &marks[i]);
total += marks[i]; // Calculate total marks
}
// Calculate percentage
percentage = (total / 500) * 100; // Assuming each subject is out of 100
// Determine grade based on percentage
if (percentage >= 90) {
grade = 'A';
} else if (percentage >= 80) {
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else if (percentage >= 40) {
grade = 'E';
} else {
grade = 'F';
}
// Output total, percentage, and grade
printf("\nTotal Marks: %.2f\n", total);
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}
20.write a c programm to take input absent days and find out Total Fine fees accourding to below Condition.
1 week pay 5 rs per day
2 week pay 10 rs per day
3 week pay 15 rs per day
Rest 20 rupees per days.
#include <stdio.h>
int main() {
int absentDays;
int totalFine = 0;
// Ask user to input the number of absent days
printf("Enter the number of absent days: ");
scanf("%d", &absentDays);
// Calculate the fine based on the conditions
if (absentDays <= 7) {
totalFine = absentDays * 5;
} else if (absentDays <= 14) {
totalFine = 7 * 5 + (absentDays - 7) * 10;
} else if (absentDays <= 21) {
totalFine = 7 * 5 + 7 * 10 + (absentDays - 14) * 15;
} else {
totalFine = 7 * 5 + 7 * 10 + 7 * 15 + (absentDays - 21) * 20;
}
// Print the total fine
printf("Total Fine: %d rupees\n", totalFine);
return 0;
}
21.Write a c program to check you are eligible for vote or not/drive or not.
#include <stdio.h>
int main() {
int age;
// Ask user to input their age
printf("Enter your age: ");
scanf("%d", &age);
// Check eligibility for voting
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
// Check eligibility for driving
if (age >= 16) {
printf("You are eligible to drive.\n");
} else {
printf("You are not eligible to drive.\n");
}
return 0;
}
22.Write a program in c take two numbers from user and find out largest number using conditional operator.
#include <stdio.h>
int main() {
int num1, num2, largest;
// Ask user to input two numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Use conditional logic to assign the largest number
if (num1 > num2) {
largest = num1; // num1 is larger
} else {
largest = num2; // num2 is larger or both are equal
}
// Output the largest number
printf("The largest number is: %d\n", largest);
return 0;
}
23.Write a program in c take three numbers are input from user and find the greatest number among three.
#include <stdio.h>
int main() {
int num1, num2, num3;
// Ask user to input three numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter third number: ");
scanf("%d", &num3);
// Find the greatest number using if-else statements
if (num1 >= num2 && num1 >= num3) {
printf("The greatest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
}
return 0;
}
24.Write a program in c to check number is prime or not.
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
// Ask the user to enter a number
printf("Enter a number: ");
scanf("%d", &num);
// Handle edge cases for numbers less than 2
if (num <= 1) {
isPrime = 0; // 0 and 1 are not prime numbers
} else {
// Check divisibility from 2 to sqrt(num)
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0; // Not a prime number if divisible by i
break;
}
}
}
// Print whether the number is prime or not
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
24.Write a program the number is greater or smaller in c.
#include<stdio.h>
#include<conio.h>
#include <stdio.h>
int main() {
int num1, num2;
// Ask the user to input two numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Compare the two numbers
if (num1 > num2) {
printf("%d is greater than %d\n", num1, num2);
} else if (num1 < num2) {
printf("%d is smaller than %d\n", num1, num2);
} else {
printf("%d and %d are equal\n", num1, num2);
}
return 0;
}
25.Write a program in c to find & display sum of 6+12+18+….N term and 4+10+16+…N Term , n to be entered by the user.
#include <stdio.h>
int main() {
int N, sum1 = 0, sum2 = 0;
// Taking input for N
printf("Enter the value of N: ");
scanf("%d", &N);
// Calculating sum of the first series: 6 + 12 + 18 + ... + N
printf("Series 1: 6 + 12 + 18 + ... up to N\n");
for (int i = 6; i <= N; i += 6) {
sum1 = sum1 + i;
}
printf("Sum of first series: %d\n", sum1);
// Calculating sum of the second series: 4 + 10 + 16 + ... up to N
printf("Series 2: 4 + 10 + 16 + ... up to N\n");
for (int i = 4; i <= N; i += 6) {
sum2 += i;
}
printf("Sum of second series: %d\n", sum2);
return 0;
}
26.Write a cpp program to find Fibonacci Series up to n number of terms.
The Fibonacci sequence is like this,
0, 1, 1, 2, 3, 5, 8, 13, 21.....n term.
#include <iostream>
using namespace std;
int main() {
int n;
// Taking input for number of terms
cout << "Enter the number of terms: ";
cin >> n;
// Check for the case where n is less than or equal to 0
if (n <= 0) {
cout << "Please enter a positive number for the number of terms." << endl;
} else {
// Variables to store the first two Fibonacci numbers
long long int first = 0, second = 1, next;
// Print the Fibonacci sequence up to n terms
cout << "Fibonacci Series: ";
// If n is 1, print only the first Fibonacci number
if (n >= 1) {
cout << first << " ";
}
// If n is 2, print the first two Fibonacci numbers
if (n >= 2) {
cout << second << " ";
}
// Generate and print Fibonacci numbers from the 3rd term onwards
for (int i = 3; i <= n; ++i) {
next = first + second; // Calculate the next term in the series
cout << next << " "; // Print the next term
first = second; // Update first to the second term
second = next; // Update second to the next term
}
cout << endl;
}
return 0;
}
27.Write a program in c to check you are eligible for vote or not/drive or not.
#include <stdio.h>
int main() {
int age;
// Taking input for the user's age
printf("Enter your age: ");
scanf("%d", &age);
// Check eligibility for voting
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
// Check eligibility for driving
if (age >= 16) {
printf("You are eligible to drive.\n");
} else {
printf("You are not eligible to drive.\n");
}
return 0;
}
28.Write a program in c to check whether a number is a palindrome or not.
#include <stdio.h>
int main() {
int num, reverse = 0, remainder, original;
// Taking input for the number
printf("Enter a number: ");
scanf("%d", &num);
// Store the original number for comparison later
original = num;
// Reverse the number
while (num != 0) {
remainder = num % 10; // Get the last digit
reverse = reverse * 10 + remainder; // Build the reversed number
num /= 10; // Remove the last digit
}
// Check if the original number is equal to the reversed number
if (original == reverse) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
29.Write a program in c take input salary from user theen increase 10% of salary and print total salary of employee..
#include <stdio.h>
int main() {
float salary, totalSalary;
// Taking input for salary
printf("Enter the current salary of the employee: ");
scanf("%f", &salary);
// Calculate the total salary after a 10% increase
totalSalary = salary + (salary * 0.10);
// Output the total salary
printf("The total salary after a 10%% increase is: %.2f\n", totalSalary);
return 0;
}
30.Write a program in c to check whether an alphabet is vowel or a consonant using switch case.
#include <stdio.h>
int main() {
char ch;
// Ask user for input
printf("Enter an alphabet: ");
scanf("%c", &ch);
// Convert to lowercase for easier comparison
ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;
// Check if the character is a vowel or consonant using switch case
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
if((ch >= 'a' && ch <= 'z')) {
printf("%c is a consonant.\n", ch);
} else {
printf("Invalid input. Please enter a valid alphabet.\n");
}
}
return 0;
}
31.Write a program in c,accept 1 to 7 number and display their respective day name using switch case.
#include <stdio.h>
int main() {
int day;
// Ask user to input a number between 1 and 7
printf("Enter a number (1-7) to display the corresponding day: ");
scanf("%d", &day);
// Switch case to display day name based on the input number
switch(day) {
case 1:
printf("1 corresponds to Sunday.\n");
break;
case 2:
printf("2 corresponds to Monday.\n");
break;
case 3:
printf("3 corresponds to Tuesday.\n");
break;
case 4:
printf("4 corresponds to Wednesday.\n");
break;
case 5:
printf("5 corresponds to Thursday.\n");
break;
case 6:
printf("6 corresponds to Friday.\n");
break;
case 7:
printf("7 corresponds to Saturday.\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 7.\n");
}
return 0;
}
32.Write a program in c Menu driven programm for all arithmetic operation by switch case.
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
// Display menu
printf("Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
// Ask for the user's choice
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
// If user chooses to perform an operation
if(choice >= 1 && choice <= 4) {
// Ask for two numbers
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
}
// Perform operation based on choice
switch(choice) {
case 1: // Addition
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case 2: // Subtraction
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case 3: // Multiplication
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case 4: // Division
if(num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error! Division by zero.\n");
}
break;
case 5: // Exit
printf("Exiting the program.\n");
break;
default: // Invalid choice
printf("Invalid choice! Please select a valid option from 1 to 5.\n");
}
return 0;
}
33.Write a program in c to enter a number then print their English word.
#include <stdio.h>
void printNumberInWords(int num) {
// Print the corresponding word for the number
switch (num) {
case 0: printf("Zero\n"); break;
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break;
case 3: printf("Three\n"); break;
case 4: printf("Four\n"); break;
case 5: printf("Five\n"); break;
case 6: printf("Six\n"); break;
case 7: printf("Seven\n"); break;
case 8: printf("Eight\n"); break;
case 9: printf("Nine\n"); break;
default: printf("Invalid input! Please enter a number between 0 and 9.\n");
}
}
int main() {
int num;
// Ask the user for input
printf("Enter a number between 0 and 9: ");
scanf("%d", &num);
// Call function to print the number in words
printNumberInWords(num);
return 0;
}
34.Write a menu driven program which accepts a user’s choice to calculate and display area of any of the following shapes ,with reqired inputs entered by the user for the desired shape: a)The menu should keep reappearing till the user chooses an option Exit to exit the program execution b) a.Area of a rectangle b. Area of a triangle c. Area of Circle.
#include <stdio.h>
#include <math.h>
// Function to calculate area of rectangle
void areaOfRectangle() {
float length, width, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("Area of the rectangle: %.2f\n", area);
}
// Function to calculate area of triangle
void areaOfTriangle() {
float base, height, area;
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("Area of the triangle: %.2f\n", area);
}
// Function to calculate area of circle
void areaOfCircle() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = M_PI * radius * radius; // Using math.h for the value of Pi
printf("Area of the circle: %.2f\n", area);
}
int main() {
int choice;
do {
// Display menu
printf("\nMenu:\n");
printf("1. Area of Rectangle\n");
printf("2. Area of Triangle\n");
printf("3. Area of Circle\n");
printf("4. Exit\n");
// Accept user choice
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
// Perform operation based on choice
switch(choice) {
case 1:
areaOfRectangle();
break;
case 2:
areaOfTriangle();
break;
case 3:
areaOfCircle();
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please enter a valid option (1-4).\n");
}
} while(choice != 4); // Keep showing the menu until the user chooses to exit
return 0;
}
35.Write a c program to create simple calculator using switch case.
#include <stdio.h>
int main() {
float num1, num2, result;
int choice;
// Display the menu
printf("Simple Calculator\n");
printf("-------------------\n");
printf("Select operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
// Loop to perform the operation until user chooses to exit
while(1) {
// Accept the user's choice
printf("\nEnter your choice (1-5): ");
scanf("%d", &choice);
// Check for the exit option
if(choice == 5) {
printf("Exiting the program.\n");
break;
}
// Accept the numbers for the operation
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
// Perform the operation based on the user's choice
switch(choice) {
case 1: // Addition
result = num1 + num2;
printf("Result: %.2f + %.2f = %.2f\n", num1, num2, result);
break;
case 2: // Subtraction
result = num1 - num2;
printf("Result: %.2f - %.2f = %.2f\n", num1, num2, result);
break;
case 3: // Multiplication
result = num1 * num2;
printf("Result: %.2f * %.2f = %.2f\n", num1, num2, result);
break;
case 4: // Division
if(num2 != 0) {
result = num1 / num2;
printf("Result: %.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error! Division by zero.\n");
}
break;
default: // Invalid choice
printf("Invalid choice! Please select a valid option.\n");
}
}
return 0;
}
36.Write a c program to check whether a number is positive ,negative or zero using switch case.
#include <stdio.h>
int main() {
int num;
// Ask the user for input
printf("Enter a number: ");
scanf("%d", &num);
// Use switch-case to check the number
switch (num > 0) {
case 1: // Positive number
printf("%d is a positive number.\n", num);
break;
case 0: // Either zero or negative
switch (num < 0) {
case 1: // Negative number
printf("%d is a negative number.\n", num);
break;
case 0: // Zero
printf("%d is zero.\n", num);
break;
}
break;
default:
printf("Unexpected behavior.\n");
}
return 0;
}
37.Write a c program to check whether a number is even or odd using switch case.
#include <stdio.h>
int main() {
int num;
// Ask the user for input
printf("Enter a number: ");
scanf("%d", &num);
// Use switch case to check if the number is even or odd
switch (num % 2) {
case 0: // Even number
printf("%d is an even number.\n", num);
break;
case 1: // Odd number
printf("%d is an odd number.\n", num);
break;
default:
printf("Unexpected error.\n");
}
return 0;
}
38.Write a program in C to print 1 to 10 numbers.
#include <stdio.h>
int main()
{
for(int i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
39. Write a program in C to add 1 to 10 numbers.
#include <stdio.h>
int main()
{
int sum = 0;
for(int i = 1; i <= 10; i++)
{
sum += i;
}
printf("Sum of numbers from 1 to 10 is: %d", sum); return 0;
}
40. Write a program in C to take number as input from the user and generate a table.
#include <stdio.h>
int main()
{ int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication Table of %d:\n", num);
for(int i = 1; i <= 10; i++)
{ printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
41. Write a program in C to find even numbers in 1 to 50.
#include <stdio.h>
int main() {
printf("Even numbers from 1 to 50:\n");
for(int i = 1; i <= 50; i++)
{
if(i % 2 == 0)
{ printf("%d ", i);
}
} return 0;
}
42. Write a program in C to find odd numbers in 1 to 50.
???????
#include <stdio.h>
int main() {
printf("Odd numbers from 1 to 50:\n");
for(int i = 1; i <= 50; i++)
{
if(i % 2 != 0)
{
printf("%d ", i);
}
}
return 0;
}
43. Write a program in C to display a range of alphabets in reversed & alternative order.
???????
#include <stdio.h>
int main()
{
for(char ch = 'Z'; ch >= 'A'; ch -= 2)
{
printf("%c ", ch);
}
return 0;
}
44. Write a program in C to display the numbers in reverse order.
#include <stdio.h>
int main()
{ int num = 1345;
printf("The number in reverse order is: ");
while(num != 0)
{
printf("%d", num % 10);
num /= 10;
}
return 0;
}
45.Write a program in C to display the pattern like right angle triangle with numbers.
1
22
334
4444
55555
#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
46. Write a C program that makes a pattern such as below.
1
22
333
4444
55555
???????
#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
printf("%d", i);
}
printf("\n");
}
return 0; }
47.. Write a C program that makes a pattern such as below.
55555
4444
333
22
1
???????
#include <stdio.h>
int main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= i; j++)
{
printf("%d", i);
}
printf("\n");
}
return 0;
}
48. Write a program in C that makes a pattern such as below.
1
2 3
4 5 6
7 8 9 10
???????
#include <stdio.h>
int main()
{
int num = 1;
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
{
printf("%d ", num++);
}
printf("\n");
}
return 0;
}
49. Write a program in C that makes a pattern such as below.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
???????
#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 5 - i; j++)
{
printf(" ");
}
for(int j = 1; j <= i; j++)
{
printf("%d ", i);
}
printf("\n");
}
return 0;
}
50. Write a program in C that makes a pattern such as below.
* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
int main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= 5 - i; j++)
{
printf(" ");
}
for(int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
51.Write a program in C that makes a pattern such as below.
* * * * *
* * * *
* * *
* *
*
???????
#include <stdio.h>
int main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= 5 - i; j++)
{
printf(" ");
}
for(int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
52. Write a program in C that makes a pattern such as below.
* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
int main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 1; j <= 5 - i; j++)
{
printf(" ");
}
for(int j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
53. Write a C program to take input 10 numbers in two arrays and find out the addition of two arrays.
???????
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], sum[10];
printf("Enter 10 numbers for first array:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr1[i]);
}
printf("Enter 10 numbers for second array:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr2[i]);
}
for(int i = 0; i < 10; i++)
{
sum[i] = arr1[i] + arr2[i];
}
printf("The sum of the two arrays is:\n");
for(int i = 0; i < 10; i++)
{
printf("%d ", sum[i]);
}
return 0;
}
54. Write a C program to count the total number of elements in an array.
#include <stdio.h>
int main()
{
int arr[10], count = 0;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for(int i = 0; i < 10; i++)
{
count++;
}
printf("Total number of elements in the array: %d", count);
return 0;
}
55. Write a C program to work with an array of matrices.
#include <stdio.h> int main()
{
int matrix[2][2] = {{1, 2}, {3, 4}};
printf("Matrix is:\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
56.. Take 10 integer inputs from the user and store them in an array. Now, copy all the elements in another array but in reverse order.
#include <stdio.h>
int main()
{
int arr[10], reverseArr[10];
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for(int i = 0; i < 10; i++)
{
reverseArr[i] = arr[9 - i];
}
printf("Reversed array:\n");
for(int i = 0; i < 10; i++)
{
printf("%d ", reverseArr[i]);
}
return 0;
}
57. Write a C program to take input 10 numbers in an array and find the sum of all numbers and product.
#include <stdio.h>
int main()
{
int arr[10], sum = 0, product = 1;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for(int i = 0; i < 10; i++)
{
sum += arr[i]; product *= arr[i];
}
printf("Sum: %d\n", sum);
printf("Product: %d", product);
return 0;
}
58. Write a C program to take input 5 numbers in an array and find out the square of all numbers.
#include <stdio.h>
int main()
{ int arr[5];
printf("Enter 5 numbers:\n");
for(int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("Squares of the numbers are:\n");
for(int i = 0; i < 5; i++)
{
printf("%d ", arr[i] * arr[i]);
}
return 0;
}
59. Write a program in C to take 10 integers as inputs from the user in an array and find out all even numbers and all odd numbers.
#include <stdio.h>
int main()
{ int arr[10];
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
printf("Even numbers are:\n");
for(int i = 0; i < 10; i++)
{
if(arr[i] % 2 == 0)
{ printf("%d ", arr[i]);
}
}
printf("\nOdd numbers are:\n");
for(int i = 0; i < 10; i++)
{
if(arr[i] % 2 != 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
60. Write a C program to take input 5 subjects of marks in an array and find out total marks and percentage.
#include <stdio.h>
int main()
{
int marks[5], total = 0;
float percentage;
printf("Enter marks for 5 subjects:\n");
for(int i = 0; i < 5; i++)
{
scanf("%d", &marks[i]);
total += marks[i];
}
percentage = (float)total / 5;
printf("Total marks: %d\n", total);
printf("Percentage: %.2f%%", percentage);
return 0;
}
61. Write a C program to print all negative elements in an array.
#include <stdio.h>
int main()
{
int arr[10];
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
printf("Negative numbers are:\n");
for(int i = 0; i < 10; i++)
{
if(arr[i] < 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
62. Write a C program to take 10 numbers as input from the user in an array and find out the largest element of the array.
#include <stdio.h>
int main()
{
int arr[10], largest;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
largest = arr[0];
for(int i = 1; i < 10; i++)
{
if(arr[i] > largest)
{
largest = arr[i];
}
}
printf("Largest number is: %d", largest);
return 0;
}
63. Write a C program to take 10 numbers as input from the user in an array and find out the smallest element of the array.
#include <stdio.h>
int main()
{
int arr[10], smallest;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
smallest = arr[0];
for(int i = 1; i < 10; i++)
{
if(arr[i] < smallest)
{
smallest = arr[i];
}
}
printf("Smallest number is: %d", smallest);
return 0;
}
64. Write a C program to take 10 numbers as input from the user in an array and find out the second largest element of the array.
#include <stdio.h>
int main()
{
int arr[10], largest, secondLargest;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
largest = secondLargest = arr[0];
for(int i = 1; i < 10; i++)
{
if(arr[i] > largest)
{
secondLargest = largest; largest = arr[i];
}
else if(arr[i] > secondLargest && arr[i] < largest)
{ secondLargest = arr[i];
}
}
printf("Second largest number is: %d", secondLargest);
return 0;
}
65. Write a C program to take 10 numbers as input from the user in an array and find out the second smallest element of the array.
#include <stdio.h>
int main()
{
int arr[10], smallest, secondSmallest;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
smallest = secondSmallest = arr[0];
for(int i = 1; i < 10; i++)
{
if(arr[i] < smallest)
{
secondSmallest = smallest;
smallest = arr[i];
}
else if(arr[i] < secondSmallest && arr[i] > smallest)
{
secondSmallest = arr[i];
}
}
printf("Second smallest number is: %d", secondSmallest);
return 0;
}
66. Write a C program for the addition of two matrices.
#include <stdio.h>
int main()
{
int matrix1[2][2], matrix2[2][2], sum[2][2];
printf("Enter elements of first matrix (2x2):\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of second matrix (2x2):\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
scanf("%d", &matrix2[i][j]);
}
}
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("The sum of the two matrices is:\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
67. Write a C program to check whether two matrices are equal or not.
#include <stdio.h>
int main()
{
int matrix1[2][2], matrix2[2][2];
int flag = 1;
// Assume matrices are equal
printf("Enter elements of first matrix (2x2):\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of second matrix (2x2):\n");
for(int i = 0; i < 2; i++)
{ for(int j = 0; j < 2; j++)
{
scanf("%d", &matrix2[i][j]);
}
}
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
if(matrix1[i][j] != matrix2[i][j])
{ flag = 0; break;
}
}
}
if(flag)
printf("The matrices are equal.\n");
else
printf("The matrices are not equal.\n");
return 0;
}
68. Write a bubble sort program in C.
#include <stdio.h>
int main()
{
int arr[10], temp;
printf("Enter 10 numbers to sort:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for(int i = 0; i < 10 - 1; i++)
{
for(int j = 0; j < 10 - i - 1; j++)
{
if(arr[j] > arr[j + 1]) {
temp = arr[j]; arr[j] = arr[j + 1];
arr[j + 1] = temp; }
}
}
printf("Sorted array:\n");
for(int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
69. Write a linear search program in C.
#include <stdio.h>
int main()
{
int arr[10], target, found = 0;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &target);
for(int i = 0; i < 10; i++)
{
if(arr[i] == target)
{
found = 1;
printf("Number found at index %d\n", i);
break;
}
}
if (!found)
printf("Number not found in the array.\n");
return 0;
}
70. Write an insertion sorting program in C.
???????
#include <stdio.h>
int main()
{
int arr[10], key, j;
printf("Enter 10 numbers to sort:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for(int i = 1; i < 10; i++)
{
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key)
{ arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
printf("Sorted array:\n");
for(int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
71. Write a searching program in C.
???????
#include <stdio.h>
int main()
{
int arr[10], target, found = 0;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &target);
for(int i = 0; i < 10; i++)
{
if(arr[i] == target)
{
found = 1; printf("Number found at index %d\n", i);
break;
}
}
if (!found)
printf("Number not found in the array.\n");
return 0;
}
72. Write a binary search program in C language.
#include <stdio.h>
int binarySearch(int arr[], int size, int target)
{
int low = 0, high = size - 1, mid;
while (low <= high)
{
mid = low + (high - low) / 2;
if (arr[mid] == target)
{
return mid;
}
if (arr[mid] < target)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int arr[10], target, result;
printf("Enter 10 numbers (sorted):\n");
for (int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &target);
result = binarySearch(arr, 10, target);
if (result == -1)
printf("Number not found in the array.\n");
else
printf("Number found at index %d.\n", result);
return 0;
}
73. Write a C program to take 10 numbers as input from the user in an array and then take any number as input in another variable and search if this number exists or not.
#include <stdio.h>
int main()
{
int arr[10], target, found = 0;
printf("Enter 10 numbers:\n");
for(int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &target);
for(int i = 0; i < 10; i++)
{
if(arr[i] == target)
{
found = 1; printf("Number found at index %d\n", i);
break;
}
}
if (!found)
printf("Number not found in the array.\n");
return 0;
}
74. Write a C program to find the length of a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
printf("Enter a string: ");
gets(str);
int length = strlen(str);
printf("The length of the string is: %d", length);
return 0;
}
75. Write a C program to copy one string to another string.
???????
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("Enter a string: ");
gets(str1);
strcpy(str2, str1);
printf("Copied string: %s", str2);
return 0;
}
76. Write a C program to concatenate two strings.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
77. Write a C program to compare two strings.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
if (strcmp(str1, str2) == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
78. Write a C program to convert a lowercase string to uppercase.
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
printf("Enter a lowercase string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++)
{
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s\n", str);
return 0;
}
79. Write a C program to convert an uppercase string to lowercase.
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
printf("Enter an uppercase string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++)
{
str[i] = tolower(str[i]);
}
printf("Lowercase string: %s\n", str);
return 0;
}
80. Write a C program to toggle case of characters in a string.
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++)
{
if (islower(str[i]))
str[i] = toupper(str[i]);
else if (isupper(str[i]))
str[i] = tolower(str[i]);
}
printf("Toggled case string: %s\n", str);
return 0;
}
81. Write a C program to find the reverse of a string.
#include <stdio.h>
#include <string.h>
int main()
{ char str[100], rev[100];
int len, i;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0; i < len; i++)
rev[i] = str[len - i - 1];
rev[i] = '\0';
printf("Reversed string: %s\n", rev);
return 0;
}
82. Write a C program to reverse the order of words in a given string.
#include <stdio.h>
#include <string.h>
void reverseWords(char *str)
{
int i, j, start, end;
char temp;
int len = strlen(str);// Reverse the entire string first
for (i = 0, j = len - 1; i < j; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
} // Reverse each word in the reversed string
start = 0;
for (i = 0; i <= len; i++)
{
if (str[i] == ' ' || str[i] == '\0')
{
end = i - 1;
while (start < end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
start = i + 1;
}
}
}
int main()
{
char str[100];
printf("Enter a string: ");
gets(str);
reverseWords(str);
printf("Reversed order of words: %s\n", str);
return 0; }
83. Write a C program to find the cube of any number using a function.
#include <stdio.h>
int cube(int num)
{
return num * num * num;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Cube of %d is %d\n", num, cube(num));
return 0;
}
84. Write a C program to find the diameter, circumference, and area of a circle using functions.
#include <stdio.h>
#define PI 3.14159
float diameter(float r)
{
return 2 * r;
}
float circumference(float r)
{
return 2 * PI * r;
}
float area(float r)
{
return PI * r * r;
}
int main()
{
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Diameter: %.2f\n", diameter(radius));
printf("Circumference: %.2f\n", circumference(radius));
printf("Area: %.2f\n", area(radius));
return 0;
}
85. Write a C program to check whether a number is even or odd using functions.
#include <stdio.h>
void checkEvenOdd(int num)
{
if (num % 2 == 0)
printf("%d is Even.\n", num);
else
printf("%d is Odd.\n", num);
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
checkEvenOdd(num);
return 0;
}
86.Write a program in C to swap elements using call by reference.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b; *b = temp;
}
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
87. Write a C program to find the factorial of any number using recursion.
#include <stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{ int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %ld\n", num, factorial(num));
return 0;
}
88. Write a C program to print all natural numbers between 1 to n using recursion.
#include <stdio.h>
void printNumbers(int n)
{
if (n == 0) return;
printNumbers(n - 1);
printf("%d ", n);
}
int main()
{
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Natural numbers from 1 to %d: ", n);
printNumbers(n);
printf("\n");
return 0;
}
89. Create a structure called "Student" with members name, age, and total marks. Write a C program to input data for two students, display their information, and find the average of total marks.
#include <stdio.h>
struct Student
{
char name[50];
int age;
float totalMarks;
};
int main()
{
struct Student s1, s2;
float avgMarks;
printf("Enter details of Student 1 (Name Age Total Marks): ");
scanf("%s %d %f", s1.name, &s1.age, &s1.totalMarks);
printf("Enter details of Student 2 (Name Age Total Marks): ");
scanf("%s %d %f", s2.name, &s2.age, &s2.totalMarks);
avgMarks = (s1.totalMarks + s2.totalMarks) / 2.0;
printf("\nStudent 1: Name: %s, Age: %d, Marks: %.2f\n", s1.name, s1.age, s1.totalMarks); printf("Student 2: Name: %s, Age: %d, Marks: %.2f\n", s2.name, s2.age, s2.totalMarks); printf("Average Marks: %.2f\n", avgMarks);
return 0;
}
90. Define a structure named Time with members hours, minutes, and seconds. Write a C program to input two times, add them, and display the result in proper time format.
#include <stdio.h>
struct Time
{
int hours, minutes, seconds;
};
struct Time addTime(struct Time t1, struct Time t2)
{
struct Time result;
result.seconds = t1.seconds + t2.seconds;
result.minutes = t1.minutes + t2.minutes + (result.seconds / 60);
result.seconds %= 60;
result.hours = t1.hours + t2.hours + (result.minutes / 60);
result.minutes %= 60;
return result;
}
int main()
{
struct Time t1, t2, sum;
printf("Enter first time (hh mm ss): ");
scanf("%d %d %d", &t1.hours, &t1.minutes, &t1.seconds);
printf("Enter second time (hh mm ss): ");
scanf("%d %d %d", &t2.hours, &t2.minutes, &t2.seconds);
sum = addTime(t1, t2);
printf("Sum of times: %02d:%02d:%02d\n", sum.hours, sum.minutes, sum.seconds);
return 0;
}
91.Create a structure named Book to store book details like title, author, and price. Write a C program to input details for three books and display the information.
#include <stdio.h>
struct Book
{
char title[50];
char author[50];
float price;
};
int main()
{
struct Book books[3];
for (int i = 0; i < 3; i++)
{
printf("Enter details of Book %d (Title Author Price): ", i + 1);
scanf("%s %s %f", books[i].title, books[i].author, &books[i].price);
}
printf("\nBook Information:\n");
for (int i = 0; i < 3; i++)
{
printf("Book %d - Title: %s, Author: %s, Price: %.2f\n", i + 1, books[i].title, books[i].author, books[i].price);
}
return 0;
}
92.Define a structure named Circle to represent a circle with a radius. Write a C program to calculate the area circle and display the results.
#include <stdio.h>
#define PI 3.14159
// Define a structure to represent a Circle
struct Circle {
float radius;
};
// Function to calculate area of the circle
float calculateArea(struct Circle c) {
return PI * c.radius * c.radius;
}
int main() {
struct Circle c;
// Input radius from user
printf("Enter the radius of the circle: ");
scanf("%f", &c.radius);
// Calculate and display area
printf("The area of the circle is: %.2f\n", calculateArea(c));
return 0;
}
93. 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>
// Define the structure Employee
struct Employee {
int id;
char name[50];
float salary;
};
// Function to find the employee with the highest salary
struct Employee findHighestSalary(struct Employee emp[], int size) {
struct Employee highest = emp[0];
for (int i = 1; i < size; i++) {
if (emp[i].salary > highest.salary) {
highest = emp[i];
}
}
return highest;
}
int main() {
struct Employee employees[3], highestPaid;
// Input details for 3 employees
for (int i = 0; i < 3; i++) {
printf("Enter details for Employee %d (ID, Name, Salary): ", i + 1);
scanf("%d", &employees[i].id);
getchar(); // To consume the newline character left by scanf
printf("Enter Name: ");
fgets(employees[i].name, sizeof(employees[i].name), stdin);
printf("Enter Salary: ");
scanf("%f", &employees[i].salary);
}
// Find the highest salary employee
highestPaid = findHighestSalary(employees, 3);
// Display the employee with the highest salary
printf("\nEmployee with the Highest Salary:\n");
printf("ID: %d\n", highestPaid.id);
printf("Name: %s", highestPaid.name);
printf("Salary: %.2f\n", highestPaid.salary);
return 0;
}
94.Create a structure named "Date" with members day, month, and year. Write a C program to display information.
#include <stdio.h>
// Define the structure Date
struct Date {
int day;
int month;
int year;
};
int main() {
struct Date d;
// Input date details
printf("Enter the date (DD MM YYYY): ");
scanf("%d %d %d", &d.day, &d.month, &d.year);
// Display the date
printf("The entered date is: %02d/%02d/%04d\n", d.day, d.month, d.year);
return 0;
}
95.Write a C program to add two numbers using pointers.
#include <stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2; // Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2); // Assign addresses to pointers
ptr1 = &num1;
ptr2 = &num2; // Calculate sum using pointers
sum = *ptr1 + *ptr2; // Display result
printf("Sum = %d\n", sum);
return 0;
}
96. Write a C program to swap two numbers using pointers.
#include <stdio.h>
// Function to swap two numbers using pointers
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
// Input two numbers printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
// Call function to swap
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
97. Write a C program to find the length of a string using pointers.
???????
#include <stdio.h>
int stringLength(char *str)
{
int length = 0;
while (*str != '\0')
{ // Traverse till null character
length++;
str++;
// Move pointer to next character
}
return length;
}
int main()
{
char str[100];
// Input string
printf("Enter a string: ");
gets(str); // Compute and display length
printf("Length of the string is: %d\n", stringLength(str));
return 0;
}
98. Write a C program to reverse an array using pointers.
#include <stdio.h>
void reverseArray(int *arr, int size)
{
int *start = arr, *end = arr + size - 1, temp;
// Swap elements using pointers
while (start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main()
{
int arr[100], n;
// Input array size
printf("Enter number of elements: ");
scanf("%d", &n);
// Input array elements
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Reverse array
reverseArray(arr, n);
// Display reversed array
printf("Reversed array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
99. Write a C program to find the reverse of a string using pointers.
#include <stdio.h>
#include <string.h>
void reverseString(char *str)
{
char *start = str;
char *end = str + strlen(str) - 1;
char temp;
// Swap characters using pointers
while (start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main()
{
char str[100];
// Input string
printf("Enter a string: ");
gets(str);
// Reverse the string
reverseString(str);
// Display reversed string
printf("Reversed string: %s\n", str);
return 0;
}
check using function
#include<stdio.h>
#include<stdbool.h>
bool check_prime(int num)
{
bool isPrime=true;
int i;
if(num==0||num==1)
{
isPrime=false;
return isPrime;
}
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
return isPrime=false;
}
}
return isPrime;
}
int main()
{
int n,i;
printf("Enter a Number");
scanf("%d",&n);
if(check_prime(n))
{
printf("its prime number");
}
else
{
printf("its not prime number");
}
return 0;
}
using function between 1 to nth term
#include<stdio.h>
#include<stdbool.h>
bool check_prime(int num)
{
bool isPrime=true;
int i;
if(num==0||num==1)
{
isPrime=false;
return isPrime;
}
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
return isPrime=false;
}
}
return isPrime;
}
int main()
{
int n,i;
printf("Enter n Number for find prime number ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(check_prime(i))
{
printf("%d , ",i);
}
}
return 0;
}
nsertion sorting in c
// Insertion sort in C
#include <stdio.h>
int main() {
int data[50] ,step,i,n;
printf("Enter Size of array (Max 50 )");
scanf("%d",&n);
printf("Enter array elements in array \n");
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
for ( i = 1; i < n; i++) {
int key = data[i];
int j = i - 1;
while (key < data[j] && j >= 0) {
data[j + 1] = data[j];
--j;
}
data[j + 1] = key;
}
printf("Sorted array in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", data[i]);
}
}
Linear Search program in c language
#include<stdio.h>
int main()
{
int num[50],data,found,i,n,loc;
printf("Enter Size of array (Max 50 )");
scanf("%d",&n);
printf("Enter array elements in array \n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
printf("enter element to be searched for\n");
scanf("%d",&data);
for(i=0;i<n;i++)
{
if(num[i]==data)
{
found=1;
loc=i;
}
}
if(found==1)
{
printf("Data Found Possition at %d",loc+1);
}
else
{
printf("Data not found");
}
return 0;
}
Binary Search program in c language.
#include<stdio.h>
int main()
{ //in binary search first check mid point is equal
//or not with item
int num[50],data,beg,last,n,mid,x=0,l,i; // if item is greater then chenge mid with beg
printf("Enter Size of array (Max 50 )"); //if item is smaller then change mid with last
scanf("%d",&n);
printf("Enter array elements in asc order \n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
printf("enter element to be searched for\n");
scanf("%d",&data);
beg=0;
last=n-1;
while(beg<=last && !x)
{
mid=(beg+last)/2;
if(data==num[mid])
{
x=1;
l=mid;
}
else if(data>num[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
if(x==1)
{
printf("data Found possition at %d",l+1);
}
else
{
printf("not found");
}
return 0;
}
A pointer in C is a variable that stores the memory address of another variable. Pointers are used for dynamic memory allocation, accessing arrays, and passing variables by reference to functions.
Key Points About Pointers:
- Declared using the
*
symbol. - Hold the address of another variable.
- The
&
operator is used to get the address of a variable. - The
*
operator (dereference operator) is used to access the value at the memory address.
Example 1:
#include<stdio.h>
void main()
{
int a = 10; // Declare an integer variable
int *ptr = &a; // Declare a pointer that holds the address of 'a'
printf("Value of a: %d\n", a); // Print the value of 'a'
printf("Address of a: %p\n", &a); // Print the address of 'a'
printf("Value of ptr: %p\n", ptr); // Print the value stored in pointer (address of 'a')
printf("Value at address ptr: %d\n", *ptr); // Dereference pointer to get the value of 'a'
return 0;
}
Example 2 :
Swapping Two Numbers Using Pointers:
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x; // Dereference to access and swap values
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b); // Pass addresses of 'a' and 'b' to the function
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Example 3 :
Pointer to Array:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // Pointer to the first element of the array
int i;
for (i = 0; i < 5; i++) {
printf("Value at arr[%d]: %d\n", i, *(ptr + i)); // Access elements using pointer arithmetic
}
return 0;
}
Example 4 :
Scan data in array Pointer
#include <stdio.h>
int main()
{
int arr[5];
int *ptr = arr;
int i;
printf("Enter elements in array:\n");
for (i=0; i < 5; i++)
{
scanf("%d", ptr);
// Move pointer to next array element
ptr++;
}
ptr = arr;
printf("Array elements: ");
for (i = 0; i < 5; i++)
{
// Print value pointed by the pointer
printf("%d, ", *(ptr+i));
// printf("%d, ", *ptr);
// Move pointer to next array element
// ptr++;
}
return 0;
}
example 5 :
Pointer to a function :
#include <stdio.h>
int add(int a)
{
return a;
}
int main()
{
int (*myfunp)(int);
int result ;
myfunp=add;
result = (*myfunp)(56);
printf("%d",result);
return 0;
}
Example 6:
#include <stdio.h>
// Function to be pointed to
void greet() {
printf("Hello, World!\n");
}
// Function with parameters
void add(int a, int b) {
printf("Sum: %d\n", a + b);
}
int main() {
void (*func_ptr1)() = greet; // Pointer to greet function
void (*func_ptr2)(int, int) = add; // Pointer to add function
// Call the functions using pointers
func_ptr1();
func_ptr2(10, 20);
return 0;
}
example 7 :
get input data to pointer function
#include <stdio.h>
int add(int a,int b)
{
int c=a+b;
return c;
}
int main()
{
int a,b;
int(*pf)(int,int);
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
pf=add;
result=(*pf)(a,b);
printf("Result is %d",result);
return 0;
}
Last Updated on : 23rd Feb 2025 22:11:56 PM
Latest Update
- JavaScript Interview Question MongoDb Command Curd Operation Node MonogoDb Express EJSC Practice Question Example Data Structure with CPPCurd Operation with static array in node js and ejs filejava practice questionCurd operation with localstorage in javascriptComplete curd operation in react with json server and axios and router-domCPP Practice Question Java Pattern Program