Sei sulla pagina 1di 16

1)Program to Multiply Two Numbers

#include <stdio.h>
int main()
{
double firstNumber, secondNumber, product;
printf("Enter two numbers: ");

// Stores two floating point numbers in variable firstNumber and secondNumber respectively
scanf("%lf %lf", &firstNumber, &secondNumber);

// Performs multiplication and stores the result in variable productOfTwoNumbers


product = firstNumber * secondNumber;

// Result up to 2 decimal point is displayed using %.2lf


printf("Product = %.2lf", product);

return 0;
}

Program to Print ASCII Value


#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");

// Reads character input from the user


scanf("%c", &c);

// %d displays the integer value of a character


// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}

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

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");


scanf("%d", &dividend);

printf("Enter divisor: ");


scanf("%d", &divisor);

// Computes quotient
quotient = dividend / divisor;

// Computes remainder
remainder = dividend % divisor;

printf("Quotient = %d\n", quotient);


printf("Remainder = %d", remainder);

return 0;
}

#include <stdio.h>
int main()
{
int integerType;
float floatType;
double doubleType;
char charType;

// Sizeof operator is used to evaluate the size of a variable


printf("Size of int: %ld bytes\n",sizeof(integerType));
printf("Size of float: %ld bytes\n",sizeof(floatType));
printf("Size of double: %ld bytes\n",sizeof(doubleType));
printf("Size of char: %ld byte\n",sizeof(charType));

return 0;
}

#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;

printf("Enter first number: ");


scanf("%lf", &firstNumber);

printf("Enter second number: ");


scanf("%lf",&secondNumber);

// Value of firstNumber is assigned to temporaryVariable


temporaryVariable = firstNumber;

// Value of secondNumber is assigned to firstNumber


firstNumber = secondNumber;

// Value of temporaryVariable (which contains the initial value of


firstNumber) is assigned to secondNumber
secondNumber = temporaryVariable;
printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
printf("After swapping, secondNumber = %.2lf", secondNumber);

return 0;
}

#include <stdio.h>
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;

printf("Enter an alphabet: ");


scanf("%c",&c);

// evaluates to 1 (true) if c is a lowercase vowel


isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c
== 'u');

// evaluates to 1 (true) if c is an uppercase vowel


isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c
== 'U');

// evaluates to 1 (true) if either isLowercaseVowel or


isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

#include <stdio.h>
int main()
{
int n, i;
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;
}

#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 1; i <= n; ++i)


{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

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

printf("Enter an integer: ");


scanf("%d", &n);

while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}

printf("Reversed Number = %d", reversedNumber);

return 0;
}

#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;

printf("Enter an integer: ");


scanf("%d", &n);

originalInteger = n;

// reversed integer is stored in variable


while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}

// palindrome if orignalInteger and reversedInteger are equal


if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);

return 0;
}

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

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i<=rows; ++i)


{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
# include <stdio.h>

int main() {

char operator;
double firstNumber,secondNumber;

printf("Enter an operator (+, -, *,): ");


scanf("%c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf",&firstNumber, &secondNumber);

switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber + secondNumber);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber - secondNumber);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber * secondNumber);
break;

case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber / secondNumber);
break;

// operator doesn't match any case constant (+, -, *, /)


default:
printf("Error! operator is not correct");
}

return 0;
}

#include <stdio.h>
#include <math.h>
int convertBinaryToDecimal(long long n);

int main()
{
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n,
convertBinaryToDecimal(n));
return 0;
}

int convertBinaryToDecimal(long long n)


{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}

#include <stdio.h>
void reverseSentence();

int main()
{
printf("Enter a sentence: ");
reverseSentence();

return 0;
}

void reverseSentence()
{
char c;
scanf("%c", &c);

if( c != '\n')
{
reverseSentence();
printf("%c",c);
}
}

#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}

#include <iostream>
using namespace std;

int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;

cout << "Enter an alphabet: ";


cin >> c;

// evaluates to 1 (true) if c is a lowercase vowel


isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c
== 'u');

// evaluates to 1 (true) if c is an uppercase vowel


isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c
== 'U');

// evaluates to 1 (true) if either isLowercaseVowel or


isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";

return 0;
}

#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


{
cout << "Largest number: " << n1;
}

if(n2 >= n1 && n2 >= n3)


{
cout << "Largest number: " << n2;
}

if(n3 >= n1 && n3 >= n2) {


cout << "Largest number: " << n3;
}

return 0;
}

#include <iostream>
using namespace std;

