Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

C Program to Check Armstrong Number of three digits

Source code:

#include <stdio.h>
int main()
{
    int number, 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.",number);
    else
        printf("%d is not an Armstrong number.",number);

    return 0;
}


Output:

Enter a three digit integer: 371
371 is an Armstrong number.

C Program to Check Whether a Number is Positive or Negative

Source code:

#include <stdio.h>
int main()
{
    double number;

    printf("Enter a number: ");
    scanf("%lf", &number);

    if (number <= 0.0)
    {
        if (number == 0.0)
            printf("You entered 0.");
        else
            printf("You entered a negative number.");
    }
    else
        printf("You entered a positive number.");
    return 0;
}


Output 1:

Enter a number: 12.3
You entered a positive number.

Output 2:

Enter a number: 0
You entered 0.

C Program to Display Prime Numbers Between Intervals Using Function

Source code:

#include <stdio.h>

int checkPrimeNumber(int n);
int main()
{
    int n1, n2, i, flag;

    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);
    printf("Prime numbers between %d and %d are: ", n1, n2);

    for(i=n1+1; i<n2; ++i)
    {
        // i is a prime number, flag will be equal to 1
        flag = checkPrimeNumber(i);

        if(flag == 1)
            printf("%d ",i);
    }
    return 0;
}

// user-defined function to check prime number
int checkPrimeNumber(int n)
{
    int j, flag = 1;

    for(j=2; j <= n/2; ++j)
    {
        if (n%j == 0)
        {
            flag =0;
            break;
        }
    }
    return flag;
}


Output:

Enter two positive integers: 12
30
Prime numbers between 12 and 30 are: 13 17 19 23 29

C Program to Display Prime Numbers Between Two Intervals

Source code:

#include <stdio.h>
int main()
{
    int low, high, i, flag;
    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);

    printf("Prime numbers between %d and %d are: ", low, high);

    while (low < high)
    {
        flag = 0;

        for(i = 2; i <= low/2; ++i)
        {
            if(low % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            printf("%d ", low);

        ++low;
    }

    return 0;
}


Output:

Enter two numbers(intervals): 20 
50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47

C Program to Check Whether a Number is Prime or Not

Source code:

#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
    
    return 0;
}


Output:

Enter a positive integer: 29
29 is a prime number.

C Program to Calculate the Sum of Natural Numbers

Source code:

#include <stdio.h>
int main()
{
    int n, i, sum = 0;
    
    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=1; i <= n; ++i)
    {
        sum += i;   // sum = sum+i;
    }

    printf("Sum = %d",sum);

    return 0;
}


Output:

Enter a positive integer: 100
Sum = 5050

C Program to Display Factors of a Number

Source code:

#include <stdio.h>
int main()
{
    int number, i;

    printf("Enter a positive integer: ");
    scanf("%d",&number);

    printf("Factors of %d are: ", number);
    for(i=1; i <= number; ++i)
    {
        if (number%i == 0)
        {
            printf("%d ",i);
        }
    }

    return 0;
}

Output:


Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60

C Program to Check Whether a Number is Prime or Not

Source code:

#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i<=n/2; ++i)
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
    
    return 0;
}

Output:

Enter a positive integer: 29
29 is a prime number.

C Program to Check Leap Year

Source code:

#include <stdio.h>

int main()
{
    int year;

    printf("Enter a year: ");
    scanf("%d",&year);

    if(year%4 == 0)
    {
        if( year%100 == 0)
        {
            // year is divisible by 400, hence the year is a leap year
            if ( year%400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        }
        else
            printf("%d is a leap year.", year );
    }
    else
        printf("%d is not a leap year.", year);
    
    return 0;
}

Output 1:

Enter a year: 1900
1900 is not a leap year.

Output 2:

Enter a year: 2012
2012 is a leap year.

C Program to Calculate Area and Circumference of circle

Source Code:

#include<stdio.h>

int main() {

   int rad;
   float PI = 3.14, area, ci;

   printf("\nEnter radius of circle: ");
   scanf("%d", &rad);

   area = PI * rad * rad;
   printf("\nArea of circle : %f ", area);

   ci = 2 * PI * rad;
   printf("\nCircumference : %f ", ci);

   return (0);
}

Output:

Enter radius of a circle : 1
Area of circle : 3.14
Circumference  : 6.28

Write a program to print "Hello World"

Program : -


#include<stdio.h>            //Heder file
#include<conio.h>           //Heder file
void min()                        // Main function
{
    clrscr();                       // clear screen function
    printf("Hello World");  // Print Function
    getch();                      // getch is hold our output
}


Output :-

Hello World





C program to create singly link-list

Programs:-


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
     int data;
     struct node *next;
}*start=NULL;


void creat()
{
     char ch;
     do
     {
     struct node *new_node,*current;

     new_node=(struct node *)malloc(sizeof(struct node));
     
     printf("nEnter the data : ");
     scanf("%d",&new_node->data);
     new_node->next=NULL;

     if(start==NULL)
     {
          start=new_node;
          current=new_node;
      }
     else
     {
          current->next=new_node;
          current=new_node;
     }
     
      printf("Do you want to creat another : ");
      ch=getche();
      }while(ch!='n');
}
void display()
{
     struct node *new_node;
      printf("The Linked List : n");
      new_node=start;
      while(new_node!=NULL)
     {
          printf("%d--->",new_node->data);
           new_node=new_node->next;
       }
       printf("NULL");
}

void main()
{
     create();
     display();
}


Output : -


Enter the data : 10
Do you want to creat another :  y
Enter the data : 20
Do you want to creat another : y

Enter the data : 30
Do you want to creat another : n

The Linked List :
10--->20--->30--->NULL


C program to find the frequency of characters in a string

Program : - 

#include<stdio.h>
#include<conio.h>
void main()
{
       char c[1000],ch;
       int i,count=0;
       printf("Enter a string: ");
       gets(c);
       printf("Enter a character to find frequency: ");
       scanf("%c",&ch);
       for(i=0;c[i]!='\0';++i)
       {
               if(ch==c[i])
               ++count;
       }
       printf("Frequency of %c = %d", ch, count);
       getch();
}

 

Output : -

Enter a string: This website is awesome.
Enter a character to find frequency: e
Frequency of e = 4

 


Make a reverse half piramid using numbers

5 5 5 5 5 4 4 4 4 3 3 3 2 2 1



Program : -

#include<stdio.h> #include<conio.h> int main() { int i,j,rows; printf("Enter the number of rows :"); scanf("%d",&rows); for(i=rows;i>=1;--i) { for(j=1;j<=i;++j) { printf("%d ",i); } printf("\n"); } getch(); }

Output : -


Enter the number of rows :5

5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

C Program to print inverted half pyramid using number

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1




Program : -

#include<stdio.h> #include<conio.h> int main() { int i,j,rows; printf("Enter the number of rows :"); scanf("%d",&rows); for(i=rows;i>=1;--i) { for(j=1;j<=i;++j) { printf("%d ",j); } printf("\n"); } getch(); }

Output : -


Enter the number of rows :5

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1