CSE 101 CT-2
#Only printing something , this kind of function '' void func(){ //code
}'
Function://
return_type function_name(parameter_list) {
// body of the function
return value; // if return_type is not void
}
int getFive() {
return 5;
} //// a type of func;
#Declaring global variable:
#include <stdio.h>
int score = 0; // 🌍 Global variable
void increaseScore() {
score += 10;
}
void showScore() {
printf("Score: %d\n", score);
}
int main() {
increaseScore();
increaseScore();
showScore(); // Output: Score: 20
return 0;
}
Problems:
#lcd, bcd;
#Fibonacci;
#Factor, true factor ;
/*Write down a function that will take a
number n as a parameter and return nth Fibonacci number. In your main
function take a number N as user input
and use this function, to show N-th
Fibonacci number.*/
#include<stdio.h>
#include<math.h>
int check_prime(int n)
{
if(n<2){return 0;}
else if (n==2){return 1;}
else if(n%2==0){return 0;}
else {
int limit = sqrt(n);
for(int i=3; i<=limit; i+=2)
{
if(n%i==0){return 0;}
}
return 1;
}
}
int how_times(int a,int b)
{ int count =0;
while(a>0)
{
if(a%b==0){count ++;}
a=a/b;
}
return count;
}
void div_func(int a){
for(int i=2; i<=a/2; i++)
{
if(a%i==0 && check_prime(i)){printf("%d(%d) ", i, how_times(a, i));}
}
}
int main()
{
div_func(28);
}
#smax logic in Array :
for(int i=0; i<n ;i++){
if(arr[i]>max){ smax= max;max = arr[i]; }
else if(arr[i]>smax && arr[i]!=max){smax=arr[i];}
}
# distinct elements:
int k=0; int new_arr[100];
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j <i; j++) {
if ( arr[i] == arr[j] && i!=j) {
count=1;
break;
}
}
if (count == 0) {
new_arr[k++] = arr[i];
}
}
for(int i=0; i<k; i++)
{
printf("%d ", new_arr[i]);
}
# right side
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}//1 2 3 4 5 4 5 1 2 3
int position;
scanf("%d", &position);
int temp[position];
for (int i = 0; i < position; i++)
{
temp[i] = arr[n-position+i];
}
for (int i = n-position-1; i >= 0; i--)//2 1 0 432 2 1 0
{
arr[i+position] = arr[i];
}
for (int i = 0; i < position; i++)
{
arr[i]=temp[i];
}
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
#binary search
int binarySearch(int arr[], int n, int target)
{
int low=0; int high= n-1; int mid;
while(low<=high)
{mid= (low+high)/2;
if(arr[mid]==target){
return mid;
}
else if(target<arr[mid])
high= mid-1;
else low = mid+1;
}
return 0;
}
Types of Func:
| Type | Description | Example |
|---|---|---|
| ✅ Function with arguments and return value | Takes input, returns result | int add(int a, int b) |
| ✅ Function with arguments and no return value | Takes input, just performs a task | void printSum(int a, int b) |
| ✅ Function without arguments but with return value | No input, returns a result | int getRandom() |
| ✅ Function without arguments and no return value | No input or output, just does something | void greet() |
Comments
Post a Comment