0% found this document useful (0 votes)
28 views11 pages

2nd Assginment

Pingu programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views11 pages

2nd Assginment

Pingu programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PROBLEM STATEMENT:-

WAP in C two two numbers without using third variable .

SOURCE CODE :-

#include<stdio.h>

int main()

int a=10, b=20;

printf("Before swap a=%d b=%d",a,b);

a=a+b;//a=30 (10+20)

b=a-b;//b=10 (30-20)

a=a-b;//a=20 (30-10)

printf("\nAfter swap a=%d b=%d",a,b);

return 0;

OUTPUT:-

PROBLEM STATEMENT :-

WAP in C to find out whether a number is a power of 2 using bitwise operator

SOURCE CODE :-

#include <stdio.h>
int checkPowerOf2(unsigned int num)

// Check if the number has only one set bit

if ((num & (num - 1)) != 0)

return 0;

return 1;

int main()

unsigned int num = 0;

printf("Enter Number: ");

scanf("%d", &num);

if (checkPowerOf2(num))

printf("Given number is power of 2.\n");

else

printf("Given number is not power of 2.\n");

return 0;

OUTPUT:-
PROBLEM STATEMENT :-

WAP in c t check whether it is prime or not

Source code :-

nt main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

// 0 and 1 are not prime numbers

// change flag to 1 for non-prime number

if (n == 0 || n == 1)

flag = 1;

for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime

// change flag to 1 for non-prime number

if (n % i == 0) {
flag = 1;

break;

// flag is 0 for prime numbers

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

OUTPUT:-

PROBLEM STATEMENT :-

WAP in C to generate Fibonacci series upto n terms . { n = value taken from user }

SOURCE CODE :-

#include<stdio.h>
int main()

int n1=0,n2=1,n3,i,number;

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

scanf("%d",&number);

printf("\n%d %d",n1,n2);//printing 0 and 1

for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed

n3=n1+n2;

printf(" %d",n3);

n1=n2;

n2=n3;

return 0;

OUTPUT:-

PROBLEM STATEMENT :-

WAP in C to find out LC and GCD of two number

SOURCE CODE :-
#include <stdio.h>

void main()

int num1, num2, gcd, lcm, remainder, numerator, denominator;

printf("Enter two numbers:\n");

scanf("%d %d", &num1, &num2);

//To find numerator and denominator

numerator = (num1>num2)?num1:num2;

denominator = (num1<num2)?num1:num2;

remainder = numerator % denominator;

while (remainder != 0)

numerator = denominator;

denominator = remainder;

remainder = numerator % denominator;

gcd = denominator;

lcm = num1 * num2 / gcd;

printf("GCD of %d and %d = %d\n", num1, num2, gcd);

printf("LCM of %d and %d = %d\n", num1, num2, lcm);


}

OUTPUT:-

PROBLEM STATEMENT :-

WAP in C to find out the combination of a number

Source CODE :-

#include <stdio.h>

int fact(int z);

void main()

int n, r, nCr;

printf("Enter the value of n and r?");

scanf("%d %d",&n,&r);

nCr = fact(n) / (fact(r) * fact(n - r));

printf("\nnCr = %d", nCr);

int fact(int z)
{

int f = 1, i;

if (z == 0)

return(f);

else

{ for (i = 1; i <= z; i++)

f = f * i;

return(f);

OUTPUT:-
PROBLEM STATEMENT :-

WAP in C to create a menu driven program which will take two values from user and a
choice between addition , subtraction , multiplication and division and perform that acton
on the values and show result .

SOURCE CODE :-

#include <stdio.h>

int main() {

char op;

double first, second;

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

scanf("%c", &op);

printf("Enter two operands: ");

scanf("%lf %lf", &first, &second);

switch (op) {

case '+':

printf("%.1lf + %.1lf = %.1lf", first, second, first + second);

break;

case '-':

printf("%.1lf - %.1lf = %.1lf", first, second, first - second);

break;

case '*':

printf("%.1lf * %.1lf = %.1lf", first, second, first * second);


break;

case '/':

printf("%.1lf / %.1lf = %.1lf", first, second, first / second);

break;

// operator doesn't match any case constant

default:

printf("Error! operator is not correct");

return 0;

OUTPUT:-

---------- X ----------

You might also like