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
/* Program to print your name */#include<stdio.h>intmain() {
puts("~~~~~~~~~~~~~~");
puts("Rishab");
puts("~~~~~~~~~~~~~~");
return0;
}
Result:
2) To print College address.
/* College Address */#include<stdio.h>intmain() {
printf("\n\t\t\t -Guru Nanak Dev Engineering College,");
printf("\n\t\t\t -Gill Road,");
printf("\n\t\t\t -Ludhiana , Punjab\n");
return0;
}
Result:
3) Program to add two integers.
/* To add two integers */#include<stdio.h>intmain() {
inta,b;
printf("\nEnter the numbers....");
printf("\nA:");
scanf("%d",&a);
printf("\nB:");
scanf("%d",&b);
a=a+b;
printf("\n Sum of the number is %d \n",a);
return0;
}
Result:
4) Program to find quotient and remainder.
/* To find quotient and remainder */#include<stdio.h>intmain() {
inta,b,r,q;
printf("\nEnter the Dividend: ");
scanf("%d",&a);
printf("\nEnter the divisor: ");
scanf("%d",&b);
r=a%b;
q=a/b;
printf("\nRemainder: %d",r);
printf("\nQuotient: %d \n",q);
return0;
}
Result :
5) Program to swap two variables without 3rd variable.
/* Swapping without 3rd variable */#include<stdio.h>intmain() {
inta,b;
printf("Enter the value of A: ");
scanf("%d",&a);
printf("Enter the value of B: ");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nA: %d",a);
printf("\nB: %d"\n,b);
return0;
}
Result:
6) Program to check even odd number.
/* To find whether number is even or odd */#include<stdio.h>intmain() {
intnum;
printf("Enter the Number:");
scanf("%d",&num);
if(num%2==0)
printf("\nNumber is Even");
elseprintf("\nNumber is Odd\n");
printf("\n");
return0;
}
Result:
7) Finding greteast of two numbers.
/* Largest one in two */#include<stdio.h>intmain() {
inta,b;
printf("Enter any two number(A and B): ");
scanf("%d%d", &a,&b);
if(a>b)
printf("\nA is largest");
elseprintf("\nB is largest\n");
return0;
}
Result:
8) Find greatest of three number .
/* Largest of three number */#include<stdio.h>intmain() {
intx, y, z, large;
printf(" Enter any three integer numbers for x, y, z : ") ;
scanf("%d %d %d", &x, &y, &z) ;
large= (x>y ? ( x>z ? x : z) : (y>z ? y : z)) ;
printf("\n\n Largest or biggest or greatest or maximum among 3 numbers using Conditional ternary Operator : %d", large) ;
return0;
}
Result:
9) Program to assign grade to student according to percentage.
/* To find grade of a student by marks */#include<stdio.h>intmain() {
ints1,s2,s3,s4,s5,agg;
floatperc;
printf("Enter the Marks in 5 Subjects Respectively:\n");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
agg=s1+s2+s3+s4+s5; // Aggregate Marksperc=agg/500.0*100; // Perc Marksif(perc>=90)
{
printf("\nA\n");
}
elseif (perc>=80&&perc<90)
{
printf("\nB\n");
}
elseif(perc>=70&&perc<80)
{
printf("\nC\n");
}
elseif(perc>=60&&perc<70)
{
printf("\nD\n");
}
elseif(perc>=50&&perc<60)
{
printf("\nE\n");
}
else
{
printf("\nScope of Improvement\n");
}
return0;
}
Result:
10) Program to print roots of quadratic equation.
/*Program to print roots */#include<stdio.h>#include<stdlib.h>#include<math.h>intmain() {
floata, b, c, root1, root2;
floatrealp, imagp, disc;
printf("Enter the values of a, b and c \n");
scanf("%f %f %f", &a, &b, &c);
/* If a = 0, it is not a quadratic equation */if (a==0||b==0||c==0)
{
printf("Error: Roots cannot be determined \n");
exit(1);
}
else
{
disc=b*b-4.0*a*c;
if (disc<0)
{
printf("Imaginary Roots\n");
realp=-b / (2.0*a) ;
imagp=sqrt(abs(disc)) / (2.0*a);
printf("Root1 = %f +i %f\n", realp, imagp);
printf("Root2 = %f -i %f\n", realp, imagp);
}
elseif (disc==0)
{
printf("Roots are real and equal\n");
root1=-b / (2.0*a);
root2=root1;
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
}
elseif (disc>0 )
{
printf("Roots are real and distinct \n");
root1=(-b+sqrt(disc)) / (2.0*a);
root2=(-b-sqrt(disc)) / (2.0*a);
printf("Root1 = %f \n", root1);
printf("Root2 = %f \n", root2);
}
}
return0;
}
11) Program to check year is leap or not.
/* To find whether year is leap or not */#include<stdio.h>intmain() {
intyear,temp;
printf("Enter teh year: ");
scanf("%d",&year);
temp=year%4;
if(year%100==0)
{
if(year%400==0)
printf("\nLeap year...");
}
else
{
if(year%4==0)
printf("\nLeap year\n");
elseprintf("\nNot a Leap year\n");
}
return0;
}
Result:
12) Program to print table of 5.
/* Table of 5 */#include<stdio.h>intmain() {
inti,res;
for(i=0;i<=10;i++) {
res=5*i;
printf("5 X %d = %d\n",i,res);
}
return0;
}
Result:
13) To make simple calculator using switch case.
/* C Program to Create Simple Calculator using Switch Case */#include<stdio.h>intmain() {
charOperator;
floatnum1, num2, result=0;
printf("\n Please Enter an Operator (+, -, *, /) : ");
scanf("%c", &Operator);
printf("\n Please Enter the Values for two Operands: num1 and num2 : ");
scanf("%f%f", &num1, &num2);
switch(Operator)
{
case'+':
result=num1+num2;
break;
case'-':
result=num1-num2;
break;
case'*':
result=num1*num2;
break;
case'/':
result=num1 / num2;
break;
default:
printf("\n You have enetered an Invalid Operator ");
}
printf("\n The result of %.2f %c %.2f = %.2f\n", num1, Operator, num2, result);
return0;
}
Result:
14) To calculate reverse of a number.
/* To find reverse of a Number*/#include<stdio.h>intmain() {
intnum,rev=0;
printf("\nEnter the Number:");
scanf("%ld",&num);
while(num!=0)
{
rev=rev*10;
rev=rev+num%10;
num=num/10;
}
printf("\nReversed number:%d\n",rev);
printf("\n\n");
return0;
}
Result:
15) To check whether number is palindrome or not.
/* Palindrome */#include<stdio.h>intmain() {
intn,rev=0,check,rem;
printf("\nEnter the number:");
scanf("%d",&n);
check=n;
while( n!=0 )
{
rem=n%10;
rev=rev*10+rem;
n /= 10;
}
if(rev==check)
printf("\nNumber is a Pallindrome\n");
elseprintf("\nNumber is not a Pallindrome\n");
printf("\n\n");
return0;
}
Result:
16) To check whether a number is prime or not.
/* Program to check prime no. */#include<stdio.h>#include<stdlib.h>intmain() {
intnum, j, flag;
printf("Enter a number \n");
scanf("%d", &num);
if (num <= 1)
{
printf("%d is not a prime numbers \n", num);
exit(1);
}
flag=0;
for (j=2; j <= num / 2; j++)
{
if ((num % j) ==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number \n", num);
elseprintf("%d is not a prime number \n", num);
return0;
}
Result:
17) Program to print prime number to 100.
/* Prime number 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;
}
Result:
18) Program to check whether a number is armstrong or not.
/* To check armstrong number */#include<stdio.h>intmain()
{
intnumber, originalNumber, remainder, result=0;
printf("Enter a three digit integer: ");
scanf("%d", &number);
originalNumber=number;
while (originalNumber!=0)
{
remainder=originalNumber%10;
result+=remainder*remainder*remainder;
originalNumber /= 10;
}
if(result==number)
printf("%d is an Armstrong number.\n",number);
elseprintf("%d is not an Armstrong number\n.",number);
return0;
}
#include<stdio.h>intmain() {
intr,i,j,num=1;
printf("Enter number of rows: ");
scanf("%d",&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=i;++j)
{
printf("%d",num);
++num;
}
printf("\n");
}
return0;
}
####Result:
20) Program to find largest from 1 dimensional array.
/* Largest in 1 dimensional array */#include<stdio.h>intmain() {
inti, n;
floatarr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
printf("\n");
// Stores number entered by the userfor(i=0; i<n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
// Loop to store largest number to arr[0]for(i=1; i<n; ++i)
{
// Change < to > if you want to find the smallest elementif(arr[0] <arr[i])
arr[0] =arr[i];
}
printf("Largest element = %.2f", arr[0]);
return0;
}
Result:
21) To find sumof the N natural numbers in an array.
/* Sum of N no.s in array */#include<stdio.h>intmain() {
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
intn, sum=0, c, array[100];
printf("Enter the number of integers you want to add: ");
scanf("%d", &n);
printf("\n\nEnter %d integers \n\n", n);
for(c=0; c<n; c++)
{
scanf("%d", &array[c]);
sum+=array[c]; // same as sum = sum + array[c]
}
printf("\n\nSum = %d\n\n", sum);
return0;
}
Result:
22) Program to add two matrices .
/* Addition of matrix */#include<stdio.h>intmain(){
intr, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
// Adding Two matricesfor(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the resultprintf("\nSum of two matrices: \n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
return0;
}
Result:
23) Program to multiply two matrices .
/* Multiplation of matrices */#include<stdio.h>intmain()
{
inta[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] =0;
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j==c2-1)
printf("\n\n");
}
return0;
}
Result:
24) Program to check whether a string is palindrome or not .
#include<stdio.h>#include<string.h>intmain() {
chars[1000];
inti,n,c=0;
printf("Enter the string : ");
gets(s);
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome\n");
elseprintf("string is not palindrome\n");
return0;
}
Result:
25) Program to perform basic operations like lenghth of string ,string concat, string copy ,string compare and string reverse.
/*Fundamental string operation, lenth, concatenation, compare and copy strings without string.h */#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'; //adding null character at the end of input
}
/*returns 0 if thery are same otherwise returns 1*/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]; //string variables declaration with size 20intchoice;
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");
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case5:
exit(0);
}
}
return0;
}
Result:
26) Programs to swap two numbers using call by value and call by refernce.
/* Call by reference */#include<stdio.h>voidswap(int*, int*);
intmain() {
intx, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return0;
}
voidswap(int*a, int*b)
{
inttemp;
temp=*b;
*b=*a;
*a=temp;
}
Result:
call by value:-
/* Call by value */#include<stdio.h>voidswap(int, int);
intmain() {
intx, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(x, y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return0;
}
voidswap(inta, intb) {
inttemp;
temp=b;
b=a;
a=temp;
printf("Values of a and b is %d %d\n",a,b);
}
Result:
27) Program to calculate factorial of a number with and without recursion both.
/* Recursion */#include<stdio.h>long intmultiplyNumbers(intn);
intmain() {
intn;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld\n", n, multiplyNumbers(n));
return0;
}
long intmultiplyNumbers(intn)
{
if (n >= 1)
returnn*multiplyNumbers(n-1);
elsereturn1;
}
Result:
/* Without recrsion: */#include<stdio.h>intmain() {
intc, n, fact=1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &n);
for (c=1; c <= n; c++)
fact=fact*c;
printf("Factorial of %d = %d\n", n, fact);
return0;
}
Result:
28) Program to print fibonacci series with and without recursion both.
/* Program to print fibonaci series with recursion */#include<stdio.h>voidseries(int); //prototypeintmain() {
intn;
printf("\n\nEnter the number of terms you wish: ");
scanf("%d",&n);
printf("\n\n");
series(n); // function callprintf("\n\n\n");
return0;
}
voidseries(intn) // definition
{
intt1=0,t2=1,next;
for(inti=0;i<=n;i++)
{
printf("%d\n",t1);
next=t1+t2;
t1=t2;
t2=next;
}
}
Result:
// Fibonaci series without recursion:-#include<stdio.h>intfibo(int);
intmain() {
intnum;
intresult;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num<0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result=fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return0;
}
intfibo(intnum)
{
if (num==0)
{
return0;
}
elseif (num==1)
{
return1;
}
else
{
return(fibo(num-1) +fibo(num-2));
}
}
Result:
29) Program to calculate average of 5 numbers using function.
/* program to find average of 5 numbers */#include<stdio.h>intavg(int,int,int,int,int); // prototypeintmain() { inta1,a2,a3,a4,a5,res;
printf("\nEnter the numbers respectiively....");
scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);
res=avg(a1,a2,a3,a4,a5); // function callprintf("Average of the numbers %d",res);
return0;
}
intavg(inta1,inta2,inta3,inta4,inta5) // definition
{ intp;
p=(a1+a2+a3+a4+a5)/5;
returnp;
}
Result:
30) Program to implement linear serach and binary.
/* 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;
}
Result:
/* Binary Search */#include<stdio.h>intmain()
{
intc, first, last, middle, n, search, array[100];
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]);
printf("Enter value to find\n");
scanf("%d", &search);
first=0;
last=n-1;
middle= (first+last)/2;
while (first <= last) {
if (array[middle] <search)
first=middle+1;
elseif (array[middle] ==search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
elselast=middle-1;
middle= (first+last)/2;
}
if (first>last)
printf("Not found! %d isn't present in the list.\n", search);
return0;
}
Result:
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;
}
Result:
32) Program to store information of 10 students using array of structures.