C Prog Part A and B 27 Progs
C Prog Part A and B 27 Progs
Write a program to read radius of a circle and to find and display area and circumference
#include<stdio.h>
void main()
{
int radius;
float area, circum;
scanf("%d",&radius);
area=3.14*radius*radius;
circum=2*3.14*radius;
//2. Write a program to read Principal amount, Time and Rate and calculate Simple Interest.
#include <stdio.h>
void main() {
}
//3. C program to find the largest number among three numbers
#include <stdio.h>
int main()
{
int a = 10, b = 22, c = 9;
printf("The numbers a, b and c are: %d, %d, %d\n", a, b, c);
if (a >= b) {
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else {
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
return 0;
}
//4. Write a program to function as a basic calculator; it should ask the user to input what
type of arithmetic operation he would like, and then ask for the numbers on which the
operation should be performed. The calculator should then give the output of the operation.
Use switch. Error message should be reported, if any attempt is made to divide by zero.
#include <stdio.h>
int main() {
char op;
int first, second;
switch (op) {
case '+':
printf("%d + %d = %d", first, second, first + second);
break;
case '-':
printf("%d - %d = %d", first, second, first - second);
break;
case '*':
printf("%d * %d = %d", first, second, first * second);
break;
case '/':
if(second == 0){
printf("Error! second number cannot be zero");
}
else{
printf("%d / %d = %d", first, second, first / second);
}
break;
return 0;
}
//5. Write a program to find the roots of quadratic equation (Demonstration of else-if ladder)
#include<stdio.h>
#include<math.h>
void main () {
float a,b,c,r1,r2,d;
d= b*b - 4*a*c;
if (d>0) {
r1 = -b+sqrt(d) / (2*a);
r2 = -b-sqrt(d) / (2*a);
printf("The real roots = %f %f", r1, r2);
}
else if(d==0) {
r1 = -b/(2*a);
r2 = -b/(2*a);
printf("Roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");
//6. Write a program to read marks scored by a student in n subjects and find and display the
average of all marks
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, mark, sum=0;
float avg;
avg = sum/n;
//7. Write a program to demonstrate basic mathematical library functions defined in math.h
#include<stdio.h>
#include <math.h>
void main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
}
//8. Write a program to find HCF (GCD) of two numbers.
#include <stdio.h>
// Main code
void main()
{
int a,b;
//9. Write a program that accepts a number n, and prints all prime numbers between 1 to n.
#include<stdio.h>
void main(){
flag=1;
for(i=2;i<=num/2;i++){
if(num%i==0){
flag=0;
break;
}
}
if(flag==1)
printf("%d ",num);
}
}
//10. Write a program to read a number, find the sum of its digits, reverse the number and
check if it is a palindrome. Display appropriate message.
#include <stdio.h>
void main()
{
int num, temp, remainder, reverse = 0, sum=0;
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
}
//11. Write a program to read numbers from keyboard continuously till the user presses 999
and to find the sum of only positive numbers. Display appropriate error messages.
#include<stdio.h>
void main(){
int n, sum=0;
while(1){
printf("\nEnter a number:");
scanf("%d",&n);
if(n==999){
printf("\nExiting the program");
break;
}
else if(n>=0){
sum=sum+n;
}
else{
printf("\nError: You entered negative Number");
}
}
//12. Write a program to read percentage of marks and to display appropriate message
specifying class obtained. (demonstration of Switch Case statement)
#include<stdio.h>
void main(){
float percentage;
int temp;
switch(temp){
case 10:
case 9:
case 8:
printf("\n Grade: A");
break;
case 7:
case 6:
printf("\n Grade: B");
break;
case 5:
case 4:
printf("\n Grade: C");
break;
case 3:
case 2:
case 1:
case 0:
printf("\n Grade: Fail");
break;
default:
printf("\nInvalid input");
}
}
//13. Write a program to read and concatenate two strings without using library function
#include<stdio.h>
while(s[i]!='\0'){
i++;
}
return i;
}
void main(){
i=0;
s1_length = length(s1);
while(s2[i]!='\0'){
s1[s1_length+i]=s2[i];
i++;
}
s1[s1_length+i]='\0';
printf("\n concatenated string : %s",s1);
//14. Write a program to print sum of even numbers and sum of odd numbers from array of
integers which are to be inputed.
#include<stdio.h>
void main(){
sum_even = 0;
sum_odd = 0;
printf("\nEnter %d elements\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]%2==0)
sum_even = sum_even + a[i];
else
sum_odd = sum_odd + a[i];
}
//15. Write a program to read a list of numbers, print it, then remove duplicate elements from
the list and print the modified list. Use single dimensional Array
#include<stdio.h>
void main(){
int a[100], n, i, j, k;
printf("\nEnter %d elements\n",n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
for(j = i+1; j < n; j++){
if(a[i] == a[j]){
for(k = j; k < n; k++){
a[k] = a[k+1];
}
j--;
n--;
}
}
}
}
PART B
//1. Write a program to swap(interchange) two numbers without using a temporary variable
#include <stdio.h>
int main() {
int a, b;
return 0;
}
//2. Write a program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters using character functions
#include <stdio.h>
#include <ctype.h> // For character functions
int main() {
char str[100];
int i;
int alphabets = 0, digits = 0, vowels = 0, consonants = 0, spaces = 0, specialChars = 0;
return 0;
}
//3. Write a program to read a string, reverse it, concatenate the original string with its
reverse, and print length of the original string and the concatenated string using built in
functions
#include <stdio.h>
#include <string.h> // For string functions
int main() {
return 0;
}
//4. Write a program to read a string and find the length of a string without using built in
function
#include <stdio.h>
int main() {
char str[100];
int length;
return 0;
}
//5. Write a program to generate and display first n values in the Fibonacci sequence using
recursive function
#include <stdio.h>
int main() {
int n, i;
return 0;
}
#include <stdio.h>
int main() {
int number;
return 0;
}
//7. Write a program to read elements in a square matrix, display it in the form of a matrix
and find and print its trace
#include <stdio.h>
int main() {
int n, i, j;
int matrix[n][n];
return 0;
}
//8. Write a program to read two matrices and perform addition and subtraction on them
#include <stdio.h>
int main() {
return 0;
}
//9. Write a program to read, display and multiply two m x n matrices using functions.
#include <stdio.h>
// Function prototypes
void readMatrix(int matrix[][10], int rows, int cols);
void displayMatrix(int matrix[][10], int rows, int cols);
void multiplyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int rows1, int cols1, int
rows2, int cols2);
int main() {
int rows1, cols1, rows2, cols2;
printf("Second matrix:\n");
displayMatrix(matrix2, rows2, cols2);
return 0;
}
//10. Write a program to check and display if a number is prime by defining isprime( )
function
#include <stdio.h>
int main() {
int number;
return 0;
}
//11. Write a program using functions that takes in three arguments - a start temperature (in
Celsius), an end temperature (in Celsius) and a step size. Print out a table that goes from the
start temperature to the end temperature, in steps of the step size; converting each Celsius to
Fahrenheit.
#include <stdio.h>
// Function prototypes
void printTemperatureTable(int start, int end, int step);
float celsiusToFahrenheit(float celsius);
int main() {
int startTemp, endTemp, stepSize;
// Read the start temperature, end temperature, and step size from the user
printf("Enter the start temperature (in Celsius): ");
scanf("%d", &startTemp);
printf("Enter the end temperature (in Celsius): ");
scanf("%d", &endTemp);
printf("Enter the step size: ");
scanf("%d", &stepSize);
//12. Write a C program to create array of structure which stores Roll No, Name and Average
marks of students. Accept n students data and print it in proper format.
#include <stdio.h>
int main() {
int n;
printf("Name: ");
scanf("%s", &students[i].name);
return 0;
}
//13. Write a C program to illustrate difference between structure and union by defining
emp_no ,emp_name, salary as members and display the size of the defined structure
#include <stdio.h>
#include <string.h>
int main() {
// Declare variables of structure and union
struct EmployeeStruct empStruct;
union EmployeeUnion empUnion;
return 0;
}