This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Thursday, September 19, 2019

Top c programming examples with output basic c programs

Today Discuss topic c programming examples with output then learn programming languages startup basic c programming. there are adding basic programs. addition two no of in c programming,palondrim program in c programming, reverse string program in c,

list of them a c programming following the c programs. Addition of two numbers,area of rectangle c program, even odd program in c programming,factorial of a number in c programming, Armstrong number in c programming, prime number program in c, palindrome program in c ,leap year in  c , reverse a string in c,if else in c program and nested if else cbasic programming list










c programming examples with output:


1. Addition of two numbers in C programming:



#include <stdio.h>
int main()
{
   int num1, num2, sum;
   printf("Enter first number: ");
   scanf("%d", &num1);
   printf("Enter second number: ");
   scanf("%d", &num2);

   sum = num1 + num2;
   printf("Sum of the entered numbers: %d", sum);
   return 0;
}


Output:

Enter first number: 20
Enter second number: 19
Sum of the entered numbers: 39





2.area of rectangle c program :


#include<stdio.h>
#include<conio.h>
 
int main() {
   int length, breadth, area;
 
   printf("\nEnter the Length of Rectangle : ");
   scanf("%d", &length);
 
   printf("\nEnter the Breadth of Rectangle : ");
   scanf("%d", &breadth);
 
   area = length * breadth;
   printf("\nArea of Rectangle : %d", area);
 
   return (0);
}

Output :

Enter the Length of Rectangle  : 5
Enter the Breadth of Rectangle : 4
Area of Rectangle : 20

3. even odd program in c programming:

#include <stdio.h>
int main()
{
  
    int num;
 
    printf("Enter an integer number: ");
    scanf("%d",&num);
 
    /*If number is divisible by 2 then number 
    is EVEN otherwise number is ODD*/
 
    if(num%2==0)
        printf("%d is an EVEN number.",num);
    else
        printf("%d is an ODD number.",num);
 
    return 0;
}


Output:


First Run:
    Enter an integer number: 123
    123 is an ODD number.

    Second Run:
    Enter an integer number: 110
    110  an EVEN number.



4.factorial of a number in c programming:


#include <stdio.h>
#include < conio.h > { <br> int n, i; <br> unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d",&n); // show error if the user enters a negative integer if (n < 0) printf("Error! Factorial of a negative number doesn't exist."); else { for(i=1; i<=n; ++i) { factorial *= i; // factorial = factorial*i; } printf("Factorial of %d = %llu", n, factorial); } return 0; }



Output:


Enter a number: 5
Factorial of 5 is: 120


5.armstrong number in c programming



#include < stdio.h >
#include < conio.h >
void main()
{
int number, sum = 0, rem = 0, cube = 0, temp;
printf ("enter a number");
scanf("%d", &number);
temp = number;
while (number != 0)
{
rem = number % 10;
cube = pow(rem, 3);
sum = sum + cube;
number = number / 10;
}
if (sum == temp)
printf ("The given no is armstrong no");
else
printf ("The given no is not a armstrong no");
}

Output:


enter the number=153
armstrong number



6.prime number program in c program:


#include <stdio.h>
int main()
{
   int i, num, p = 0;
   printf("Please enter a number: \n");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         p++;
      }
   }
   if(p==2)
   {
      printf("Entered number is %d "\
             "and it is a prime number.",num);
   }
   else
   {
      printf("Entered number is %d "\
             "and it is not a prime number.",num);
   }
}


Output:


Please enter a number: 13

Entered number is 13 and it is a prime number


7.palindrome program in c program :


#include <stdio.h>
int main()
{
   int number, t, rev=0, rmndr;

   printf("Please enter a number to check Palindrome : ");
   scanf("%d",&number);
   printf("\nEntered number: %d", number);
 
   t = number;

   while (number > 0)
   {
      rmndr = number%10;
      rev = rev*10 + rmndr;
      number = number/10;
   }
   printf("\nReversed number: %d", rev);

   if(t == rev)
   {
      printf("\nEntered number %d is a palindrome", t);
   }
   else
   {
      printf("\nEntered number %d is not a palindrome", t);
   }
   return 0;
}


Output:


Please enter a number to check Palindrome : 656

Entered number: 656

Reversed number: 656

Entered number 656 is a palindrome


8.leap year in c program :


#include <stdio.h>

int main() {
   int year;
   year = 2016;

   if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
      printf("%d is a leap year", year);
   else
      printf("%d is not a leap year", year);

   return 0;
}

Output:


2016 is a leap year



9.reverse a string in c program:


#include<stdio.h>
#include<conio.h>
void main()
{
    int i, j, k;
    char str[100];
    char rev[100];
    printf("Enter a string:\t");
    scanf("%s", str);
    printf("The original string is %s\n", str);
    for(i = 0; str[i] != '\0'; i++);
    {
        k = i-1;
    }
    for(j = 0; j <= i-1; j++)
    {
        rev[j] = str[k];
        k--;
    }
    printf("The reverse string is %s\n", rev);
    getch();
}


Output:


Enter a string: studytonight
The original string is studytonight
The reverse string is thginotyduts


10.if else in c program :


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

   /* local variable definition */
   int a = 100;
 
   /* check the boolean condition */
   if( a < 20 ) {
      /* if condition is true then print the following */
      printf("a is less than 20\n" );
   } else {
      /* if condition is false then print the following */
      printf("a is not less than 20\n" );
   }
   
   printf("value of a is : %d\n", a);
 
   return 0;
}


Output:


a is not less than 20;
value of a is : 100


11.nested if statements in c


#include <stdio.h>
int main()
{
   int var1, var2;
   printf("Input the value of var1:");
   scanf("%d", &var1);
   printf("Input the value of var2:");
   scanf("%d",&var2);
   if (var1 != var2)
   {
 printf("var1 is not equal to var2\n");
 //Nested if else
 if (var1 > var2)
 {
  printf("var1 is greater than var2\n");
 }
 else
 {
  printf("var2 is greater than var1\n");
 }
   }
   else
   {
 printf("var1 is equal to var2\n");
   }
   return 0;
}



Output:


Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1






No comments:

Post a Comment