You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<stdio.h>intmain()
{
intn1,n2,q,r;
printf("enter first number ");
scanf("%d",&n1);
printf("enter second number ");
scanf("%d",&n2);
q=n1/n2;
r=n1%n2;
printf("The quotient is %d\n",q);
printf("The remainder is %d\n",r);
return0;
}
Output:
5.Program to swap two numbers without using third variable:
#include<stdio.h>voidmain()
{
inta,b;
printf("enter value of a ");
scanf("%d",&a);
printf("enter value of b ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("swapped values are a=%d b=%d ",a,b);
}
Output:
6.Program to check if number is even or odd:
#include<stdio.h>voidmain()
{
intnum;
printf("enter the number ");
scanf("%d",&num);
if(num%2==0)
{
printf("number is even ");
}
else
{
printf("number is odd ");
}
}
Output:
7.Program to find greater of two numbers:
#include<stdio.h>voidmain()
{
intnum1,num2;
printf("enter two numbers ");
scanf("%d",&num1);
scanf("%d",&num2);
if(num1>num2)
{
printf("%d is greater ",num1);
}
else
{
printf("%d is greater ",num2);
}
}
Output:
8.Program to find greater of three numbers:
#include<stdio.h>voidmain()
{
intnum1,num2,num3;
printf("enter first number ");
scanf("%d",&num1);
printf("enter second number ");
scanf("%d",&num2);
printf("enter third number ");
scanf("%d",&num3);
if((num1>num2)&&(num1>num3))
{
printf("%d is greater ",num1);
}
elseif((num2>num1)&&(num2>num3))
{
printf("%d is greater ",num2);
}
else
{
printf("%d is greater ",num3);
}
}
Output:
9.Program to find grade of student according to marks entered:
#include<stdio.h>#include<math.h>voidmain()
{
inta,b,c;
floatroot1,root2;
printf("enter value of a ");
scanf("%d",&a);
printf("enter value of b ");
scanf("%d",&b);
printf("enter value of c ");
scanf("%d",&c);
intdisc;
disc=b*b-4*a*c;
if(disc<0)
{
printf("Roots does not exist \n");
}
elseif(disc==0)
{
root1=-b/(2*a);
root2==root1;
}
else
{
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
}
printf("Roots of quadratic equation are %f %f\n",root1,root2);
}
Output:
11.Program to check if year is leap or not:
#include<stdio.h>voidmain()
{
long intyr;
printf("enter the year ");
scanf("%d",&yr);
if(yr%4==0)
{
printf("%d is leap year ",yr);
}
else
{
printf("%d is not leap year ",yr);
}
}
#include<stdio.h>intmain()
{
inta,b;
charo;
printf("enter +,-,* or / ");
scanf("%c", &o);
printf("enter first number ");
scanf("%d",&a);
printf("enter second number ");
scanf("%d",&b);
switch(o)
{
case'+':
printf("sum is %d ",a+b);
break;
case'-':
printf("subtraction is %d ",a-b);
break;
case'*':
printf("multiplication is %d ",a*b);
break;
case'/':
printf("division is %d ",a/b);
break;
default:
printf("error\n");
}
return0;
}
Output:
14.Program to calculate reverse of a number:
#include<stdio.h>intmain()
{
intn,remainder,reverse=0;
printf("enter the number ");
scanf("%d",&n);
while(n!=0)
{
remainder=n%10;
reverse=reverse*10+remainder;
n=n/10;
}
printf("reversed number is %d\n",reverse);
return0;
}
}
Output:
15.Program to check if a number is palandrome or not:
#include<stdio.h>voidmain()
{
intn,remainder,reverse=0,originalnum;
printf("enter number ");
scanf("%d",&n);
originalnum=n;
while(n!=0)
{
remainder=n%10;
reverse=reverse*10+remainder;
n=n/10;
}
if(originalnum==reverse)
{
printf("%d is a palandrome\n", reverse);
}
else
{
printf("%d is not a palandrome\n", reverse);
}
}
Output:
16.Program to check if a number is prime or not:
#include<stdio.h>voidmain()
{
intnum,prime,flag;
printf("Enter the Number ");
scanf("%d",&num);
if(num==1)
{
printf("1 is neither prime nor composite");
}
if(num%2==0)
{
flag=0;
}
else
{
flag=1;
}
if(flag==1)
{
printf("%d is prime ",num);
}
else
{
printf("%d is composite ",num);
}
}
Output:
17.Program to print prime numbers from 1 to 100:
#include<stdio.h>intmain()
{
intnumbr,k,remark;
printf(" The prime numbers between 1 and 100 : \n");
for(numbr=2;numbr<=100;++numbr)
{
remark=0;
for(k=2;k<=numbr/2;k++){
if((numbr % k) ==0){
remark++;
break;
}
}
if(remark==0)
printf("\n %d ",numbr);
}
return0;
}
Output:
18.Program to check whether a number is armstrong or not:
#include<stdio.h>#include<math.h>intmain()
{
intnumber, originalNumber, remainder, result=0, n=0 ;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber=number;
while (originalNumber!=0)
{
originalNumber /= 10;
++n;
}
originalNumber=number;
while (originalNumber!=0)
{
remainder=originalNumber%10;
result+=pow(remainder, n);
originalNumber /= 10;
}
if(result==number)
printf("%d is an Armstrong number.\n", number);
elseprintf("%d is not an Armstrong number.\n", number);
return0;
}
24.Program to check if string is palandrome or not:
#include<stdio.h>#include<string.h>intmain(){
charstring1[20];
inti, length;
intflag=0;
printf("Enter a string:");
scanf("%s", string1);
length=strlen(string1);
for(i=0;i<length ;i++){
if(string1[i] !=string1[length-i-1]){
flag=1;
break;
}
}
if (flag) {
printf("%s is not a palindrome\n", string1);
}
else {
printf("%s is a palindrome\n", string1);
}
return0;
}
Output:
25.Program to perform basic operations like length of string,string concat,string copy,string compare and string reverse:
#include<stdio.h>#include<stdlib.h>intfind_length(charstring[]) {
intlen=0, i;
for (i=0; string[i] !='\0'; i++) {
len++;
}
returnlen;
}
voidjoin_strings(charstring1[], charstring2[]) {
inti, len1, len2;
len1=find_length(string1);
len2=find_length(string2);
for (i=len1; i<len1+len2; i++) {
string1[i] =string2[i-len1];
}
string1[i] ='\0';
}
intcompare_strings(charstring1[], charstring2[]) {
intlen1, len2, i, count=0;
len1=find_length(string1);
len2=find_length(string2);
if (len1!=len2)
return1;
for (i=0; i<len1; i++) {
if (string1[i] ==string2[i])
count++;
}
if (count==len1)
return0;
return1;
}
voidcopy_string(chardestination[], charsource[]) {
intlen, i;
len=find_length(source);
for (i=0; i<len; i++) {
destination[i] =source[i];
}
destination[i] ='\0';
}
intmain() {
charstring1[20], string2[20];
intchoice;
while (1) {
printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch (choice) {
case1:
printf("Enter the string: ");
scanf("%s", string1);
printf("The length of string is %d", find_length(string1));
break;
case2:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
join_strings(string1, string2);
printf("The concatenated string is %s", string1);
break;
case3:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
if (compare_strings(string1, string2) ==0) {
printf("They are equal");
} else {
printf("They are not equal");
}
break;
case4:
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 = %s\n",string1);
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case5:
exit(0);
}
}
return0;
}
Output:
26.Program to swap two numbers using call by value and call by reference:
A.Call by value
#include<stdio.h>voidswap(intx,inty);
voidmain()
{
inta,b;
printf("enter value of a ");
scanf("%d",&a);
printf("\n");
printf("enter value of b ");
scanf("%d",&b);
printf("\n");
swap(a,b);
}
voidswap(intx,inty)
{
intc;
if(x==y)
{
printf("swapped numbers are %d%d",x,y);
}
else
{
c=x;
x=y;
y=c;
printf("after swapping \n");
printf("value of a %d\n",x);
printf("value os b %d\n",y);
}
}
Output:
B.Call by reference
#include<stdio.h>voidswap(int*a,int*b);
voidmain()
{
inta,b;
printf("enter value of a ");
scanf("%d",&a);
printf("enter value of b ");
scanf("%d",&b);
swap(&a,&b);
printf("after swapping\nvalue of a=%d\nvalue of b=%d\n",a,b);
}
voidswap(int*a,int*b)
{
inttemp;
temp=*a;
*a=*b;
*b=temp;
}
Output:
27.Program to calculate factorial of a number with and without recursion:
A.Without
Recursion
#include<stdio.h>intfact(intm);
voidmain()
{
inta,func;
printf("enter the number ");
scanf("%d",&a);
func=fact(a);
printf("facorial is %d\n",func);
}
intfact(intm)
{
intsum=1;
for(inti=1;i<=m;++i)
{
sum=sum*i;
}
returnsum;
}
30.Program to implement linear search and binary search:
A.Linear search
#include<stdio.h>intmain()
{
intarray[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c=0; c<n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c=0; c<n; c++)
{
if (array[c] ==search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c==n)
printf("%d isn't present in the array.\n", search);
return0;
}
Output:
B.Binary Search
#include<stdio.h>voidmain()
{
intn,arr[20],search;
printf("enter amount of numbers ");
scanf("%d",&n);
for(inti=0;i<n;i++)
{
printf("enter values in array ");
scanf("%d",&arr[i]);
}
printf("enter the element to search ");
scanf("%d",&search);
intfirst=0;
intlast=n-1;
intmiddle=(first+last)/2;
while(first<=last)
{
if(search>arr[middle])
{
first=first+1;
}
elseif(search==arr[middle])
{
printf("element found at %drd position \n",middle+1);
break;
}
else
{
last=last-1;
}
middle=(first+last)/2;
}if(first>last)
{
printf("element not found ");
}
}
Output:
31.Program to implement bubble sort:
#include<stdio.h>intmain()
{
intarray[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c=0; c<n; c++)
scanf("%d", &array[c]);
for (c=0 ; c<n-1; c++)
{
for (d=0 ; d<n-c-1; d++)
{
if (array[d] >array[d+1])
{
swap=array[d];
array[d] =array[d+1];
array[d+1] =swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c=0; c<n; c++)
printf("%d\n", array[c]);
return0;
}
Output:
32.Program to store information of 10 students using array of structures: