0% found this document useful (0 votes)
98 views15 pages

Assignment RV

The document contains C code snippets for various programs including: 1) A program to find the sum of digits of a given number. 2) A program to find numbers divisible by 7 that are less than 100 and calculate their sum. 3) A program to calculate the sum of a series up to n terms like 1+11+111+1111 etc. 4) Additional programs for tasks like finding the sum of 10 numbers, prime numbers less than 100, generating the Fibonacci series, finding occurrences of a number in a sequence, calculating the sum of the upper triangle of a matrix, finding the factorial of a number using a function, reversing a string, checking if a string is a palindrome, sorting 10

Uploaded by

Ashish Dwivedi
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)
98 views15 pages

Assignment RV

The document contains C code snippets for various programs including: 1) A program to find the sum of digits of a given number. 2) A program to find numbers divisible by 7 that are less than 100 and calculate their sum. 3) A program to calculate the sum of a series up to n terms like 1+11+111+1111 etc. 4) Additional programs for tasks like finding the sum of 10 numbers, prime numbers less than 100, generating the Fibonacci series, finding occurrences of a number in a sequence, calculating the sum of the upper triangle of a matrix, finding the factorial of a number using a function, reversing a string, checking if a string is a palindrome, sorting 10

Uploaded by

Ashish Dwivedi
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/ 15

Program to Find Sum of Digits of Given Number

#include <stdio.h>
void main()
{
int num, temp, digit, sum = 0;

printf("Enter the number \n");


scanf("%d", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number = %d\n", temp);
printf("Sum of the digits %d = %d\n", temp, sum);
}
Program to find list of no. Divisible by 7 and less than 100

#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum=0;
clrscr();
printf("All nos. between 1 - 100 which is divisible by 7\n");
for(i=1;i<100;i++)
{
if(i%7==0)
{
printf("%5d",i);
sum+=i;
}
}
printf("\n\nsum = %d",sum);
getch();
}
Program to find sum of series upto n terms 1+11+111+1111….. n

#include <stdio.h>

void main()
{
int n,i;
long sum=0;
long int t=1;
printf("Input the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%ld ",t);
if (i<n)
{
printf("+ ");

}
sum=sum+t;
t=(t*10)+1;
}
printf("\nThe Sum is : %ld\n",sum);
}
Program to find the sum of 10 numbers

#include <stdio.h>

int main()
{
int n=10, sum = 0, c, value;

printf("Enter %d integers\n", n);

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


{
scanf("%d", &value);
sum = sum + value;
}

printf("Sum of the integers = %d\n", sum);

return 0;
}
Program to find prime numbers less than 100

#include <stdio.h>

int main()
{
int i, Number, count;

printf(" Prime Number from 1 to 100 are: \n");


for(Number = 1; Number <= 100; Number++)
{
count = 0;
for (i = 2; i <= Number/2; i++)
{
if(Number%i == 0)
{
count++;
break;
}
}
if(count == 0 && Number != 1 )
{
printf(" %d ", Number);
}
}
return 0;
}
Program to Generate Fibonacci Series

#include <stdio.h>

void main()
{
int fib1 = 0, fib2 = 1, fib3, num, count = 0;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2;
while (count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
Program to Find no. of occurences of a Number in sequence

#include <stdio.h>

int main()
{
int num,tNum,digit,cnt;
int rem;

printf("Enter a number: ");


scanf("%d",&num);
printf("Enter digit to search: ");
scanf("%d",&digit);

cnt=0;
tNum=num;

while(tNum>0)
{
rem=tNum%10;
if(rem==digit)
cnt++;
tNum/=10;
}

printf("Total occurrence of digit is: %d in number: %d.",cnt,num);

return 0;
}}
Program to find sum of elements of upper triangle of a Matrix

#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
int main()
{
int A[MAX_ROWS][MAX_ROWS];
int row, col, sum = 0;
printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_COLS; col++)
{
scanf("%d", &A[row][col]);
}
}
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_COLS; col++)
{
if(col>row)
{
sum += A[row][col];
}
}
} printf("Sum of upper triangular matrix = %d", sum);

return 0;
}
Program to find factorial of no. using function

#include <stdio.h>

long factorial(int);

int main()
{
int number;
long fact = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &number);

printf("%d! = %ld\n", number, factorial(number));

return 0;
}

long factorial(int n)
{
int c;
long result = 1;

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


result = result * c;

return result;
}
Program to reverse a String

#include <stdio.h>

int main()
{
char s[1000], r[1000];
int begin, end, count = 0;

printf("Input a string\n");
gets(s);

// Calculating string length

while (s[count] != '\0')


count++;

end = count - 1;

for (begin = 0; begin < count; begin++) {


r[begin] = s[end];
end--;
}

r[begin] = '\0';

printf("%s\n", r);

return 0;
}
Program to check whether string is Palindrome or not
#include <stdio.h>
#include <sting.h>
int main()
{
char string1[20];
int I, length;
int flag = 0;
printf(“Enter a String”);
scanf(“%s”, string1);

length = strlen(string1);

for(I = 0 ; i<length ;i++)


{
if( string1[i] != string1[length -i-1]){
flag = 1;
break;
}}
if( flag)
printf(“Palindrome string”);
else
printf(“Not a Palindrome string”);
return 0;
}
Program to sort 10 Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j,temp , a[10];
printf(“ Enter 10 Integer no.”);
for( i = 0 ; i<10;i++)
{
scanf(“%d”, &a[i]);
}
for(i =0; i<10; i++)
{
for( j=i+1; j<10:j++)
{
if(a[i] > a[j])
{
temp = a[j];
a[j]=a[i];
a[i]=temp;
}}}
printf(“\n the 10 no. sorted are : “);
for(i = 0; i< 10; i++)
printf(“%d \t” , a[i]);
}
Program to Insert a number in the 5th Position in an Array of 10 Numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i,n;
printf(“Enter the elements of array”);
for( i = 0;i<10; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the elements to insert”);
scanf(“%d”, &n);
for( i = 9; i >=5;i++)
a[i+1] = array[i];
a[4] = n;
printf(“Resultant Array is \n”);
for(i = 0; i< 10; i++)
printf(“%d \t” , a[i]);

retuen 0;
}
Program to Find the Sum of series
1+x-x2 +x3…
#include<stdio.h>
#include<conio.h>
void main()
{
int i , x ,n ,sum =1 ;
printf(“Enter the value of x”);
scanf(“%d” , &x);

printf(“Enter the no. of terms”);


scanf(“%d” , &n);

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


{
if( i % 2 != 0)
sum = sum + (x^i);
else
sum = sum – (x^i);
}
printf(“Sum = %d”, sum);
return 0;
}

You might also like