//Program to print multiplication table up to n numbers
import java.util.*;
class mtable
{
public static void main(String args[])
{
int n,m,i,k;
System.out.println(" Up to which number you want to print tables:");
Scanner obj=new Scanner(System.in);
m=obj.nextInt();
System.out.println("\n Tables upto "+m);
for(n=1;n<=m;n++)
{
for(i=1;i<=10;i++)
{
System.out.println("\t"+n+" X "+i+" = "+n*i);
}
System.out.println("\n");
}
}
}
30 August 2018
Java Program to print multiplication table up to n numbers
Java Program to print multiplication table of a given number.
//Program to print multiplication table of a given number.
import java.util.*;
class table
{
public static void main(String args[])
{
int n,i;
System.out.println(" Enter a number to find its multiplication table :");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
System.out.println("Table of "+n);
for(i=1;i<=10;i++)
{
System.out.println("\t"+n+" X "+i+" = "+n*i);
}
System.out.println( );
}
}
Java Program to sort a list of integers in ascending order
//Program to sort a list of integers in ascending order
import java.util.*;
class ascend
{
public static void main(String args[])
{
int n,i,j,temp;
int a[]=new int[50];
System.out.println(" How many numbers you want to sort in Ascending order:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
System.out.println(" Enter elements");
for(i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
System.out.println(" unsorted array : ");
for(i=0;i<n;i++)
{
System.out.print("\t "+a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("\n sorted array in ascending order ");
for(i=0;i<n;i++)
{
System.out.print("\t "+a[i]);
}
System.out.println( );
}
}
Java Program to sort a list of integers in descending order
//Program to sort a list of integers in descending order
import java.util.*;
class descend
{
public static void main(String args[])
{
int n,i,j,temp;
int a[]=new int[50];
System.out.println(" How many numbers you want to sort in descending order:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
System.out.println(" Enter elements");
for(i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
System.out.println(" unsorted array : ");
for(i=0;i<n;i++)
{
System.out.print("\t "+a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("\n sorted array in descending order ");
for(i=0;i<n;i++)
{
System.out.print("\t "+a[i]);
}
System.out.println( );
}
}
import java.util.*;
class descend
{
public static void main(String args[])
{
int n,i,j,temp;
int a[]=new int[50];
System.out.println(" How many numbers you want to sort in descending order:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
System.out.println(" Enter elements");
for(i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
System.out.println(" unsorted array : ");
for(i=0;i<n;i++)
{
System.out.print("\t "+a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("\n sorted array in descending order ");
for(i=0;i<n;i++)
{
System.out.print("\t "+a[i]);
}
System.out.println( );
}
}
29 August 2018
C++ Program to find average of n numbers
//Program to find average of n numbers
#include<iostream>
using namespace std;
int main()
{
int n,i,a[100];
float avg,sum=0;
cout<<"\n Of how many number you want to find average:";
cin>>n;
cout<<"\n Enter numbers:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/n;
cout<<"\n Average of "<<n<<" numbers is :"<<avg<<endl;
return 0;
}
C++ Program to check a given numbers is positive,negative (or) zero
//Program to check a given numbers is positive,negative (or) zero
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"\n Enter a number:";
cin>>n;
if(n<0)
cout<<"\n Entered number is negative "<<endl;
else if(n==0)
cout<<"\n Entered number is Zero "<<endl;
else
cout<<"\n Entered number is positive"<<endl;
return 0;
}
28 August 2018
Java Program to calculate the discriminant of quadratic equation
//Program to calculate the discriminant of quadratic equation
import java.util.*;
class quadratic
{
public static void main(String args[] )
{
double a,b,c,k;
Scanner obj=new Scanner(System.in);
System.out.println("Enter Co-efficients value");
a=obj.nextDouble();
b=obj.nextDouble();
c=obj.nextDouble;
k=b*b-4*a*c;
if(k<0)
System.out.println("\n It has no real solutions");
else if(k>0)
System.out.println("\n It has two real solutions");
else
System.out.println("\n It has one real solution");
}
}
Java Program to multiply two matrices
//Program to multiply two matrices
import java.util.*;
class matrixmul
{
public static void main(String args[] )
{
int m,n,p,q,i,j,k;
int [][]a =new int[10][10];
int [][]b=new int[10][10];
int [][]c=new int[10][10];
Scanner obj=new Scanner(System.in);
System.out.println("Enter row and column of 1st matrix");
m=obj.nextInt();
n=obj.nextInt();
System.out.println("Enter row and column of 2nd matrix");
p=obj.nextInt();
q=obj.nextInt();
if(n!=p)
System.out.println("Matrix multiplication is not possible");
else
System.out.println("Enter 1st matrix");//reading 1st matrix
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=obj.nextInt();
}
}
System.out.println("Enter 2nd matrix");//reading 2nd matrix
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
b[i][j]=obj.nextInt();
}
}
System.out.println("\t 1st matrix is : \n ");//printing 1st matrix
for(i=0;i<m;i++)
{
if(i==m-1)
{
System.out.println(" ");
}
for(j=0;j<n;j++)
{
System.out.print(" \t "+a[i][j]);
}
}
System.out.println("\n\t 2nd matrix is : \n");//Printing 2nd matrix
for(i=0;i<p;i++)
{
if(i==p-1)
{
System.out.println(" ");
}
for(j=0;j<q;j++)
{
System.out.print(" \t "+b[i][j]);
}
}
System.out.println("\n Product of 1st and 2nd matrix :");//multiplying the matrices
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
for(i=0;i<m;i++)//printing resultant matrix
{
if(i==m-1)
{
System.out.println(" ");
}
for(j=0;j<q;j++)
{
System.out.print("\t "+c[i][j]);
}
}
}
}
import java.util.*;
class matrixmul
{
public static void main(String args[] )
{
int m,n,p,q,i,j,k;
int [][]a =new int[10][10];
int [][]b=new int[10][10];
int [][]c=new int[10][10];
Scanner obj=new Scanner(System.in);
System.out.println("Enter row and column of 1st matrix");
m=obj.nextInt();
n=obj.nextInt();
System.out.println("Enter row and column of 2nd matrix");
p=obj.nextInt();
q=obj.nextInt();
if(n!=p)
System.out.println("Matrix multiplication is not possible");
else
System.out.println("Enter 1st matrix");//reading 1st matrix
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=obj.nextInt();
}
}
System.out.println("Enter 2nd matrix");//reading 2nd matrix
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
b[i][j]=obj.nextInt();
}
}
System.out.println("\t 1st matrix is : \n ");//printing 1st matrix
for(i=0;i<m;i++)
{
if(i==m-1)
{
System.out.println(" ");
}
for(j=0;j<n;j++)
{
System.out.print(" \t "+a[i][j]);
}
}
System.out.println("\n\t 2nd matrix is : \n");//Printing 2nd matrix
for(i=0;i<p;i++)
{
if(i==p-1)
{
System.out.println(" ");
}
for(j=0;j<q;j++)
{
System.out.print(" \t "+b[i][j]);
}
}
System.out.println("\n Product of 1st and 2nd matrix :");//multiplying the matrices
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
for(i=0;i<m;i++)//printing resultant matrix
{
if(i==m-1)
{
System.out.println(" ");
}
for(j=0;j<q;j++)
{
System.out.print("\t "+c[i][j]);
}
}
}
}
27 August 2018
Java Program to find sum of digits of a given number
//Program to find sum of digits of a given number
import java.util.*;
class sumofd
{
public static void main(String args[])
{
int a,r,num,sum=0;
System.out.println(" Enter a number:");
Scanner obj=new Scanner(System.in);
a=obj.nextInt();
if(a<0)
num=-a;
else
num=a;
while(num>0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
if(a<0)
System.out.println(" sum of digits = -"+sum);
else
System.out.println(" sum of digits = "+sum);
}
}
Java Program to find a given number is even (or) odd.
// Program to find a given number is even (or) odd.
import java.util.*;
class evenodd
{
public static void main(String args[])
{
int n,i,is_even=0;
System.out.println(" Enter a number : ");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
for(i=2;i<n;i++)
{
if(n%2==0)
is_even=1;
}
if(is_even==1)
System.out.println(n+" is an even ");
else
System.out.println(n+" is an odd ");
}
}
Java Program to calculate cube(^3) of a given number
//Program to calculate cube(^3) of a given number
import java.util.*;
class cube
{
public static void main(String args[])
{
int n,cube;
System.out.println("\n Enter a number to find its cube:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
cube=n*n*n;
System.out.println("\n\t"+n+" ^ 3 = "+cube);
System.out.println();
}
}
Java Program to find a given number is Composite (or) not.
//Program to find a given number is Composite (or) not.
import java.util.*;
class composite
{
public static void main(String args[])
{
int a,i,count=0;
System.out.println(" Enter a number:");
Scanner obj=new Scanner(System.in);
a=obj.nextInt();
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count>2)
System.out.println(a+" is a composite number ");
else
System.out.println(a+" is a not a composite number ");
}
}
Java Program to check a given number is positive/negative/zero
//Program to check a given number is positive/negative/zero
import java.util.*;
class check
{
public static void main(String args[])
{
int n;
System.out.println(" Enter a number:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
if(n<0)
System.out.println(" Entered number is negative ");
else if(n==0)
System.out.println(" Entered number is Zero ");
else
System.out.println(" Entered number is positive ");
}
}
Java Program to find average of n numbers
//Program to find average of n numbers
import java.util.*;
class areaofc
{
public static void main(String args[])
{
int n,i;
int a[]=new int[50];
float avg,sum=0;
System.out.println(" Of how many number you want to find average:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
System.out.println(" Enter numbers:");
for(i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/n;
System.out.println(" Average of "+n+" numbers is : "+avg);
}
}
Java Program to find area and perimeter of a rectangle
//Program to find area and perimeter of a rectangle
import java.util.*;
class areaofr
{
public static void main(String args[])
{
int l,b,area;
System.out.println(" enter length of a rectangle :");
Scanner obj=new Scanner(System.in);
l=obj.nextInt();
System.out.println(" Enter breadth/width of a rectangle :");
b=obj.nextInt();
System.out.println(" Area of a rectangle : "+l*b);
System.out.println(" Perimeter of rectangle : "+2*(l+b));
}
}
Java Program to find area and perimeter of a square
//Program to find area and perimeter of a square
import java.util.*;
class areaofs
{
public static void main(String args[])
{
int a,area;
System.out.println(" Enter the side of a square :");
Scanner obj=new Scanner(System.in);
a=obj.nextInt();
System.out.println(" Area of a square : "+a*a);
System.out.println(" Perimeter of square : "+4*a);
}
}
Java Program to calculate area of circle using if-else statement
//Program to calculate area of circle using if-else statement
import java.util.*;
class areaofc
{
public static void main(String args[])
{
float n,r,d,area;
System.out.println(" You want to calculate area of circle using");
System.out.println(" 1.Radius \n 2.Diameter");
System.out.println(" Enter your choice:");
Scanner obj=new Scanner(System.in);
n=obj.nextFloat();
if(n==1)
{
System.out.println(" Enter radius:");
r=obj.nextFloat();
System.out.println(" Area of circle = "+3.14*2*r);
}
else if(n==2)
{
System.out.println(" Enter diameter:");
d=obj.nextFloat();
System.out.println(" Area of circle = "+3.14*d);
}
else
System.out.println(" Invalid choice ");
}
}
A similar program to this :
Java Program to calculate area of circle using SWITCH statement
Java Program to generate Fibonacci series
//Program to generate Fibonacci series
import java.util.*;
class fibonacci
{
public static void main(String args[])
{
int f1=1,f2=1,f3,n,i;
System.out.print("Enter how many numbers you want to generate in the series : ");
Scanner obj = new Scanner(System.in);
n=obj.nextInt();
if(n==1)
System.out.println(f1);
else if(n==2)
System.out.println(f1+" "+f2);
else if(n>=3)
{
System.out.print(f1+" "+f2);
for(i=3;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
System.out.print(" "+f3);
}
}
}
}
A similar program to this
Java Program to generate nth term in Fibonacci series
Java Program to find largest and smallest in a list of numbers
//Program to find largest and smallest in a list of numbers
import java.util.*;
class largesmall
{
public static void main(String args[])
{
int a []=new int[25];
int n,i,large,small;
System.out.println("How many numbers you want enter in the list");
Scanner obj =new Scanner(System.in);
n=obj.nextInt();
System.out.println("Enter the integers :");
for(i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
large=a[0];
small=a[0];
for(i=0;i<n;i++)
{
if(large<a[i])
large=a[i];
else if(small>a[i])
small=a[i];
}
System.out.println(" Large element : "+large);
System.out.println(" Small element : "+small);
}
}
Java Program to print prime numbers between 1 and n
//Program to print prime numbers between 1 and n
import java.util.*;
class nprime
{
public static void main(String args[])
{
int n,i,j;
int is_prime;
Scanner obj = new Scanner(System.in);
System.out.print(" Enter a number : ");
n=obj.nextInt();
System.out.println(" Prime numbers between 1 and "+n+" are :");
for(j=2;j<=n;j++)
{
is_prime=1;
for(i=2;i<j;i++)
{
if(j%i==0)
is_prime=0;
}
if(is_prime==1)
System.out.print(" "+j+" ");
}
}
}
Similar program(s):
C++ Program to find a given number is Prime (or) not.
C++ Program to print prime factors of given number
Java Program to check given number is prime (or) not
//Program to check given number is prime (or) not
import java.util.*;
class prime
{
public static void main(String args[])
{
int n,is_prime=1;
System.out.print("Enter a number : ");
Scanner obj = new Scanner(System.in);
n=obj.nextInt();
for(int i=2;i<n;i++)
{
if(n%i==0)
is_prime=0;
}
if(is_prime==1)
System.out.println("It is Prime");
else
System.out.println("It is NOT Prime");
}
}
Similar program(s):
C++ Program to print prime factors of given number
C++ Program to find prime number between 1 and n
24 August 2018
C Program to find prime number between 1 and n
//Program to find prime number between 1 and n
#include<stdio.h>
int main()
{
int n,m,i=2,is_prime=1;
printf("Enter up to which number you want to find prime numbers:");
scanf("%d",&m);
for(n=2;n<=m;n++)
{
for(i=2;i<n;i++)
{
if(n%i==0)
{
is_prime=0;
break;
}
else
is_prime=1;
}
if(is_prime)
printf(" %d ",n);
}
printf("\n");
return 0;
}
#include<stdio.h>
int main()
{
int n,m,i=2,is_prime=1;
printf("Enter up to which number you want to find prime numbers:");
scanf("%d",&m);
for(n=2;n<=m;n++)
{
for(i=2;i<n;i++)
{
if(n%i==0)
{
is_prime=0;
break;
}
else
is_prime=1;
}
if(is_prime)
printf(" %d ",n);
}
printf("\n");
return 0;
}
C Program to print cube(s) from 1 to n
//Program to print cube(s) from 1 to n
#include<stdio.h>
int main()
{
int n,i,cube;
printf("\n Enter up to which number you want to find cube(s):");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
cube=i*i*i;
printf("\n\t %d ^ 3 = %d",i,cube);
}
printf("\n");
return 0;
}
#include<stdio.h>
int main()
{
int n,i,cube;
printf("\n Enter up to which number you want to find cube(s):");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
cube=i*i*i;
printf("\n\t %d ^ 3 = %d",i,cube);
}
printf("\n");
return 0;
}
C Program to print composite numbers between 1 to n.
//Program to print composite numbers between 1 to n.
#include<stdio.h>
int main()
{
int a,i,j,count=0;
printf("\n enter up to which number you want to find Composite numbers:");
scanf("%d",&a);
for(j=1;j<=a;j++)
{
count=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
count++;
}
if(count>2)
printf(" %d ",j);
}
printf("\n");
return 0;
}
#include<stdio.h>
int main()
{
int a,i,j,count=0;
printf("\n enter up to which number you want to find Composite numbers:");
scanf("%d",&a);
for(j=1;j<=a;j++)
{
count=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
count++;
}
if(count>2)
printf(" %d ",j);
}
printf("\n");
return 0;
}
C Program to find sum of n natural numbers
//Program to find sum of n natural numbers
#include<stdio.h>
int main()
{
int n,sum;
printf("\n enter a natual number:");
scanf("%d",&n);
sum=n*(n+1)/2;
printf("\n sum of %d natural numbers is : %d \n",n,sum);
return 0;
}
C Program to find FACTORS of a given number
//Program to find FACTORS of a given number
#include<stdio.h>
int main()
{
int a;
printf("\n enter a positive number to find factors:");
scanf("%d",&a);
printf("\n Factors of %d are : ",a);
for(int i=1;i<=a;i++)
{
if(a%i==0)
printf(" %d ",i);
}
printf("\n");
return 0;
}
Similar program(s):
C++ Program to print prime factors of given number
C Program to factorial of a number
//Program to factorial of a number
#include<stdio.h>
int main()
{
int n,i,f;
printf("\n enter a number to find factorial:");
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d is : %d \n",n,f);
return 0;
}
23 August 2018
C Program to calculate square of a given number
//Program to calculate square(^2) of a given number
#include<stdio.h>
int main()
{
int n,square;
printf("\n Enter a number to find its square:");
scanf("%d",&n);
square=n*n;
printf("\n\t %d ^ 2 = %d \n",n,square);
return 0;
}
C Program to find BIG and SMALL number among two given numbers.
//Program to find BIG&SMALL number among two given numbers.
#include<stdio.h>
int main()
{
int a,b,max,min;
printf("\n Enter 1st number:");
scanf("%d",&a);
printf("\n Enter 2nd number:");
scanf("%d",&b);
max=(a>b)?a:b;
min=(a<b)?a:b;
printf("\n Big number = %d",max);
printf("\n Small number = %d \n",min);
return 0;
}
C Program to find a given number is prime(or)composite.
//Program to find a given number is prime(or)composite.
#include<stdio.h>
int main()
{
int a,i,count=0;
printf("\n enter a positive number:");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count==1)
printf("\n 1 is neither Prime nor Composite \n");
else if(count==2)
printf("\n It is a prime number \n");
else if(count>2)
printf("\n It is a composite number \n");
return 0;
}
C Program to check given string is palindrome (or) not.
//Program to check given string is palindrome (or) not.
#include<stdio.h>
#include<string.h>
int main()
{
char s[50];
printf("\n enter a string : ");
scanf("%s",s);
int i=0,j,is_palin=1;
j=strlen(s)-1;
while(i<j)
{
if(s[i]!=s[j])
{
is_palin=0;
break;
}
else
{
++i;
--j;
}
}
if(is_palin)
printf("\n It is palindrome \n");
else
printf("\n It is not palindrome \n");
return 0;
}
C Program to generate Fibonacci series
//Program to generate Fibonacci series
#include<stdio.h>
int main()
{
int n,i,f1=0,f2=1,f3;
printf("\n Enter how many numbers you want to generate in Fibonacci series:");
scanf("%d",&n);
printf("\n The Fibonacci series is \n");
printf("\n\t");//Spacing for output
if(n==1)
printf("%d",f1);
else if(n==2)
printf("%d %d",f1,f2);
else
if(n>2)
{
printf("%d %d ",f1,f2);
for(i=3;i<=n;i++)
{
f3=f2+f1;
f1=f2;
f2=f3;
printf(" %d ",f3);
}
}
printf("\n");
return 0;
}
C Program to calculate cube(^3) of a given number
//Program to calculate cube(^3) of a given number
#include<stdio.h>
int main()
{
int n,cube;
printf("\n Enter a number to find its cube:");
scanf("%d",&n);
cube=n*n*n;
printf("\n\t %d ^ 3 = %d",n,cube);
printf("\n");
return 0;
}
C Program to check a given number is positive,negative (or) zero
//Program to check a given number is positive/negative/zero
#include<stdio.h>
int main()
{
int n;
printf("\n Enter a number:");
scanf("%d",&n);
if(n<0)
printf("\n Entered number is negative \n");
else if(n==0)
printf("\n Entered number is Zero \n");
else
printf("\n Entered number is positive \n");
return 0;
}
C Program to find average of n numbers
//Program to find average of n numbers
#include<stdio.h>
int main()
{
int n,i,a[100];
float avg,sum=0;
printf("\n Of how many number you want to find average:");
scanf("%d",&n);
printf("\n Enter numbers:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/n;
printf("\n Average of %d numbers is %f \n:",n,avg);
return 0;
}
C Program to sort a list of integers in ascending order
//Program to sort a list of integers in ascending order
#include<stdio.h>
int main()
{
int n,i,j,temp,a[100];
printf("\n How many numbers you want to sort in Ascending order:");
scanf("%d",&n);
printf("\n enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n unsorted array \n");
for(i=0;i<n;i++)
{
printf("\t %d",a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n sorted array in ascending order \n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n");
return 0;
}
C Program to find area of a square
//Program to find area of a square
#include<stdio.h>
int main()
{
int a,area;
printf("\n enter the side of a square :");
scanf("%d",&a);
printf("\n Area of a square : %d \n",a*a);
printf("\n Perimeter of square :%d",4*a);
return 0;
}
C Program to find area and perimeter of a rectangle
//Program to find area and perimeter of a rectangle
#include<stdio.h>
int main()
{
int l,b,area;
printf("\n enter length of a rectangle :");
scanf("%d",&l);
printf("\n enter breadth/width of a rectangle :");
scanf("%d",&b);
printf("\n Area of a rectangle : %d",l*b);
printf("\n Perimeter of rectangle : %d \n",2*(l+b));
return 0;
}
C Program to calculate area of circle using radius (or) diameter
//Program to calculate area of circle using radius (or) diameter.
#include<stdio.h>
int main()
{
float n,r,d,area;
printf("\n You want to calculate area of circle using");
printf("\n 1.Radius \n 2.Diameter");
printf("\n Enter your choice:");
scanf("%g",&n);
if(n==1)
{
printf("\n Enter radius:");
scanf("%g",&r);
printf("\n Area of circle = %g \n",3.14*2*r);
}
else if(n==2)
{
printf("\n Enter diameter:");
scanf("%g",&d);
printf("\n Area of circle = %g \n",3.14*d);
}
else
printf("\n Invalid choice \n");
return 0;
}
20 August 2018
C Program to find a given character is vowel or consonant.
//Program to find a given character is vowel or consonant.
#include<stdio.h>
int main()
{
char ch;
printf("\n enter a character :");
scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("\n %c is a vowel \n",ch);
else
printf("\n %c is a consonant \n",ch);
return 0;
}
C Program to find sum of digits of a given number
//Program to find sum of digits of a given number
#include<stdio.h>
int main()
{
int a,r,num,sum=0;
printf("\n enter a number:");
scanf("%d",&a);
if(a<0)
num=-a;
else
num=a;
while(num>0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
if(a<0)
printf("\n sum of digits= -%d \n",sum);
else
printf("\n sum of digits=%d \n",sum);
return 0;
}
C Program to print multiplication table of a given number.
//Program to print multiplication table of a given number.
#include<stdio.h>
int main()
{
int n,m,i,k;
printf("\n enter a number to find its multiplication table :");
scanf("%d",&m);
printf("\n Tables of upto %d \n ",m);
for(i=1;i<=10;i++)
{
for(n=1;n<=m;n++)
{
printf("\t %d X %d = %d ",n,i,n*i);
}
printf("\n");
}
return 0;
}
C Program to calculate GCD/HCF of two(2) numbers
//Program to calculate GCD/HCF of two(2) numbers
#include<stdio.h>
int main()
{
int a,b,max,gcd=1;
printf("\n enter 1st number:");
scanf("%d",&a);
printf("\n enter 2nd number:");
scanf("%d",&b);
for(int i=1;i<=a && i<=b;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}
printf("\n GCD of %d and %d is = %d \n",a,b,gcd);
return 0;
}
C Program to find a given number is even (or) odd.
// Program to find a given number is even (or) odd.
#include<stdio.h>
int main()
{
int n,i,is_even=0;
printf("\n enter a number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%2==0)
is_even=1;
}
if(is_even)
printf("\n %d is an even \n",n);
else
printf("\n %d is an odd \n",n);
return 0;
}
C Program to find a given number is Composite (or) not.
//Program to find a given number is Composite (or) not.
#include<stdio.h>
int main()
{
int a,i,count=0;
printf("\n enter a number:");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count>2)
printf("\n It is a composite number \n");
else
printf("\n It is a not a composite number \n");
return 0;
}
C Program to print HELLO WORLD
A simple program to start coding C language.Also to understand the syntax of C we first write and execute "Hello World" program.
//Program to print HELLO WORLD
#include<stdio.h>
int main()
{
printf("\n HELLO WORLD \n");
return 0;
}
17 August 2018
C/C++ Program to check given string is palindrome (or) not.
//Program to check given string is palindrome (or) not.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[50];
cout<<"\n enter a string : ";
cin>>s;
int i=0,j,is_palin=1;
j=strlen(s)-1;
while(i<j)
{
if(s[i]!=s[j])
{
is_palin=0;
break;
}
else
{
++i;
--j;
}
}
if(is_palin)
cout<<"\n It is palindrome"<<endl;
else
cout<<"\n It is not palindrome"<<endl;
return 0;
}
Similar program(s)
C++ Program to check given string is palindrome without library function
16 August 2018
C/C++ Program to find frequency count of a character in a given text
//Program to find frequency count of a character in a given text
#include<iostream>
using namespace std;
int main()
{
char a[100],ch;
int i,f=0;
cout<<"\n Enter a string : ";
cin>>a;
cout<<"\n enter a character to find its frequency : ";
cin>>ch;
for(i=0;a[i]!='\0';i++)
{
if(ch==a[i])
f++;
}
cout<<"\n Frequency count of "<<ch<<" is : "<<f;
cout<<endl;
return 0;
}
C/C++ Program to genereate Fibonnaci series
//Program to generate Fibonacci series
#include<iostream>
using namespace std;
int main()
{
int n,i,f1=0,f2=1,f3;
cout<<"\n Enter how many numbers you want to generate in Fibonacci series:";
cin>>n;
cout<<"\n The Fibonacci series is"<<endl;
cout<<"\n\t";//Spacing for output
if(n==1)
cout<<f1;
else if(n==2)
cout<<f1<<" "<<f2;
else
if(n>2)
{
cout<<f1<<" "<<f2;
for(i=3;i<=n;i++)
{
f3=f2+f1;
f1=f2;
f2=f3;
cout<<" "<<f3;
}
}
cout<<endl;
return 0;
}
C/C++ Program to sort a list of integers in descending order
//Program to sort a list of integers in descending order
#include<iostream>
using namespace std;
int main()
{
int n,i,j,temp,a[100];
cout<<"\n How many numbers you want to sort in Descending order:";
cin>>n;
cout<<"\n enter elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n unsorted array"<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n sorted array in descending order";
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<endl;
return 0;
}
C/C++ Program to sort a list of integers in ascending order
//Program to sort a list of integers in ascending order
#include<iostream>
using namespace std;
int main()
{
int n,i,j,temp,a[100];
cout<<"\n How many numbers you want to sort in Ascending order:";
cin>>n;
cout<<"\n enter elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n unsorted array"<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n sorted array in ascending order";
cout<<endl;
for(i=0;i<n;i++)
{
cout<<"\t"<<a[i];
}
cout<<endl;
return 0;
}
15 August 2018
Java Program to find max and min in a list of integers
//Program to find max and min in a list of integers
import java.util.*;
import java.io.*;
class maxmin
{
public static void main(String args[])
{
int []a=new int[25];
int i,n;
Scanner obj=new Scanner(System.in);
System.out.println("how many integers");
n=obj.nextInt();
System.out.println("enter a list of integers");
for( i=0;i<n;i++)
{
a[i]=obj.nextInt();
}
int max,min;
max=min=a[0];
for( i=0;i<n;i++)
{
if(max<a[i])
{ max=a[i]; }
else if(min>a[i])
{ min=a[i]; }
}
System.out.println("maximum = "+max);
System.out.println("minimum = "+min);
}
}
11 August 2018
C/C++ Program to calculate area of circle
//Program to calculate area of circle
#include<iostream>
using namespace std;
int main()
{
double n,r,d,area;
cout<<"\n You want to calculate area of circle using";
cout<<"\n 1.Radius \n 2.Diameter"<<endl;
cout<<"\n Enter your choice:";
cin>>n;
if(n==1)
{
cout<<"\n Enter radius:";
cin>>r;
cout<<"\n Area of circle ="<<3.14*2*r<<endl;
}
else if(n==2)
{
cout<<"\n Enter diameter:";
cin>>d;
cout<<"\n Area of circle ="<<3.14*d<<endl;
}
else
cout<<"\n Invalid choice"<<endl;
return 0;
}
C/C++ Program to print cube(s) from 1 to n
//Program to print cube(s) from 1 to n
#include<iostream>
using namespace std;
int main()
{
int n,i,cube;
cout<<"\n Enter upto which number you want to find cube(s):";
cin>>n;
for(i=1;i<=n;i++)
{
cube=i*i*i;
cout<<"\n\t"<<i<<"^3"<<" = "<<cube;
}
cout<<endl;
return 0;
}
C/C++ Program to print square(s) from 1 to n
//Program to print square(s) from 1 to n
#include<iostream>
using namespace std;
int main()
{
int n,i,square;
cout<<"\n Enter a upto which number you want to find square(s):";
cin>>n;
cout<<"\n Square value(s):"<<endl;
for(i=1;i<=n;i++)
{
square=i*i;
cout<<"\n\t"<<i<<"^2 = "<<square;
}
cout<<endl;
return 0;
}
C/C++ Program to calculate cube of a given number
//Program to calculate cube of a given number
#include<iostream>
using namespace std;
int main()
{
double n,cube;
cout<<"\n Enter a number to find its cube:";
cin>>n;
cube=n*n*n;
cout<<"\n\t"<<n<<"^3"<<" = "<<cube;
cout<<endl;
return 0;
}
C/C++ Program to calculate square of a given number
//Program to calculate square of a given number
#include<iostream>
using namespace std;
int main()
{
double n,square;
cout<<"\n Enter a number to find its square:";
cin>>n;
square=n*n;
cout<<"\n\t"<<n<<"^2"<<" = "<<square;
cout<<endl;
return 0;
}
10 August 2018
C/C++ Program to convert mile into km and vice-versa
//Program to convert mile into km and vice-versa
#include<iostream>
using namespace std;
int main()
{
double km,mile;
int ch;
cout<<"\n Enter your choice:";
cout<<"\n\t 1. Convert KM into MILE \n\t 2.Convert MILE to KM"<<endl;
cout<<"\n Enter your choice :";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n enter value to convert into MILE :";
cin>>km;
cout<<"\n "<<km<<" km = "<< km*0.621371<<" Mile approx."<<endl;
break;
case 2: cout<<"\n enter value to convert into KM :";
cin>>mile;
cout<<"\n "<<mile<<" Mile= "<<mile*1.60934<<" Km approx."<<endl;
break;
default: cout<<"\n Invalid option"<<endl;
break;
}
return 0;
}
07 August 2018
C/C++ Program to swap to integers without temp variable
//Program to swap to integers without temp variable
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"\n enter A:";
cin>>a;
cout<<"\n enter B:";
cin>>b;
cout<<"\n Before swapping";
cout<<"\n A:"<<a<<"\t B:"<<b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\n After swapping";
cout<<"\n A:"<<a<<"\t B:"<<b<<endl;
return 0;
}
C/C++ Swap two integers using temp variable
//Program to swap two integers using temp variable
#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"\n enter A:";
cin>>a;
cout<<"\n enter B:";
cin>>b;
cout<<"\n Before swapping";
cout<<"\n A:"<<a<<"\t B:"<<b;
temp=a;
a=b;
b=temp;
cout<<"\n After swapping";
cout<<"\n A:"<<a<<"\t B:"<<b<<endl;
return 0;
}
C/C++ Program to find sum of cubes of n natural numbers
//Program to find sum of cubes of n natural numbers
#include<iostream>
using namespace std;
int main()
{
int n,sum;
cout<<"\n enter upto which number you want to find sum of cubes:";
cin>>n;
sum=n*(n+1)/2;//using sum of n naturals formula
sum=sum*sum;
cout<<"\n sum of "<<n<<" natural numbers is : "<<sum<<endl;
return 0;
}
Similar program(s) :
C++ Program to find sum of squares of n natural numbers
C++ Program to find sum of n natural numbers
06 August 2018
C/C++ Program to find sum of squares of n natural numbers
//Program to find sum of squares of n natural numbers
#include<iostream>
using namespace std;
int main()
{
int n,sum;
cout<<"\n enter upto which number you want to find sum of squares:";
cin>>n;
sum=(n*(n+1)*(2*n+1))/6;
cout<<"\n sum of squares of "<<n<<" natural numbers is : "<<sum<<endl;
return 0;
}
Similar program(s):
C++ Program to find sum of cubes of n natural numbers
C++ Program to find sum of digits of a given number
C++ Program to print square(s) from 1 to n
C++ Program to print cube(s) from 1 to n
C/C++ Program to print composite numbers between 1 to n.
//Program to print composite numbers between 1 to n.
#include<iostream>
using namespace std;
int main()
{
int a,i,j,count=0;
cout<<"\n enter upto which number you want to find Composite numbers:";
cin>>a;
for(j=1;j<=a;j++)
{
count=0;
for(i=1;i<=j;i++)
{
if(j%i==0)
count++;
}
if(count>2)
cout<<" "<<j;
}
cout<<endl;
return 0;
}
Similar program(s) :
C++ Program to find a given number is Composite (or) not.
C++ Program to find a given number is even (or) odd.
C++ Program to find a given number is Prime (or) not.
C/C++ Program to find a given number is prime(or)composite.
//Program to find a given number is prime(or)composite.
#include<iostream>
using namespace std;
int main()
{
int a,i,count=0;
cout<<"\n enter a positive number:";
cin>>a;
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count==1)
cout<<"\n 1 is neither Prime nor Composite"<<endl;
else if(count==2)
cout<<"\n It is a prime number"<<endl;
else if(count>2)
cout<<"\n It is a composite number"<<endl;
return 0;
}
Similar program(s):
C++ Program to find a given number is even (or) odd.
C++ Program to find a given number is Prime (or) not.
C++ Program to find a given number is Composite (or) not.
C++ Program to print composite numbers between 1 to n.
C++ Program to find prime number between 1 and n
C/C++ Program to find a given number is Composite (or) not.
//Program to find a given number is Composite (or) not.
#include<iostream>
using namespace std;
int main()
{
int a,i,count=0;
cout<<"\n enter a number:";
cin>>a;
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count>2)
cout<<"\n It is a composite number"<<endl;
else
cout<<"\n It is a not a composite number"<<endl;
return 0;
}
Similar program(s) :
C++ Program to find a given number is even (or) odd.
C++ Program to find a given number is Prime (or) notC++ Program to find prime number between 1 and n
#include<iostream>
using namespace std;
int main()
{
int a,i,count=0;
cout<<"\n enter a number:";
cin>>a;
for(i=1;i<=a;i++)
{
if(a%i==0)
count++;
}
if(count>2)
cout<<"\n It is a composite number"<<endl;
else
cout<<"\n It is a not a composite number"<<endl;
return 0;
}
Similar program(s) :
C++ Program to find a given number is even (or) odd.
C++ Program to find a given number is Prime (or) notC++ Program to find prime number between 1 and n
05 August 2018
C/C++ Program to find maximum n minimum in a list of numbers
//Program to find maximum n minimum in a list of numbers
#include<iostream>
using namespace std;
int main()
{
int a[30],i,n,max,min;
cout<<"\n how many numbers you want to enter in the list:";
cin>>n;
cout<<"\n enter a list of integers:"<<endl;
for( i=0;i<n;i++)
{
cin>>a[i];
}
max=min=a[0];
for( i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
else if(min>a[i])
min=a[i];
}
cout<<"\n maximum="<<max;
cout<<"\n minimun="<<min<<endl;
return 0;
}
C/C++ Program to find a given character is vowel (or) consonant.
//Program to find a given character is vowel (or) consonant.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"\n enter a character :";
cin>>ch;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
cout<<"\n "<<ch<<" is a vowel "<<endl;
else
cout<<"\n "<<ch<<" is a consonant"<<endl;
return 0;
}
04 August 2018
C/C++ Program to print multiplication table of a given number.
//Program to print multiplication table of a given number.
#include<iostream>
using namespace std;
int main()
{
int n,i;
cout<<"\n enter a number to find its multiplication table :";
cin>>n;
cout<<"\n Table of "<<n<<endl;
for(i=1;i<=10;i++)
cout<<"\t"<<n<<" X "<<i<<" = "<<n*i<<endl;
cout<<endl;
return 0;
}
C/C++ Program to find area and perimeter of a rectangle
//Program to find area and perimeter of a rectangle
#include<iostream>
using namespace std;
int main()
{
int l,b,area;
cout<<"\n enter length of a rectangle :";
cin>>l;
cout<<"\n enter breadth/width of a rectangle :";
cin>>b;
cout<<"\n Area of a rectangle :"<<l*b<<endl;
cout<<"\nPerimeter of rectangle :"<<2*(l+b)<<endl;
return 0;
}
C/C++ Program to find area of a square
//Program to find area of a square
#include<iostream>
using namespace std;
int main()
{
int a,area;
cout<<"\n enter the side of a square :";
cin>>a;
cout<<"\n Area of a square :"<<a*a<<endl;
cout<<"\n Perimeter of square :"<<4*a<<endl;
return 0;
}
C/C++ Program to factorial of a number
//Program to factorial of a number
#include<iostream>
using namespace std;
int main()
{
int n,i,f;
cout<<"\n enter a number to find factorial:";
cin>>n;
f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"\n Factorial of "<<n<<" is :"<<f<<endl;
return 0;
}
Subscribe to:
Comments (Atom)
To convert from Upper case to lowercase(A to a)
//converting from Upper to lower(A to a) import java.util.*; class case { public static void main(String args[]) { System.out.pr...
-
//C++ program to calculate x^n #include<iostream> using namespace std; int main() { int n,x,k; cout<<...
-
//Program to calculate LCM of two(2) numbers #include<iostream> using namespace std; int main() { int a,b,max,lcm; cout<...
-
//Program to calculate area of circle using SWITCH statement import java.util.*; class switch1 { public static void main(String a...