Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

// GREATEST OF 3 NUMBERS

#include<stdio.h>
int main()
{
float a,b,c;

printf("Enter value of a,b and c: ");


scanf("%f\n%f\n%f",&a,&b,&c);

if(a>=b && a>=c)


{
printf("%.3f is the greatest number!",a);
}
else if(b>=c && b>=a)
{
printf("%.3f is the greatest number!",b);
}
else
{
printf("%.3f is the greatest number!",c);
}

return 0;
}
// FIBONACCI SERIES
#include<stdio.h>
int main()
{
int n1=0, n2=1, n3, i, no;

printf("Enter the number of elements you want in


the series: ");
scanf("%d",&no);

printf("The fibonacci series of first %d numbers


is as follows:\n",no);
printf("%d\n%d\n",n1,n2);

for( i=2; i<=no; i++ )


{
n3= n1 + n2;
printf("%d\n",n3);
n1=n2;
n2=n3;
}

return 0;
}
// PASCAL'S TRIANGLE USING NESTED FOR LOOPS (M-1)
#include<stdio.h>
int main()
{
int i, j, sp, row, c=1; /*i =
no. of rows
j =
maximum elements in a row
sp=
spacing
c =
value of element printed in triangle*/
printf("Enter the number of rows for Pascal's
Triangle: ");
scanf("%d",&row);

printf("Pascal's Triangle for %d rows is:\


n",row);

for( i=1; i<=row; i++ )


{
for( sp=1; sp<=(row-i); sp++ )
{
printf(" ");
}
for( j=1; j<=i; j++ )
{
if( i==1 || j==1 )
{
c=1;
}
else
{
c= ( c*(i-j+1) ) / (j-1) ;
}
printf("%4d",c);
}
printf("\n");
}
return 0;
}

/* PASCAL'S TRIANGLE (M-2)


#include<stdio.h>
int main()
{
int i, j, sp, row, c=1; i =
no. of rows
j =
maximum elements in a row
sp=
spacing
c =
value of element printed in triangle
printf("Enter the number of rows for Pascal's
Triangle: ");
scanf("%d",&row);

printf("Pascal's Triangle for %d rows is:\


n",row);

for( i=0; i<row; i++ )


{
for( sp=1; sp<=(row-i); sp++ )
{
printf(" ");
}
for( j=0; j<=i; j++ )
{
if( j==0 || i==0 )
{
c=1;
}
else
{
c= ( c*(i-j+1) ) / (j) ;
}
printf("%4d",c);
}
printf("\n");
}
return 0;
}
*/

// MENU DRIVEN ARITHMETIC OPERATIONS


#include<stdio.h>
int main()
{
int a,b,c;
int choice;
printf("Enter a:");
scanf("%d",&a);
printf("Enter b:");
scanf("%d",&b);
printf("You can perform the following
operations:\n1-Sum\n2-Difference\n3-Product\n4-
Division\n5-Remainder\nEnter your choice: \n");
scanf("%d", &choice);
switch(choice)
{
case 1: c=a+b;
printf("Sum= %d",c);
break;
case 2: c=a-b;
printf("Difference= %d",c);
break;
case 3: c=a*b;
printf("Product= %d",c);
break;
case 4: c=a/b;
printf("Division= %d",c);
break;
case 5: c=a%b;
printf("Remainder= %d",c);
break;
default:
printf("Invalid Choice!");
}
return 0;
}

// ARMSTRONG NUMBER USING FUNCTION


#include<stdio.h>
int arm(int n)
{
int rem,sum=0,d,num;
d=n;

while(n>0)
{
rem = n%10;
sum = sum + rem*rem*rem;
n = n/10;
}
if(sum == d )
{
printf("%d is an Armstrong number!",d);
}
else
{
printf("%d is not an Armstrong number!",d);
}
}
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
arm(num);
return 0;
}

// GCD USING RECURSION


#include<stdio.h>
int gcd(int n1, int n2)
{
if(n2==0)
{
return n1;
}
else
{
return gcd(n2,n1%n2);
}
}
int main()
{
int n1,n2;
printf("Enter the two numbers you want to find
the gcd of:\n");
scanf("%d %d",&n1,&n2);

printf("GCD of %d and %d is:


%d",n1,n2,gcd(n1,n2));

// SORTING 10 NUMBERS IN ASCENDING ORDER


#include<stdio.h>
int main()
{
int temp, i, j;
int a[11];
printf("Enter any 10 numbers:\n");

for(i=0; i<10; i++)


{
scanf("%d", &a[i]);
}
for(i=0;i<10;++i)
{
for(j=i+1; j<10; ++j)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Sorting all numbers in ascending order:
");

for(i=0; i<10; ++i)


{
printf("%d\t",a[i]);
}
return 0;
}

// TRANSPOSE OF MATRIX USING FUNCTION


#include<stdio.h>
void transpose(int A[10][10],int B[10][10],int r,int
c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
B[j][i]=A[i][j];
}
}
}
int main()
{
int A[10][10], B[10][10], r,c,i,j;
printf("Enter the number of rows and columns:\
n");
scanf("%d\t%d",&r,&c);

printf("Enter the Matrix elements:\n");


for(i=0;i<r;i++)
//Input Matrix
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
}
}

transpose(A,B,r,c);
//Calling Tranpose Function

printf("Transpose of Matrix:\n");
for(i=0;i<c;i++)
//Printing the Transpose
{
for(j=0;j<r;j++)
{
printf("%d ",B[i][j]);
//Space between two columns
}
printf("\n");
}
return 0;
}

// STRING OPERATIONS
#include<stdio.h>
#include<string.h>
int main()
{
char str1[50],str2[50];

printf("Enter the first string: ");


scanf("%s",&str1);
printf("Enter the second string: ");
scanf("%s",&str2);
printf("String 1= %s\nString 2= %s",str1,str2);

printf("\nAfter strcmp: %d",strcmp(str1,str2));


// -1 (HELLO<WORLD since H is less than W)

strcat(str1," ");
strcat(str1,str2);
printf("\nAfter strcat: %s",str1);
// str1= HELLO WORLD str2=WORLD
printf("\nAfter strlwr: %s ",strlwr(str1));
// str1=hello world str2=world

printf("\nAfter strcpy: %s",strcpy(str1,str2));


// str1=WORLD str2=WORLD

return 0;
}

// STRING PALINDROME
#include<stdio.h>
#include<string.h>
int main()
{
char str1[10];
int l, i, count;

printf("Enter the string: ");


gets(str1);

l= strlen(str1);

for(i=0; i<l/2; i++)


{
if(str1[i]==str1[ l - 1 - i])
{
count++;
}
}
if(count==i)
{
printf("The given string is a Palindrome!");
}
else
{
printf("The given string is not a
Palindrome!");
}
return 0;
}

// STUDENT RECORDS USING STRUCTURE


#include<stdio.h>
struct student
{
char name[10],div[10];
int rollno, per;
};
int main()
{
int i,n;
struct student info[11]; //Defining an array
for structure of type employee to be used in loops
printf("Enter number of records of students you
want to enter:\n");
scanf("%d",&n);
printf("Enter Details of %d Students:\n",n);
for(i=0;i<n;i++) //LOOP TO READ DETAILS
{
printf("Enter Student Name: ");
scanf("%s",&info[i].name);
printf("Enter Student Roll Number: ");
scanf("%d",&info[i].rollno);
printf("Enter Student Percentage: ");
scanf("%d",&info[i].per);
printf("Enter Student Division: ");
scanf("%s",&info[i].div);
}

printf("\nStudent Details are as follows:\n");


for(i=0;i<n;i++) //LOOP TO READ DETAILS
{
printf("Student %d Details:\nName: %s\tRoll
No: %d\tPercentage: %d\tDivision: %s\n",i+1,
info[i].name, info[i].rollno, info[i].per,
info[i].div);
}

// SUM OF 10 NUMBERS USING POINTERS


#include<stdio.h>
int main()
{
int a[10], i;
int *ptr=a;
printf("Enter any 10 numbers:\n");

for(i=0;i<10;i++)
{
scanf("%d",&ptr[i]); // if
*ptr=a then &ptr=a (* anf & both represent address)
}

printf("Sum of 10 numbers: ");


int sum=0;
for(i=0;i<10;i++)
{
sum += *ptr;
//*ptr points to a[i]
ptr++;
//ptr++ increments a[i] by 1 and makes it point to
next element in array
}
printf("%d",sum);

return 0;
}

You might also like