Posts

Showing posts from August, 2018

Structures in C Language

Image
C Programming Structure In this article you will learn about structures in C language.What is it, how to define it and use it in your program. structure: It is a Collection of variables of different types under a single name.                                 struct student{  char name[50];  int age;  float percentage;  }; Here Struct  is the keyword used to define structure and student is the name of the structure. Creating Structure Variable:   struct student{               char name[50];  int age;  float percentage;  }struct student s1,s2,s3; Here S1,S2,S3 are the structure variables.We can access the members of the structures using these variables.  There are two types of operators used for accessing members of a structure. Member operator(.) Structure pointer operator(->) ...

Finding NCR and NPR.

Image
long find_ncr(int , int) long find_npr(int , int) long factorial(int) int main() {      int n,r;      long ncr,npr;      printf("Enter the value of n and r\n");      scanf("%d%d",n,r);      ncr = find_ncr(n,r);      npr = find_npr(n,r);  printf("%dC%d=%ld\n",n,r,ncr);  printf("%dC%d=%ld\n",n,r,npr); return 0; } long find_ncr(int n, int r) {       long result;              result = factorial(n)/factorial(n-r)*factorial(r);       return result; } long find_npr(int n, int r) {       long result;              result = factorial(n)/factorial(n-r);       return result; }  long factorial(int n) {    if(n==1||n==0)    return 1;    else    return n*f...

Rotating the elements of an array data structure by k positions to the right.

Image
#include <stdio.h> // K is no.of positions an array to be rotated int main() {     int k=2 , n=4,a[4]={1,2,3,4}, b[4],i; for(i=0;i<n;i++) {     b[(i+k)%n]=a[i]; } for(i=0;i<n;i++) {     printf("%d\t",b[i]); }     return 0; }

Array of Structures

Image
#include<stdio.h> #include<string.h> #define MAX 3 struct student {        char name[20];        int roll_no,i;        float marks; }; int main() {     struct student arr_student[MAX];     int i;     for(i=0;i<MAX;i++)     {        printf("\nEnter the details of student%d\n\n",i+1);              printf("Enter Name: ");        scanf("%s",&arr_student[i].name);              printf("Enter roll no: ");        scanf("%d",&arr_student[i].roll_no);        printf("Enter marks: ");        scanf("%f",&arr_student[i].marks);     }         printf("\n");       printf("Name\tRollno\tMarks\n");    for(i=0;i<MAX;i++)  ...

finding first smallest and the second smallest

Image
int main() {        int i,j,temp,x,y;        int a[]={16,70,8,100,9,85};        int n = sizeof(a)/sizeof(a[0]);          for(i=0;i<n-1;i++)          {                for(j=i+1;j<n-1;j++)                 {                        if(a[i]<a[j])                        {                            temp=a[i];                            a[i]=a[j];                            a[j] = temp;                     ...

Finding the second largest in an array

Image
int main() {        int i,j,temp,x,y;        int a[]={16,70,8,100,9,85};        int n = sizeof(a)/sizeof(a[0]);          for(i=0;i<n-1;i++)          {                for(j=i+1;j<n-1;j++)                 {                        if(a[i]>a[j])                        {                            temp=a[i];                            a[i]=a[j];                            a[j] = temp;                     ...

Finding the smallest and the largest element in an array

Image
#include <stdio.h> void main() {     int i;     int a[]={2,3,6,12,15,18};     int n = sizeof(a)/sizeof(a[0]);          int small = a[0],max=a[0];          for(i=0;i<n;i++)         {              if(a[i]>max)              max = a[i];              if(a[i]<small)             small = a[i];         }         printf("small is %d and large is %d",small,max); }

Finding the maximum element of an array

Image
#include <stdio.h> int findMaxElement(int[], int); int main() {     int a[] = {2,3,6,12,15,18};     int n = sizeof(a)/sizeof(a[0]);     int large = findMaxElement(a,n);     printf("%d",large);     return 0; } int findMaxElement(int a[],int n) {      int max= a[0];      int i;     for(i=0;i<n;i++)     {         if(max<a[i])         {            max = a[i];          }      }       return max; }

finding how many times a sorted array is rotated

First we should have a sorted array in order to compare with the input what we have given so we can know how many times it is sorted. a[] = {2,3,6,12,15,18}. int main() {       int a[]={15,18,2,3,6,12};       int n = sizeof(a)/sizeof(a[0]);       int count = countRotations(a,n);       printf("%d",count);       return 0; }     int countRotations(int a[],int n)     {         int min = a[0],x;         for(int i=0;i<n;i++)         {               if(min>a[i])              {                    min = a[i];                    x = i;               }         }   ...

Biggest Of Three Numbers Using Conditional operator/Ternary Operator in C

Image
int main() {        int a,b,c,max;        printf("Enter three numbers\n");        scanf("%d%d%d",&a,&b,&c);        max = a>b?(a>c?a:c):(b>c?b:c);        printf("%d",max);        return 0; }

Print "Hello World" without using semicolon.

Image
int main() { //using if loop if(printf("Hello World")){ } //using switch switch(printf("Hello World")){ } //using while while(!printf("Hello World")){ } return 0; }

Finding the roots of a Quadratic equation

Image
#include<math.h> int main() { float a=2,b=4,c=1; float d,root1,root2; d= b*b-4*a*c; if(d<0) {   printf("Roots are Complex\n");   printf("The roots of Quadratic expession are");   printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));     printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));       return 0;   }   else if(d==0){    printf("Both roots are equal.\n");    root1 = -b /(2* a);    printf("Root of quadratic equation is: %.3f ",root1);    return 0;   }   else{    printf("Roots are real numbers.\n");      root1 = ( -b + sqrt(d)) / (2* a);    root2 = ( -b - sqrt(d)) / (2* a);    printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);   }   return 0; }

power of a number

Here we are going to calculate the power of "4". int main() {     int x=4;  int z= power(5,4);  printf("%d",z);   return 0; } int power(int n,int y) {   if(n==0)   {     return 1;   }   return y*power(n-1,y); }

Strong Number

//A Number is said to be a strong number if factorial sum of its digits is equal to its number int main() { int n=145,k,s,sum=0,number; number =n;   while(n!=0)   {     s=n%10;     k=fact(s);     sum+=k;     n=n/10;   }   if(number==sum)   {     printf("The given number is a strong number");   }   return 0; } int fact(int x) {     if(x==1||x==0)   {     return 1;   }   x= x*fact(x-1);   return x; }

Armstrong Number

A Number is said to be an armstrong number if sum of cubes of its digits is equal to its number. int main() {   int k,sum=0,n1,n=371;   n1=n;   while(n!=0)   {     k=n%10;     k=k*k*k;     sum+=k;     n=n/10;   }   if(sum==n1)   {     printf("The given number is armstrong number");   }   return 0; }

program to check whether a number is a perfect number or not

A Number is said to be a perfect number which is equal to sum of its divisors int main() { int i,number =6,sum=0; for(i=1;i<number;i++) {   if(number%i==0)   {     sum+=i;   } } if(number==i) {   printf("The given number is a perfect number"); }   return 0; }