C Programming Assignment 02 - PDF
C Programming Assignment 02 - PDF
1 . Start
2. Enter the number that you want to reverse .
3 . While number is not equal to 0 .
4 . Remainder = number %10 .
5 . Rev = rev * 10 + remainder .
11 . Number = number / 10 .
12 . Print the reverse number .
13 . Stop .
FLOWCHAR Start
T :
Declare n ,
reverse and
remainder .
Yes No
While
Remainder= n%10 (n !=
Reverse = reverse*10+remainder 0)
n = n/10
C CODE :
•#include <stdio.h>
•#include <conio.h>
•void main()
•{
•int num, rev = 0, rem;
•printf("Enter the number to reverse: ");
•scanf("%d", &num);
•while (num != 0){
•rem = num % 10;
•rev = rev * 10 + rem;
•num = num/10;
•}
•printf("The reversed number is: %d", rev);
•getch();
REMARK
S
• :There are many other ways also to solve this problem .
Q 2 ) Write a c program that will convert binary to decimale number . ( integer
number )
ALGORITHM :
1 . Start
2 . Take base = 1 , decimale = 0
3. Enter the number that you want to convert .
4 . Use while loop .
5 . remainder = temp % 10; decimal = decimal
+ remainder * base; temp = temp / 10; base
= base * 2;
6 . Print the binary value .
7 . Print the decimale value .
8 . Stop .
C CODE :
#include <stdio.h>
int main() {
int binary, decimal = 0, base = 1, remainder;
printf("Enter the Binary Number = ");
scanf("%d", &binary);
int temp = binary;
while(temp > 0)
{ remainder = temp %
10;
decimal = decimal +
remainder * base;
temp = temp / 10;
base = base * 2;
}
printf("The Binary Value = %d\n", binary);
printf("The Decimal Value = %d\n", decimal);
return 0;
}
REMARKS :
● There are many other ways also to solve this problem .
Q 3 ) Write a c program that will convert the decimale to binary number . ( integer number )
ALGORITHM :
1 . Start
2 . Take number , i , j , a[10] as input .
3. Please enter the number you want to convert .
4 . Take a for loop .
5 . a[i] = number % 2; number = number / 2;
6 . Print the binary number of a given number .
7 . Stop .
C CODE :
#include
<stdio.h> int
main() {
int a[10], number,
i, j;
printf("\n Please Enter the Number You want to
Convert : ");
scanf("%d", &number);
for(i = 0; number > 0; i++) {
a[i] = number % 2;
number = number / 2;
}
printf("\n Binary Number of a Given Number = ");
for(j = i - 1; j >= 0; j--)
{
printf(" %d ", a[j]);
}
printf("\n");
return 0;
}
REMARKS :
ALGORITHM :
1 . Start .
2 . Enter the number you want to convert .
3 . Take a for loop .
4 . i = 0; number > 0; i++ .
5 . octalNumber[i] = number % 8; number = number / 8;
6. Print the equivalent octal number .
7 . Use for loop - for(j = i - 1; j >= 0;
j--) 8 . Stop .
C CODE :
#include <stdio.h>
int main() {
int octalNumber[10], number, i, j;
printf("\n Please Enter the Number You want to Convert : ");
scanf("%d", &number);
for(i = 0; number > 0; i++)
{
octalNumber[i] = number % 8;
number = number / 8;
}
printf("\n Equivalent Octal Number of a Given Number = ");
for(j = i - 1; j >= 0; j--)
{
printf("%d", octalNumber[j]);
}
return 0;
}
REMARKS :
ALGORITHM :
1 . Start
2 . Enter the octal number .
3 . Scan the input
4. Print hexadecimal number
5 . Use %x .
6 . Stop
C CODE :
Include<stdio.h>
int main(){
int a ;
printf(“ Enter the octal number \n “ ) ;
scanf(“%d” , &a ) ;
printf( “ Enter the hexadecimal number “ , a )
return 0 ;
}
Q 6 ) Write a c program that will show the simple calculator using switch
case .
ALGORITHM :
1 . Start
2 . Taking a , b as integer .
3. Use char op to store the operator .
4 . Use the switch case pattern
switch(op) { case '+’:
result = a + b; break;
case '-’:
result = a - b;
break;
case '*’:
result = a * b;
break;
case '/’:
result = a / b;
break; }
5 . Print the
result .
FLOWCHART :
Case 1 Add
Start
Ye
No s
Case 2 Subtract
Read a , b Ye
No s
Case 3 Multiply
Ye
Switch ( choice ) No s
No
Case 4
Divide
Stop Default
Ye
#include<stdio.h>
int main(void) C CODE :
{ int a, b, result;
char op;
printf("Enter an expression: ");
scanf("%d%c%d", &a, &op, &b);
switch(op) {
case '+’:
result = a + b;
break;
case '-’:
result = a - b;
break;
case '*’:
result = a *
b;
break;
case '/’:
result = a / b;
break;
}
printf("Result
= %d",
result);
REMARKS :
ALGORITHM :
1 . Start .
2. Enter the number you want in series .
3 . for(i=0;i<n;i++)
4 . Print fibonacci .
5 . Use if else condition
6 . if(i==0)
return 0;
else if(i==1)
return 1;
else return
(fibonacci(i-
1)+fibonacci
(i-2));
7 . Stop .
#include<stdio.h> C CODE :
#include<conio.h>
int fibonacci(int);
int main(){
int n, i;
printf("Enter the number of element you want in series :\n");
scanf("%d",&n);
printf("fibonacci series is : \n");
for(i=0;i<n;i++) {
printf("%d ",fibonacci(i));
}
getch();
}
int fibonacci(int i){
if(i==0)
return 0;
else if(i==1)
return 1;
else return
(fibonacci(i-
1)+fibonacci
(i-2));
REMARKS :
ALGORITHM :
1 . Start
2. Take a and b as integer .
3 . Enter the value of a .
4 . Enter the value of b .
5.a=a–b;
b = a +
b; a=b
–a;
6 . Print the
value of a
and b
after
swapping .
7 . Stop .
Start
FLOWCHART :
Read a , b
a=b-b ;
b=a+b ;
a=b-a
Stop
Print a , b .
C CODE :
#include<stdio.h>
#include<conio.h>
void main(){
int a,b;
printf("enter the value of a: ");
scanf("%d",&a);
printf("enter the value of b: ");
scanf("%d",&b);
a=a-b;
b=a+b;
a=b-a;
printf("After swapping \n");
printf("value of a is : %d\n", a);
printf("value of b is : %d ",b);
getch();
}
REMARK
S:
1 . Start
2 . For string operation we have to use include<string.h>
3. char str[20];
int i, len, temp=0;
int flag = 0;
4. Enter a string .
5 . len = strlen(str);
for(i=0;i < len ;i++)
{
if(str[i] != str[len-
i-1])
{
temp = 1;
6 . Use if else
condition .
7. If ( temp == 0 ) then print string is palindrome .
8 . Else print string is not palindrome .
#include<stdio.h>
#include<conio.h>
#include<string.h>
C CODE :
int main(){
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0;i < len ;i++)
{
if(str[i] != str[len-i-1]){
temp = 1;
break;
}
}
if (temp==0) {
printf("String is a
palindrome");
}
else {
printf("String is not a
palindrome");
}
return 0;
Start Stop
Print “ String is
FLOWCHA Print “ String is not palindrome .
RT : Take the
palindrome .
string Ye
as input s
No
Is given
string = rev
Calculate the
length of
the string .
No
Ye
s
Initialize rev i>=0
as a empty
string .