Posts

Printing prime Number between a range 2 to 10

Image
int main() {   int i,j;   for(i=2;i<=10;i++)   {     for(j=2;j<=i;j++)     {       if(i%j==0)       {         break;       }     }     if(i==j)     {       printf("%d\t",j);     }   }   return 0; } OUTPUT:

Median

Image
/*Median of given numbers*/ #include <stdio.h> int main() {   int n=4,k,a[10]={1,2,3,4,5};   k=n/2;   if(n%2!=0)   {     printf("%d",a[k+1]);   }   else if(n%2==0)   {     int x= a[k]+a[(k/2)+1];     float y = x/2;     printf("%.2f",y);   }   return 0; }

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;                     ...