JAVA CODING AND C PROGRAMMING EXAMPLES - PROGRAMMING FOR BEGINNERS (BooksRack - Net)
JAVA CODING AND C PROGRAMMING EXAMPLES - PROGRAMMING FOR BEGINNERS (BooksRack - Net)
C
PROGRAMMING
EXAMPLES
C PROGRAMMING EXAMPLES
C PROGRAM TO FIND SUM AND AVERAGE OF TWO
NUMBERS.
C PROGRAM TO PRINT ASCII VALUE OF A
CHARACTER
C PROGRAM TO PRINT ALL NUMBERS FROM 1 TO N
USING GOTO STATEMENT
PROGRAM TO PRINT NUMBERS FROM 1 TO N USING
WHILE LOOP
PROGRAM TO PRINT SQUARE, CUBE AND SQUARE
ROOT
PROGRAM TO PRINT TABLE OF A GIVEN NUMBER
PROGRAM FOR LARGEST NUMBER AMONG THREE
NUMBERS
AREA OF RECTANGLE PROGRAM IN C
C PROGRAM - CONVERT FEET TO INCHES
PROGRAM TO CREATE A TEXT FILE USING FILE
HANDLING
C PROGRAM TO FIND FACTORIAL OF A NUMBER
PROGRAM TO SWAP TWO BITS OF A BYTE
PROGRAM TO SWAP TWO NUMBERS USING FOUR
DIFFERENT METHODS
AGE CALCULATOR (C PROGRAM TO CALCULATE
AGE)
C PROGRAM TO DESIGN A DIGITAL CLOCK
PROGRAM TO CALCULATE COMPOUND INTEREST
PROGRAM TO CHECK WHETHER NUMBER IS
PERFECT SQUARE OR NOT
PROGRAM TO PRINT MESSAGE WITHOUT USING ANY
SEMICOLON IN PROGRAM
SUM OF SERIES PROGRAMS / EXAMPLES USING C
JAVA CODING
EXAMPLES
PROGRAM
class j2{
public static void main(String args[])
{
int num;
float b;
char c;
String s;
//integer
num = 100;
//float
b = 1.234f;
//character
c = 'A';
//string
s = "Hello Java";
Output
Value of num: 100
Value of b: 1.234
Value of c: A
Value of s: Hello Java
Read and print an integer value in
Java
Here we will learn how to take an integer input from the user and print it on
the screen, to take the input of an integer value-we use Scanner class, for this
we must include java.util. * package in our Java program.
PROGRAM
import java.util.*;
class j3
{
public static void main(String args[])
{
int a;
PROGRAM
// Find sum and average of two numbers in Java
import java.util.*;
Output
Enter first number : 100
Enter second number : 200
Sum : 300
Average : 150.0
Java program to print Christmas
tree
Example:
*
***
*****
*******
***
*****
*******
*********
*****
*******
*********
***********
*******
*********
***********
*************
*
*
*******
PROGRAM
public class ChristmasTree
{
public static final int SEGMENTS = 4;
public static final int HEIGHT = 4;
public static void main(String[] args)
{
makeTree();
}
OUTPUT
Java program to find sum of all
digits
Provided a number and we have to use java program to find sum of all its
digits.
Example 1:
Input:
Number: 852
Output:
Sum of all digits: 15
Example 2:
Input:
Number: 256868
Output:
Sum of all digits: 35
PROGRAM
import java.util.Scanner;
Second run:
Enter the Number : 256868
Sum of Digits of 256868 is : 35
Java program to calculate
compound interest
Given the principle, rate and time, and using java program we have to
find compound interest.
Example:
Enter Principal : 5000
Enter Rate : 5
Enter Time : 3
Amount : 5788.125000000001
Compound Interest : 788.1250000000009
PROGRAM
import java.util.Scanner;
Second run:
Enter Principal : 10000
Enter Rate : 20
Enter Time : 5
Amount : 24883.199999999997
Compound Interest : 14883.199999999997
Java program to find largest
number
The program reads from the keyboard (three integer numbers), and selects the
largest number.
PROGRAM
//Java program to find largest number among three numbers.
import java.util.*;
class LargestFrom3
{
public static void main(String []s)
{
int a,b,c,largest;
//Scanner class to read value
Scanner sc=new Scanner(System.in);
}
}
Output
Complie : javac LargestFrom3.java
Run : java LargestFrom3
Output
Enter first number:45
Enter second number:56
Enter third number:67
Largest Number is : 67
Java program to run an application
Using Java program this program can open (run) applications (Notepad,
Calculator). We use instance of Runtime.getRuntime() and method exec() to
open / run application.
PROGRAM
//Java program to run an application.
import java.util.*;
class OpenNotepad
{
public static void main(String[] args)
{
Runtime app=Runtime.getRuntime();
try
{
//open notepad
app.exec("notepad");
//open calculator
app.exec("calc");
}
catch (Exception Ex)
{
System.out.println("Error: " + Ex.toString());
}
}
}
OUTPUT
Notepad and Calculator will be opened.
Java program to print Fibonacci
Series
Fibonacci Series is a series where the term is the sum of two preceding terms.
PROGRAM
/*Java program to print Fibonacci Series.*/
import java.util.Scanner;
int SeriesNum ;
Scanner sc=new Scanner(System.in);
}
OUTPUT
Enter the length of fibonacci series : 10
fibonacci series :
0 1 1 2 3 5 8 13 21 34
Using for loop to print numbers
from 1 to N
PROGRAM
import java.util.Scanner;
while(i<=n)
{
System.out.println(i);
i++;
}
}
}
Output
Enter the value n : 15
Numbers are :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Addition of one dimensional and
two dimensional arrays
IN JAVA PROGRAM
There are 2 programs: two one-dimensional arrays added, and two two-
dimensional arrays added
//Display results
System.out.println("Enter sum of "+i +"index" +" " + c[i]);
}
}
}
. Output
Enter sum of 0index 7
Enter sum of 1index 9
Enter sum of 2index 11
Enter sum of 3index 13
Enter sum of 4index 15
2) Addition of two two dimensional arrays in java
class AddTwoArrayOf2DClass{
public static void main(String[] args){
// Declaration and initialization of 2D array
int a[][] = {{1,2,3},{4,5,6}};
int b[][] = {{7,8,9},{10,11,12}};
class DecimalToBinaryConversionClass{
public static void main(String[] args){
// create Scanner class object
Scanner sc = new Scanner(System.in);
We use a file called "B.txt" for this program that is located at the "F:\" drive,
thus the file path is "F:\B.txt," and the file content is:
This is line 1
This is line 2
This is line 3
This is line 4
PROGRAM
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
}
OUTPUT
14/8/20
Thu Aug 14 21:29:07 GMT 2020
21:29:7
Java program to Calculate Area of
a Circle.
This program reads circle radius and calculates Area of the circle
PROGRAM
//Java program to Calculate Area of a Circle.
import java.util.Scanner;
double radius;
Scanner sc=new Scanner(System.in);
}
}
OUTPUT
Enter the Radius of Circle : 12.5
Area of Circle : 490.625
Java - Print File Content, Display
File
Using Java program we will print the file size and file information in this
code snippet.
PROGRAM
//Java - Print File Content, Display File using Java Program.
import java.io.*;
//close file
inFile.close();
}
}
OUTPUT
File size is: 22
File content is:
This is a sample file.
Java program to copy files
This program copies files in Java.
//Java program to copy file.
import java.io.*;
Input: 5
Output: 152
Explanation:
1! + 2! + 3! + 4! + 5!
= 1+2+6+24+120
= 153
PROGRAM
import java.util.Scanner;
int total=0;
int i=1;
// calculate factorial here.
while(i <= n)
{
int factorial=1;
int j=1;
while(j <= i)
{
factorial=factorial*j;
j = j+1;
}
// calculate sum of factorial of the number.
total = total + factorial;
i=i+1;
}
// print the result here.
System.out.println("Sum : " + total);
}
}
Output
First run:
Enter number : 3
Sum : 9
Second run:
Enter number : 5
Sum : 153
Find smallest element in an array
Example:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Output:
Smallest element in: 25
PROGRAM
import java.util.Scanner;
public class ExArrayFindMinimum
{
public static void main(String[] args)
{
// Intialising the variables
int n, min;
Scanner Sc = new Scanner(System.in);
// creating an array.
int a[] = new int[n];
int main()
{
int a,b,sum;
float avg;
sum=a+b;
avg= (float)(a+b)/2;
return 0;
}
Output
Enter first number :10
Enter second number :15
Sum of 10 and 15 is = 25
Average of 10 and 15 is = 12.500000
C program to print ASCII value of
a character
PROGRAM
#include <stdio.h>
int main()
{
char ch;
//input character
printf("Enter the character: ");
scanf("%c",&ch);
Second run:
Enter the character: A
ASCII is = 65
C program to print all numbers
from 1 to N using goto statement
PROGRAM
#include <stdio.h>
int main()
{
int count,n;
//read value of N
printf("Enter value of n: ");
scanf("%d",&n);
start: //label
printf("%d ",count);
count++;
if(count<=n)
goto start;
return 0;
}
Output
Enter value of n: 10
1 2 3 4 5 6 7 8 9 10
Program to print numbers from 1 to
N using while loop
PROGRAM
#include <stdio.h>
int main()
{
#include <stdio.h>
#include <math.h>
int main()
{
int i,n;
return 0;
}
Program to print table of a given
number
PROGRAM
#include<stdio.h>
int main()
{
//declare variable
int count,t,r;
//Read value of t
printf("Enter number: ");
scanf("%d",&t);
count=1;
start:
if(count<=10)
{
//Formula of table
r=t*count;
printf("%d*%d=%d\n",t,count,r);
count++;
goto start;
}
return 0;
}
Output
Enter number: 10
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100
Program for Largest Number
among three numbers
PROGRAM
/* c program to find largest number among 3 numbers*/
#include <stdio.h>
int main()
{
int a,b,c;
int largest;
return 0;
}
Output
First Run:
Enter three numbers (separated by space): 10 20 30
Largest number is = 30
Second Run:
Enter three numbers (separated by space):10 30 20
Largest number is = 30
Third Run:
Enter three numbers (separated by space):30 10 20
Largest number is = 30
Fourth Run:
Enter three numbers (separated by space):30 30 30
Largest number is = 30
Area of rectangle program in c
PROGRAM
/*C program to find area of a rectangle.*/
#include <stdio.h>
int main()
{
float l,b,area;
area=l*b;
return 0;
}
Output
Enter the value of length: 1.25
Enter the value of breadth: 3.15
Area of rectangle: 3.937500
C program - Convert Feet to Inches
PROGRAM
#include<stdio.h>
int main()
{
int feet,inches;
printf("Enter the value of feet: ");
scanf("%d",&feet);
//converting into inches
inches=feet*12;
printf("Total inches will be: %d\n",inches);
return 0;
}
Output
Enter the value of feet: 15
Total inches will be: 180
Program to create a text file using
file handling
PROGRAM
#include< stdio.h >
int main()
{
Run 2:
Enter file name to create : d:/file1.txt
File created successfully.
Run 3:
Run 1:
Enter file name to create : h:/file1.txt
File does not created!!!
C program to find factorial of a
number
Factorial is an integer product, with all of it below integer till 1. Represent
factorial in mathematical by ! Sign.
Example:
Factorial 5 is:
5! = 120 [That is equivalent to 5*4*3*2*1 =120]
Simple program
/*C program to find factorial of a number.*/
#include <stdio.h>
int main()
{
int num,i;
long int fact;
printf("\nFactorial of %d is = %ld",num,fact);
return 0;
}
Output
Enter an integer number: 7
Factorial of 7 is = 5040
User Define Function
/*Using Function: C program to find factorial of a number.*/
#include <stdio.h>
for(i=n;i>=1;i--)
fact= fact * i;
return fact;
}
int main()
{
int num;
printf("\nFactorial of %d is = %ld",num,factorial(num));
return 0;
}
Program to swap two bits of a byte
Example:
Input number: 0x0A (Hexadecimal)
Binary of input number: 0000 1010
After swapping of bit 1 and 2
Binary will be: 0000 1100
Output number will be: 0x0C (Hexadecimal)
PROGRAM
#include <stdio.h>
/*
Program to swap 1st and 2nd bit of hexadecimal value stored in data variable
*/
int main()
{
unsigned char data = 0xA; // Assiging hexadecimal value
printf("After swapping the bits, data value is: %2X", data ^ (xor_of_bit
<< 1 | xor_of_bit << 2));
return 0;
}
Output
After swapping the bits, data value is: C
Program to swap two numbers
using four different methods
METHODS
1. Using third variable
2. Without using third variable
3. Using X-OR operator
4. Using simple statement
PROGRAM
//C program to swap two numbers using four different methods.
#include <stdio.h>
int main()
{
int a,b,t;
printf(" Enter value of A ? ");
scanf("%d",&a);
printf(" Enter value of B ? ");
scanf("%d",&b);
return 0;
}
Output
Enter value of A ? 100
Enter value of B ? 200
#include <stdio.h>
#include <time.h>
int main()
{
/* print age */
printf("\n## Hey you are %d years %d months and %d days old. ##\n",
year, month, days);
return 0;
}
C program to design a digital clock
PROGRAM
/*C program to design a digital clock.*/
#include <stdio.h>
#include <time.h> //for sleep() function
#include <unistd.h>
#include <stdlib.h>
int main()
{
int hour, minute, second;
hour=minute=second=0;
while(1)
{
//clear output screen
system("clear");
//increase second
second++;
return 0;
}
Program to calculate compound
interest
PROGRAM
/*C program to calculate compound interest.*/
#include <stdio.h>
#include <math.h>
int main()
{
float principal, rate, year, ci;
ci=principal*((pow((1+rate/100),year)-1));
return 0;
}
Output
Enter principal: 10000
Enter rate: 10.25
Enter time in years: 5
Compound interest is: 6288.943359
Program to check whether number
is Perfect Square or not
PROGRAM
/*C program to check number is perfect square or not.*/
#include <stdio.h>
#include <math.h>
int main()
{
int num;
int iVar;
float fVar;
fVar=sqrt((double)num);
iVar=fVar;
if(iVar==fVar)
printf("%d is a perfect square.",num);
else
printf("%d is not a perfect square.",num);
return 0;
}
#include <stdio.h>
#include <math.h>
/*function definition*/
int isPerfectSquare(int number)
{
int iVar;
float fVar;
fVar=sqrt((double)number);
iVar=fVar;
if(iVar==fVar)
return 1;
else
return 0;
}
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",&num);
if(isPerfectSquare(num))
printf("%d is a perfect square.",num);
else
printf("%d is not a perfect square.",num);
return 0;
}
Output
First Run:
Enter an integer number: 16
16 is a perfect square.
Second Run:
Enter an integer number: 26
26 is not a perfect square.
Program to print message without
using any semicolon in program
PROGRAM
/*Program to print "Hello C" using if and else
statement both.*/
#include <stdio.h>
int main()
{
if(!printf("Hello "))
;
else
printf("C\n");
return 0;
}
OUTPUT
Hello C
Sum of Series Programs / Examples
using C
1. 1+2+3+4+..N<
PROGRAM
/*This program will print the sum of all natural numbers from 1 to N.*/
#include<stdio.h>
int main()
{
int i,N,sum;
return 0;
}
Enter the value of N: 100
Sum of the series is: 5050