int main()
{
unsigned int n;
unsigned long long factorial = 1;

cout << "Enter a positive integer: ";


cin >> n;

for(int i = 1; i <=n; ++i)


{
factorial *= i;
}

cout << "Factorial of " << n << " = " << factorial;
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)


{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";


}
return 0;
}

#include <iostream>
using namespace std;

int main()
{
int n, reversedNumber = 0, remainder;

cout << "Enter an integer: ";


cin >> n;

while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber;

return 0;
}

public class JavaExample {

public static void main(String[] args) {


double[] arr = {19, 12.89, 16.5, 200, 13.7};
double total = 0;

for(int i=0; i<arr.length; i++){


total = total + arr[i];
}

/* arr.length returns the number of elements


* present in the array
*/
double average = total / arr.length;

/* This is used for displaying the formatted output


* if you give %.4f then the output would have 4 digits
* after decimal point.
*/
System.out.format("The average is: %.3f", average);
}
}

import java.util.Scanner;

class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");

//The input provided by user is stored in num


Scanner input = new Scanner(System.in);
num = input.nextInt();

/* If number is divisible by 2 then it's an even number


* else odd number*/
if ( num % 2 == 0 )
System.out.println("Entered number is even");
else
System.out.println("Entered number is odd");
}
}
public class JavaExample {

public static void main(String[] args) {


String str = "Welcome to Beginnersbook";
String reversed = reverseString(str);
System.out.println("The reversed string is: " + reversed);
}

public static String reverseString(String str)


{
if (str.isEmpty())
return str;
//Calling Function Recursively
return reverseString(str.substring(1)) + str.charAt(0);
}
}

public class Demo {

public static void main(String[] args) {

char ch = 'P';
int asciiCode = ch;
// type casting char as int
int asciiValue = (int)ch;

System.out.println("ASCII value of "+ch+" is: " + asciiCode);


System.out.println("ASCII value of "+ch+" is: " + asciiValue);
}
}

class DecimalBinaryExample{

public static void main(String a[]){


System.out.println("Binary representation of 124: ");
System.out.println(Integer.toBinaryString(124));
System.out.println("\nBinary representation of 45: ");
System.out.println(Integer.toBinaryString(45));
System.out.println("\nBinary representation of 999: ");
System.out.println(Integer.toBinaryString(999));
}
}

import java.net.InetAddress;

class GetMyIPAddress
{
public static void main(String args[]) throws Exception
{
/* public static InetAddress getLocalHost()
* throws UnknownHostException: Returns the address
* of the local host. This is achieved by retrieving
* the name of the host from the system, then resolving
* that name into an InetAddress. Note: The resolved
* address may be cached for a short period of time.
*/
InetAddress myIP=InetAddress.getLocalHost();

/* public String getHostAddress(): Returns the IP


* address string in textual presentation.
*/
System.out.println("My IP Address is:");
System.out.println(myIP.getHostAddress());
}
}

public class JavaExample {

public static void main(String[] args) {

//We will find the factorial of this number


int number = 5;
long fact = 1;
for(int i = 1; i <= number; i++)
{
fact = fact * i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}

public class JavaExample {

public static void main(String[] args) {

int count = 7, num1 = 0, num2 = 1;


System.out.print("Fibonacci Series of "+count+" numbers:");

for (int i = 1; i <= count; ++i)


{
System.out.print(num1+" ");

/* On each iteration, we are assigning second number


* to the first number and assigning the sum of last two
* numbers to the second number
*/
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
}
}
}

num = 8

# uncomment to take the input from the user


#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

# Python program to swap two variables

# To take input from the user


# x = input('Enter value of x: ')
# y = input('Enter value of y: ')

x=5
y = 10

# create a temporary variable and swap the values


temp = x
x=y
y = temp

print('The value of x after swapping: {}'.format(x))


print('The value of y after swapping: {}'.format(y))

# Program to generate a random number between 0 and 9

# import the random module


import random

print(random.randint(0,9))

# Python program to check if the input number is prime or not

num = 407

# take input from the user


# num = int(input("Enter a number: "))

# prime numbers are greater than 1


if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")

# if input number is less than


# or equal to 1, it is not prime
else:
print(num,"is not a prime number")

# Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# uncomment to take input from the user


#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

# Program to display the Fibonacci sequence up to n-th term where n is provided by the user

# change this value for a different result


nterms = 10

# uncomment to take input from the user


#nterms = int(input("How many terms? "))

# first two terms


n1 = 0
n2 = 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence upto",nterms,":")
while count < nterms:
print(n1,end=' , ')
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Potrebbero piacerti anche