0% found this document useful (0 votes)
17 views12 pages

Week 2 C Lab

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

Week 2 C Lab

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

C LAB 2

PROGRAM 1: A special two- digit number is such that when the sum of its
digits is added to the products of its digits, the result is equal to the original two -
digits number. Example: Consider the number 59. Sum of digits = 5+9 =14
Products of its digits = 5*9=45 Sum of the sum of the digits and product of the
digits = 14+45=59 Write a program to accept a two- digit number. Check
whether the given number is special number or not.

CODE:

#include <stdio.h>

int main() {

int num,sum=0,mul=1,add=0,temp;

printf("Enter a two digit number: ");

scanf("%d",&num);

temp=num;

while(temp!=0){

sum+=temp%10;

mul*=temp%10;

temp=temp/10;

add=sum+mul;

printf("Sum of two digits of number: %d\n",sum);

printf("Product of two digits of number: %d\n",mul);


if(add==num){

printf("Given number %d is a special number\n",num);

else{

printf("Given number %d is not a special number\n",num);

return 0;

}
OUTPUT:

PROGRAM 2: Given an array of integers arr[] of size N and an integer, Write


a C program to rotate the array elements to the left by d positions. Example:
Input: arr[] = {3, 4, 5, 6, 7, 1, 2}, d=2 Output: 5 6 7 1 2 3 4

CODE:

#include <stdio.h>

int main() {

int size,shift,ele;

printf("Enter the size of array: ");

scanf("%d",&size);

int array[size];

int array2[size];

for(int i = 0;i<size;i++){

printf("Value of element-%d: ",(i+1));

scanf("%d",&ele);

array[i]=ele;

}
printf("Enter no.of untis to shift: ");

scanf("%d",&shift);

for(int i=0;i<size;i++){

if(i<shift){

array2[5-shift+i+1]=array[i];

else{

array2[i-shift]=array[i];

printf("Array before: ");

for(int i=0;i<size;i++){

printf("%d ",array[i]);

printf("\n");

printf("Array after: ");

for(int i=0;i<size;i++){

printf("%d ",array2[i]);

printf("\n");

return 0;

}
OUTPUT:
PROGRAM 3: Write C Program to print the Floyd's Triangle using for loop.

1 |A
23 |BC
456 |DEF
7 8 9 10 |GHIJ

CODE:

// Online C compiler to run C program online

#include <stdio.h>

int main() {

int num=1;

for(int i=0;i<4;i++){

for(int j=0;j<i+1;j++){

printf("%d ",num);

num+=1;

}
printf("\n");

printf("\n");

int ch =(char) 65;

for(int i=0;i<4;i++){

for(int j=0;j<i+1;j++){

printf("%c ",ch);

ch+=1;

printf("\n");

return 0;

}
OUTPUT:

PROGRAM 4: Write a C program that implements a program to count the


number of digits in a given integer using a do-while loop and reverse the number
and display it.
CODE:

#include <stdio.h>

int main() {

int num,temp,sum=0,rev=0,count=0;

printf("Enter the number: ");

scanf("%d",&num);

temp=num;

do{

rev=rev*10+temp%10;

sum+=temp%10;

count+=1;

temp=temp/10;

}while(temp!=0);

printf("No.of digits in the number: %d\n",count);

printf("Sum digits in the number: %d\n",sum);

printf("Reverse of the number: %d\n",rev);

return 0;

}
OUTPUT:

PROGRAM 5: Write a C program that accepts a sequence of different values and


calculates the sum of the values before and after the maximum value.

Number of integers in the sequence: 5

Input the numbers:

12945

Sum before maximum value: 3

Sum after maximum value: 9

CODE:
// Online C compiler to run C program online

#include <stdio.h>

int main() {

int size,ele,max=0;

printf("Enter no.of integers for input: ");

scanf("%d",size);

int array[size];

for(int i=0;i<size;i++){

printf("Value of element-%d: ",(i+1));

scanf("%d",ele);

array[i]=ele;

if(ele>max){

max=ele;

int sum_before,sum_after;

for(int i=0;i<max;i++){

sum_before+=array[i];

for(int i=max+1;i<size;i++){

sum_after+=array[i];
}

printf("Max number: %d",max);

printf("Sum before max number: %d",sum_before);

printf("Sum after max number: %d",sum_after);

return 0;

OUTPUT:

You might also like