1. 1.
The distance between two cities (in Km) is input through
the keyboard. Write a C program to convert and print this
distance in meter, feet, inches and centimeter.
#include<stdio.h>
int main()
{
float km,f,cm,in,m;
printf("Enter distance between two cities=");
scanf("%f",&km);
m=km*1000;
cm=km*1000*100;
f=km*3280.84;
in=km*39370.08;
printf("The distance in meter=%.2f\n",m);
printf("The distance in centimeter=%.2f\n",cm);
printf("The distance in feet=%.2f\n",f);
printf("The distance in inches=%.2f",in);
}
2. 2. C program to find the greatest of three numbers (using
nested if statement)
#include<stdio.h>
int main()
{
int n1,n2,n3;
printf("Enterr Three number:");
scanf("%d %d %d",&n1,&n2,&n3);
printf("Three numbers are:%d %d %d\n",n1,n2,n3);
if(n1>n2)
{
if(n1>n3)
printf("%d is greatest number",n1);
else
printf("%d is greatest number",n3);
}
else
{
if(n2>n3)
printf("%d is greatest number",n2);
else
printf("%d is greatest number",n3);
}
}
3. 3. C program to calculate compound interest
#include<stdio.h>
#include<math.h>
int main()
{
float p,t,r,ci;
printf("Enter principle amount:");
scanf("%f",&p);
printf("Enter rate of interest:");
scanf("%f",&r);
printf("Enter time:");
scanf("%f",&t);
ci=p*(pow((1+r/100),t));
printf("Compound interest is:%.2f\n",ci);
return 0;
}
Output:
gcc comp.c -lm
4.4. Write a program to convert years into
5. Months
6. Days
7. Hours
8. Minute
9. Seconds
Using switch () statements*/
#include<stdio.h>
int main()
{
float y, m,d,hr,sec,min;
int ch;
printf("Enter Number of years:");
scanf("%f",&y);
m=y*12;
d=y*365;
hr=24*365*y;
min=60*24*365*y;
sec=60*60*24*365*y;
printf("Enter your choice\n");
printf("1.Convert year to Month\n");
printf("2.Convert year to Days\n");
printf("3.Convert year to Hours\n");
printf("4.Convert year to Minutes\n");
printf("5.Convert year to Seconds\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("%.1f year=%.0f month\n",y,m);
break;
case 2:printf("%.1f years=%.0f days\n",y,d);
break;
case 3:printf("%.1f year=%.0f hours\n",y,hr);
break;
case 4:printf("%.1f year=%.0f minutes\n",y,min);
break;
case 5:printf("%.1f year=%.0f seconds\n",y,sec);
break;
default:printf("Invalid choice");
}
return 0;
}
5. C program to reverse a number and find the sum of individual
digits. Also check for palindromes
#include<stdio.h>
int main()
{
int n,rem=0,rev=0,sum=0;
printf("enter number=");
scanf("%d",&n);
int num=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
sum=sum+rem;
}
printf("Reverse number=%d\n",rev);
printf("Sum of individual digit is=%d\n",sum);
if(num==rev)
printf("%d is palindrome",num);
else
printf("%d is not palindrome",num);
}
10.6. C program to find a given number is a prime or not.
#include <stdio.h>
int main()
{
int n, i, flag = 1;
printf("Enter number: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 0;
for (i = 2; i <= n / 2;i++)
{
if (n % i == 0)
{
flag = 0;
break;
}
}
if (flag)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
7. Write a program for matrix multiplication after verifying
matrix multiplication conditions.
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
printf("Enter no. of rows and columns for matrix A:\n");
scanf("%d%d",&m,&n);
printf("Enter no. of rows and columns for matrix B:\n");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("Enter elements for matrix A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter elements for matrix B:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("Matrix A is=\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Matrix B is=\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("Matrix multiplication is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Multiplication not posible");
}
8. Write a program to search element in array using binary search
#include<stdio.h>
void main()
{
int a[20],ele,n,i,beg,mid,end;
printf("Enter the number element:");
scanf("%d",&n);
printf("Enter the element\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be search\n");
scanf("%d",&ele);
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end)
{
if(a[mid]==ele)
{
printf("%d found in position %d",ele,mid);
break;
}
else if(a[mid]<ele)
{
beg=mid+1;
}
else
end=mid-1;
mid=(beg+end)/2;
}
if(beg>end)
printf("%d not found",ele);
}
9. Write a program to sort array using bubble sort
#include<stdio.h>
void main()
{
int a[10],i,j,n,temp;
printf("Enter the number of element:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the elements before soting:");
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
for (i = 0 ; i< n -1; i++)
{
for (j = 0 ; j < n -1-i; j++)
{
if (a[j] > a[j+1])
{
temp = a[j];
a[j]= a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
return 0;
}
10. WAP in C to perform addition, subtraction, multiplication, division
and modulus using a function pointer.
#include <stdio.h>
#include<stdlib.h>
int main()
{
int a = 20, b = 10;
int add, sub, div, mul, mod;
int *ptr_a, *ptr_b;
ptr_a = &a;
ptr_b = &b;
add = *ptr_a + *ptr_b;
sub = *ptr_a - *ptr_b;
mul = *ptr_a * *ptr_b;
printf("Addition = %d\n", add);
printf("Subtraction = %d\n", sub);
printf("Multiplication = %d\n", mul);
if(*ptr_b!=0}
{
div = *ptr_a / *ptr_b;
mod = *ptr_a % *ptr_b;
printf("Division = %d\n", div);
printf("Modulo = %d\n", mod);
}
else
{
printf(“Divide by zero error\n”);
exit(0);
}
return 0;
}
11. WAP in C to find sum of array elements using Dynamic Memory Allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int limit
int i;
int sum;
printf("Enter limit of the array: ");
scanf("%d", &limit);
ptr = (int*)malloc(limit * sizeof(int));
for (i = 0; i < limit; i++)
{
printf("Enter element %d: ", i + 1);
scanf("%d", (ptr + i));
}
printf("\nEntered array elements are:\n");
for (i = 0; i < limit; i++)
{
printf("%d\n", *(ptr + i));
}
sum = 0;
for (i = 0; i < limit; i++)
{
sum += *(ptr + i);
}
printf("Sum of array elements is: %d\n", sum);
free(ptr);
return 0;
}
12.WAP in C to perform bitwise AND, OR, NOT, XOR, shift left and shift right.
#include<stdio.h>
void main()
int n1,n2;
printf("Enter first number:");
scanf("%d",&n1);
printf("Enter second number:");
scanf("%d",&n2);
printf("Bitwise AND:%d\n",n1 & n2);
printf("Bitwise OR:%d\n",n1 | n2);
printf("Bitwise complement ~ %d is :%d\n",n1,~n1);
printf("Bitwise complement ~ %d is:%d\n",n2,~n2);
printf("Bitwise XOR :%d\n",n1 ^ n2);
printf("Bitwise left shift %d is :%d\n",n1,n1 << 2);
printf("Bitwise right shift %d is :%d\n",n1,n1 >> 2);
13. WAP in C to find the factorial of a number using recursion and find nCr
and nPr. and combination of given numbers
#include<stdio.h>
long Factorial(int n);
long find_ncr(int n, int r);
long find_npr(int n,int r);
int main()
{
int n,r;
long ncr,npr;
printf("Enter the value of n and r:");
scanf("%d%d",&n,&r);
//scanf("%d",&n);
printf("Factorial of %d = %ld\n", n,Factorial(n));
ncr = Factorial(n)/(Factorial(r)*Factorial(n-r));
npr = Factorial(n)/Factorial(n-r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long Factorial(int n)
{
if (n>=1)
return n*Factorial(n-1);
else
return 1;
}
14. WAP in C to calculate the age from the given date of birth
#include <stdio.h>
#include <stdlib.h>
// function to calculate current age
void age(int present_date, int present_month, int present_year, int birth_date, int
birth_month, int birth_year)
{
int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (birth_date > present_date)
{
present_date = present_date + month[birth_month - 1];
present_month = present_month - 1;
}
if (birth_month > present_month)
{
present_year = present_year - 1;
present_month = present_month + 12;
}
int final_date = present_date - birth_date;
int final_month = present_month - birth_month;
int final_year = present_year - birth_year;
printf("Present Age %d Years %d Months %d Days", final_year, final_month, fi-
nal_date);
}
int main()
{
int present_date, present_month, present_year,birth_date,birth_month,birth_year;
printf("Enter present date:");
scanf("%d",&present_date);
printf("Enter present month:");
scanf("%d",&present_month);
printf("Enter present year:");
scanf("%d",&present_year);
printf("Enter birth date:");
scanf("%d",&birth_date);
printf("Enter month:");
scanf("%d",&birth_month);
printf("Enter birth year:");
scanf("%d",&birth_year);
age(present_date, present_month, present_year, birth_date, birth_month,
birth_year);
return 0;
}
15.WAP in C to find Binary Addition and Binary Subtraction.
#include <stdio.h>
// function for Binary Addition
int binAddition(int a, int b)
{
int c; //carry
while (b != 0) {
//find carry and shift it left
c = (a & b) << 1;
//find the sum
a = a ^ b;
b = c;
}
return a;
}
// function for Binary Subtraction
int binSubtracton(int a, int b)
{
int carry;
//get 2's compliment of b and add in a
b = binAddition(~b, 1);
while (b != 0) {
//find carry and shift it left
carry = (a & b) << 1;
//find the sum
a = a ^ b;
b = carry;
}
return a;
}
int main()
{
int number1, number2, binAdd, binSub;
printf("Input first integer value: ");
scanf("%d", &number1);
printf("Input second integer value: ");
scanf("%d", &number2);
binAdd = binAddition(number1, number2);
binSub = binSubtracton(number1, number2);
printf("Binary Addition: %d\n", binAdd);
printf("Binary Subtraction: %d\n", binSub);
return 0;
}
16. WAP in C to reverse a string and check palindrome or not using functions.
#include<stdio.h>
#include<string.h>
void reverse(char *s,char *rv)
{
int i,j=0,l;
l=strlen(s);
for(i=l-1;i>=0;i--)
{
rv[j]=s[i];
j++;
}
rv[j]='\0';
printf("Reversed String %s",rv);
}
int main()
{
char str[50],rev[50];
// clrscr();
printf("\nEnter a string :");
scanf("%s",str);
reverse(str,rev);
if(strcmp(rev,str)==0)
printf("\n String %s is Palindrome ",str);
else
printf("\n String %s is not Palindrome ",str);
printf("\n");
return 0;
}
17. WAP in C to find the number of digits, vowels and consonants, word ,space in a given
string.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[150];
int vow,digit,sp,word,cons;
// initialize all variables to 0
word=0;
digit=0;
vow=0;
sp=0;
cons=0;
// get full line of string input
printf("Enter sentence: ");
//gets(line);
fgets(str, sizeof(str), stdin);
// loop through each character of the string
for (int i = 0; str[i] != '\0'; ++i)
{
// convert character to lowercase
str[i] = tolower(str[i]);
// check if the character is a vowel
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u')
{
// increment value of vowels by 1
++vow;
}
else if ((str[i] >= 'a' && str[i] <= 'z')) {
++cons;
}
// check if the character is a digit
else if (str[i] >= '0' && str[i] <= '9')
{
++digit;
}
// check if the character is an empty space
else if (str[i] == ' ')
{
++sp;
}
}
printf("Vowels: %d\n", vow);
printf("Consonants: %d\n",cons);
printf("Digits: %d", digit);
printf("\nWhite spaces: %d\n",sp);
printf("Words: %d\n",sp+1);
return 0;
}
18. Suppose you need to generate a result table which consists of student id, student name, marks
of three subjects and total marks. Write a program which takes input for ten students and displays
the result table. Also display student information separately who got the highest total. USE
STRUCTURES.
#include <stdio.h>
struct student
{
int s_id;
char s_name[50];
int m1,m2,m3,tot;
};
int main()
{
int i, total;
struct student s[4],t;
for(i=0; i<4; i++)
{
printf("enter the %d student's ID:\n",i);
scanf("%d",&s[i].s_id);
printf("enter the %d students name:\n",i);
scanf("%s",s[i].s_name);
printf("enter the %d students mark in maths:\n",i);
scanf("%d",&s[i].m1);
printf("enter the %d students mark in c:\n",i);
scanf("%d",&s[i].m2);
printf("enter the %d students mark in csa:\n",i);
scanf("%d",&s[i].m3);
s[i].tot = s[i].m1 + s[i].m2 + s[i].m3;
}
total=s[0].tot;
t=s[0];
for(i=0;i<4;i++)
{
if(s[i].tot>)
t=s[i];
}
printf("total marks obtained by%s %d %d %d %d\n\n\
n",t.s_name,t.m1,t.m2,t.m3,t.tot);
return 0;
}
19. Write program to read a file.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num;
FILE *fptr;
if((fptr=fopen(“home/user-1/terminal/add.c”,”r”)=NULL)
{
printf(“Error! Opening file”);
exit(1);
}
fscanf(fptr,”%d”,&num);
printf(“Value of Num=%d”,num);
return 0;
}
20. Write a program to write a sentence to file
#include<stdio.h>
#include<stdlib.h>
int main()
{
char sentence[1000];
FILE *fptr;
fptr=fopen(“Program.txt”,”w”)
if(fpr=NULL)
{
printf(“Error!”);
exit(1);
}
Print(“Enter a sentence:”);
fgets(sentence, sizeof(sentence),stdin);
fprintf(fptr,”%s”,sentence);
fclose(fptr);
return 0;
}